• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple Perl Problem

petrek

Senior member
When I run this program:

#!/usr/bin/perl -w

$a=1;
$b=2;
print qq(The statement "$a = $b" is); if ($a == $b) {# value is 2, therefore true print " true";} else{print " false";}

I get "Missing right curly or square bracket at perlt.txt line 5, at end of line
syntax error at perlt.txt line 5, at EOF
Execution of perlt.txt aborted due to compilation errors."

Yet when I spread it out to look like this:

#!/usr/bin/perl -w

$a=1;
$b=2;
print qq(The statement "$a = $b" is); if
($a == $b) {# value is 2, therefore true
print " true";}
else{print " false";}

It runs fine. I don't understand why Perl, which is supposed to be free form, is refusing to run the first program, even though it is exactly the same as the second except for the spacing.

Thanks
Dave
 
Because you have a comment in the middle of the line. Everything after the # is thought of to be comment, so you are missing a }
 
It runs fine. I don't understand why Perl, which is supposed to be free form, is refusing to run the first program, even though it is exactly the same as the second except for the spacing.

As Kilrsat said, it's not exactly the same. How did you expect perl to know when the comment ended and the code restarted?
 
I knew it was a simple mistake, just couldn't figure it as I'm still reading the Perl in 24 hours book.

Thanks, Dave
 
When they say "There's more than one way to do it" about perl, they mean you can do the same thing as above with this code:

$a = 1;
$b = 2;
print "The statement $a = $b is ";
print eval{$a == $b ? 'true' : 'false'}

Or this code:

$a = 2;
$b = 2;
print "The statement $a = $b is ";
$a == $b and print 'true' or print 'false';

or probably half a dozen other ways. They don't mean that you can just make up things like, 'the interpreter should know that my comment ended after the word true'.
 
Back
Top