Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: map BLOCK evaluation context

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


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

Yes, but why in "A" example "or" enforces scalar, and in "C" it does not? Also: "or" does not always return a scalar.


Comment on Re^2: map BLOCK evaluation context
Re^3: map BLOCK evaluation context
by tobyink (Abbot) on Mar 08, 2013 at 14:50 UTC

    or can supply list contexts to its arguments, but it always returns a scalar... or at least, I've never seen it return a list.

    use Data::Dumper; my @A = qw(1 2 3); my @B = qw(A B C D); print Dumper(@A or @B);

    In example C, or does return a scalar. You just don't do anything with the scalar. print @list or die is not parsed as print(@list or die); it's parsed as print(@list) or die. Therefore, print only sees the list; it doesn't see the result of the or operator.

    Update: meh... the right hand side of or can result in or returning a list:

    use Data::Dumper; my @A = qw(); my @B = qw(A B C D); print Dumper(@A or @B);

    The left hand side cannot.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re^3: map BLOCK evaluation context
by vsespb (Hermit) on Mar 08, 2013 at 14:51 UTC
    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";
    prints
    X:123 Y:3

      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,

        Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-10-20 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (76 votes), past polls