Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: postfix syntax enlightenment

by hazylife (Monk)
on Mar 30, 2014 at 15:59 UTC ( [id://1080306]=note: print w/replies, xml ) Need Help??


in reply to Re: postfix syntax enlightenment
in thread postfix syntax enlightenment

One thing is still not entirely clear to me: what if my is within the condition of a postfix if?
use strict; if (my $x = 1) { print "x = $x\n" } # $x is not available outside of the if scope # this works, but is it valid? 1 if my $y = 3; print "y = $y\n"; for my $i (my @n = 1..3) { print "i = $i, n = (@n)\n" } # @n is not available here # the following code works as expected, but, again, is it valid? @ARGV = qw(/ /usr//local /etc/ sbin/); tr#/##s && s#(?<!^)/$## for my @copies = @ARGV; print "$_\n" for @copies;

Replies are listed 'Best First'.
Re^3: postfix syntax enlightenment
by AnomalousMonk (Archbishop) on Mar 30, 2014 at 19:19 UTC
    ... what if my is within the condition of a postfix if? ... is it valid?

    This is the way I look at it: The thing to remember is that the modifier clause (if that's the proper terminology) of a modified statement modifies the behavior of the statement. In order to do so, the modifier clause must always be evaluated. The ambiguity in a statement like
        my $x if $some_conditional;
    concerns whether (and when) the lexical definition is evaluated, but  $some_conditional (or whatever expression may be there) must always be evaluated.

    In a statement like
        0 if my $x = 1;
    there is no ambiguity: the lexical is always defined (and, in this example, initialized). Similarly, in the statement
        do_something($_) for my @ra = @rb;
    the for-loop initialization expression  my @ra = @rb must always be evaluated (including the definition and initialization of its lexical) in order that for may be able to control (i.e., modify the behavior of) the statement.

    So, is
        0 if my $x;
    and its ilk valid? Unquestionably (and unambiguously and without deprecation) yes, I would say. (But I'm too lazy right now to dig up a documentation citation.)

    Consider the following code. Also consider running | compiling it with  -MO=Deparse,-p added (often enlightening in these cases).

    c:\@Work\Perl\monks>perl -wMstrict -le "no warnings 'syntax'; ;; 0 if my $x = 42; print qq{$x}; ;; my @orig = qw(foo bar baz); tr{a-z}{A-Z} && printf qq{'$_' } for my @copy = @orig; print ''; print qq{(@orig) (@copy)}; ;; for (0 .. 2) { die 'Oops...' if my $x; print qq{\$x (still) undefined here} if not defined $x; $x = 42; print qq{\$x is $x here}; } " 42 'FOO' 'BAR' 'BAZ' (foo bar baz) (FOO BAR BAZ) $x (still) undefined here $x is 42 here $x (still) undefined here $x is 42 here $x (still) undefined here $x is 42 here

    (Also consider running your code with warnings enabled. And strict.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1080306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-16 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found