#!/usr/bin/perl #!/dfs/prod/ipn/bin/perl $CoC_Number=123; print "With \$CoC_Number=123,\n\n"; print "\n----------------- print function -----------------\n\n"; print "This is print using commas, i.e.\nprint \"Extracting CoC number \" , \$CoC_Number+1 , \" ...\\n\"\n"; print "Extracting CoC number " , $CoC_Number+1 , " ... which is as expected.\n"; # prints Extracting CoC number 124 ... which is as expected. print "\nThis is print using periods, i.e.\nprint \"Extracting CoC number \" . \$CoC_Number+1 . \" ...\\n\"\n"; print "Extracting CoC number " . $CoC_Number+1 . " ... which is NOT as expected.\n"; print " Evidently, the dot operator (concatenation) has the same precedence as\n"; print " the + operator. What's happening is Perl is concatenating the\n"; print " \"Extracting CoC number \" string with \$CoC_Number (123) and adding 1 to\n"; print " that. But the C language a2i routine parses \"Extracting CoC number 123\"\n"; print " looking for an integer, stops at the first non-digit (the \"E\"), and\n"; print " returns a 0, so the string so far is 0+1, or 1. Then Perl concatenates\n"; print " another string to that, resulting in the string \"1 ...\".\n"; # prints 1 ... which is NOT as expected. # also Evidently, the dot operator (concatenation) has the same precedence as # also the + operator. What's happening is Perl is concatenating the # also "Extracting CoC number " string with $CoC_Number (123) and adding 1 to # also that. But the C language a2i routine parses "Extracting CoC number 123" # also looking for an integer, stops at the first non-digit (the "E"), and # also returns a 0, so the string so far is 0+1, or 1. Then Perl concatenates # also another string to that, resulting in the string "1 ...". print "\nThis is print using periods and parenthesis, i.e.\nprint \"Extracting CoC number \" . (\$CoC_Number+1) . \" ...\\n\"\n"; print "Extracting CoC number " . ($CoC_Number+1) . " ... which is ok again.\n"; # prints Extracting CoC number 124 ... which is ok again. print "\n\n\n"; # --------------------------- Assignment --------------------------- # print "\n----------------- Assignment -----------------\n\n"; print "This is assignment using commas, i.e.\n\$a = \"Extracting CoC number \" , \$CoC_Number+1 , \" ...\"\nthen a print \"\\\$a=>\$a<\\n\"\n"; $a = "Extracting CoC number " , $CoC_Number+1 , " ..."; print "\$a=>$a< which is just a wrong use of the comma operator,\n"; print " but it shows that assignment has a higher\n"; print " precedence than the comma operator.\n"; # prints $a=>Extracting CoC number < which is just a wrong use of the comma operator, # also but it shows that assignment has a higher # also precedence than the comma operator. print "\nUsing parenthesis changes it, i.e.\n\$a = (\"Extracting CoC number \" , \$CoC_Number+1 , \" ...\")\nthen a print \"\\\$a=>\$a<\\n\"\n"; $a = ("Extracting CoC number " , $CoC_Number+1 , " ..."); print "\$a=>$a< Again, just wrong use of the comma operator,\n"; print " but shows right-most value being returned.\n"; # prints $a=> ...< Again, just wrong use of the comma operator, # also but shows right-most value being returned."; print "\nDoing assignment correctly using periods instead of commas, i.e.\n\$a = \"Extracting CoC number \" . \$CoC_Number+1 . \" ...\"\nthen a print \"\\\$a=>\$a<\\n\"\n"; $a = "Extracting CoC number " . $CoC_Number+1 . " ..."; print "\$a=>$a< which is NOT as expected (same reason as above).\n"; # prints $a=>1 ...< which is NOT as expected (same reason as above). print "\nAgain, using parenthesis fixes things, same as above, i.e.\n\$a = \"Extracting CoC number \" . (\$CoC_Number+1) . \" ...\"\nthen a print \"\\\$a=>\$a<\\n\"\n"; $a = "Extracting CoC number " . ($CoC_Number+1) . " ..."; print "\$a=>$a< which is ok.\n"; # prints $a=>Extracting CoC number 124 ...< which is ok.