Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: map BLOCK evaluation context

by vsespb (Chaplain)
on Mar 08, 2013 at 14:51 UTC ( #1022437=note: print w/replies, xml ) Need Help??

in reply to Re^2: map BLOCK evaluation context
in thread map BLOCK evaluation context

Aha, you're right. The thing was that
print "C:"; for (qq{ab cd}) { print /(\S+)/g or die; # prints "C:abcd", list context again } print "\n";
was evaluated just like
print "C:"; for (qq{ab cd}) { print(/(\S+)/g) or die; } print "\n";
It can be fixed like this:
print "D:"; for (qq{ab cd}) { print /(\S+)/g || die; } print "\n";
However seems "or" not always enforces scalar context:
my @a = (1,2,3); print "X:"; print (0 || @a); print "\n"; print "Y:"; print (@a || 0); print "\n";
X:123 Y:3

Replies are listed 'Best First'.
Re^4: map BLOCK evaluation context
by Athanasius (Chancellor) on Mar 08, 2013 at 15:43 UTC

    After noting that:

    The && and || operators differ from C’s in that, rather than returning 0 or 1, they return the last value evaluated.

    the Camel Book (4th Edition, 2012, page 120) goes on to explain that the || operator imposes scalar context on its left operand to facilitate chaining in assignment:

    ...this has the delightful result that you can select the first of a series of scalar values that happens to be true. Thus, a reasonably portable way to find out the user’s home directory might be:
    my $home = $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($<)[7] || die "You're homeless!\n";
    On the other hand, since the left argument is always evaluated in scalar context, you can’t use || for selecting between two aggregates for assignment:
    @a = @b || @c; # This doesn't do the right thing @a = scalar(@b) || @c; # because it really means this. @a = @b ? @b : @c; # This works fine though.

    (There is a truncated version of this explanation in Logical Defined Or in perlop, but the Camel Book is clearer.)

    As tobyink noted above, scalar context is not imposed on the right operand.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022437]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-10-21 14:51 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (270 votes). Check out past polls.