Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: map BLOCK evaluation context

by tobyink (Abbot)
on Mar 08, 2013 at 14:20 UTC ( #1022431=note: print w/ replies, xml ) Need Help??


in reply to map BLOCK evaluation context

Nothing to do with map; it has more to do with precedence. The or operator always returns a scalar.

Consider:

print "D:"; for (qq{ab cd}) { print(/(\S+)/g or die); } print "\n";
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: map BLOCK evaluation context
Select or Download Code
Re^2: map BLOCK evaluation context
by vsespb (Hermit) on Mar 08, 2013 at 14:37 UTC
    Yes, but why in "A" example "or" enforces scalar, and in "C" it does not? Also: "or" does not always return a scalar.

      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
      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,

Re^2: map BLOCK evaluation context
by ikegami (Pope) on Mar 09, 2013 at 18:43 UTC

    The or operator always returns a scalar.

    No, only the LHS operand is evaluated in scalar context. The RHS inherits.

    >perl -E"@a=qw( a b c ); say( 0 or @a );" abc

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-02 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls