in reply to defined ? to be : not

translates to

$dec eq "to be" ? print "Romeo is" : print "Romeo is not";
I think it is better to translate that to

print $dec eq 'to be' ? 'Romeo is' : 'Romeo is not';

or even

print 'Romeo is', $dec eq 'to be' ? '' : ' not';

There is less repetition and less chance of running into precedence problems this way. Note that I use single quotes as there is no interpolation happening.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: defined ? to be : not
by Don Coyote (Hermit) on Jun 13, 2012 at 08:44 UTC

    Passing the syntactic if conditional as an argument to print certainly would help with compacting the code and avoiding precedence. For a moment I wasn't sure what would happen if once evaluating to true a zero length string was returned, but of course in a list context it would be discarded as a value.

    The initial translation was a straight forward syntactic representation of the if condition. This meditation is aimed at the learning rather than the learned. And as you will see very quickly after this presentation of syntax I do replace the print romeo statement prior to the if conditional. Though you did manage shave off a few more keystrokes

    In this meditation interpolation is a factor, as the code is being executed on the command line through the use of the perl command and -e flag. So using the q() operator certainly helps with syntax errors on the command line due to numerous single quotes.

    Just like to add here a couple more useful switches for perl commands.

    perl -we 'code'. Initiates warnings, but continues to run the code if it can

    Shell$ perl -we 'my $word = q(yey);print qq($sword\n);' prints to tty: Name "main::sword" used only once: possible typo at -e line 1. Use of uninitialized value $sword in Concatenation (.) or String at -e + line 1. Shell$ _

    perl -ce compiles the code without attempting to execute and returns compilation errors.

    Shell$ perl -ce 'my $word = q(yey)); print qq($word\n);' prints to tty: syntax error at -e line 1, near "q(yey))" -e had compilation errors $Shell _

    Both very useful debugging switches. Pointing out different possible errors. Use along with 'use strict;' which can also be typed as first statement in a one liner on the command line. When combined, you have a fairly robust debugging practice. On the other hand, not knowing your Hamlet from your Romeo & Juliet is just plain unforgivable.

    Coyote