in reply to statement vs. block?

Something like this?

our ($For, $While, $Map, $Grep, $If) = qw( For While Map Grep If) ; local $For = 'local' for 1; local $While = 'local' while ! $a++; map +(local $Map = 'local'), 1; grep +(local $Grep = 'local'), 1; local $If = 'local' if 1; print "For: $For\n"; print "While: $While\n"; print "Map: $Map\n"; print "Grep: $Grep\n"; print "If: $If\n"; __END__ For: For While: While Map: Map Grep: Grep If: local
Here, all loop constructs introduces a scope for the expression just as they would if it was a block. if however doesn't.

While speaking about blocks for map and grep (and sort):

map { use strict; } LIST
fails with '"use" not allowed in expression ...'. A do block can be used to "upgrade" the block though avoid the ambiguity (see ysth's reply:
map do { use strict; }, LIST
works. (Interestingly, this uses map EXPR, LIST ...)

lodin

Replies are listed 'Best First'.
Re^2: statement vs. block?
by ysth (Canon) on Apr 13, 2008 at 04:59 UTC
    map { use strict; } LIST
    fails with '"use" not allowed in expression ...'.
    I think this is just a funny case of guessing that "{ bareword" begins an expression, not a block, but it dies before even noticing the missing , after }.

    Disambiguating it as map {; use strict; } @ARGV works fine.

Re^2: statement vs. block?
by ikegami (Patriarch) on Apr 13, 2008 at 05:28 UTC

    Here, all loop constructs introduces a scope for the expression just as they would if it was a block. if however doesn't.

    What if?

    >perl -MO=Deparse -e"local $If = 'local' if 1;" local $If = 'local'; -e syntax OK

    However, fixing the test so if actually exists at run-time doesn't alter the results.