print x if x > 5
is problematic in p5 for a whole bunch of reasons:(1) At a bare minimum, what you really need to say is:
print "$x\n" if $x > 5;
That extra "\n" is a definite turn-off to a lot of people when the get their very first look at Perl. Really, the default these days should be more "say"-like. Of course, "say" is there as of 5.10 if you know where to look for it, and you go out of your way to enable it: use feature 'say';
say $x if $x > 5;
(2) In "modern", better practice, production-quality Perl you pretty much have to either hedge every comparison op with a check for defined-ness: use feature 'say';
say $x if defined $x && $x > 5;
in order to head off an unsightly warning about $x not being initialized: Use of uninitialized value $x in numeric gt (>) at print-if.pl line 5.
or add even more boilerplate to suppress the warning: use feature 'say';
{
no warnings 'uninitialized';
print $x if $x > 5;
}
or be very attentive about how $x can mutate once it's gotten passed whatever validations you're imposing on it before it enters your scope -- via e.g. Params::Validate, Method::Signatures, provided you know these tools exist and are up to speed and hip as to how to use them effectively.(Yes, I know that in some contexts, the warning on 'uninitialized' can be very useful. But my point is, most of the time, and especially for novices, it's more of a misfeature).
(3) We shouldn't gloss over the fact that the magical pseudo-grammatical construction the "infix" if syntax provides is itself problematic in many ways.
In the current example, it simply isn't clear -- until you've become very comfortable with the language! -- if the original statement
say $x if $x > 5;
is or is not equivalent to the very similar construction $foo = $x if $x > 5;
say $foo
This is because it is by no means obvious as to which of the more "active" parts of the original expression -- either the "verb" (print) or the conditional (if) -- takes precedence.And unfortunately, it's not only possible, but terribly easy to "whip up" fare more volatile constructions like this in Perl 5. Various on this theme -- gotchas about print, conditionals, and complex "if/unless" constructions -- were of course given ample treatment in Perl Better Practices, to the point where (I believe) that Damian pretty much eschews "trailing" if/unless constructions altogether.
(4) Lastly, it's sad to say that for all the complains of Python having too many safety belts and what not, it completely nails the hidden complexity management issues embedded in such a simple construct like Perl's "print-if" idiom.
In Python, basically the only sensible way to achieve the original desired result is to do this:
if x > 5:
print x
Perfectly clear, completely unambiguous, and basically impossible to screw up.