SPUG: Strange code effect

BenRifkah Bergsten-Buret mail.spammagnet at gmail.com
Wed Dec 23 16:07:23 PST 2009


On Wed, Dec 23, 2009 at 3:27 PM, Joseph Werner <telcodev at gmail.com> wrote:

> On Wed, Dec 23, 2009 at 2:52 PM, Skylos <skylos at gmail.com> wrote:
> > ...
> > Which makes me think "something about that value when a shift is done
> uses
> > the value in a boolean context"
> > ...
>
> Thanks for the feedback Skylos, This is exactly the same conclusion
> that we have come too; but I STILL cannot place a boolean context in
> the a simple shift assignment. The powers that be crack the whip and
> say "Problem solved, move on".   Code that does not behave the way I
> expect it to bothers me...
>
>
I was considering that shift had an implicit boolean context as well so did
some digging.  Based on my test implementation it appears that shift doesn't
have an implicit boolean context.  Perhaps the boolean context is occuring
after the $choosenO is returned?

Here's my test script is_shift_boolean.pl:
      1 #!/usr/bin/perl
      2
      3 use strict;
      4 use warnings;
      5 use Carp;
      6
      7 my @objects = map{Snoop->new()} 1..3;
      8
      9 # Implicit boolean context here?
     10 my $first = shift @objects;
     11
     12 # explicit boolean context here
     13 if ($first) {
     14   # nothing to do here.
     15 }
     16
     17 package Snoop;
     18
     19 use overload (
     20   q{bool} => sub {
     21         my $self = shift;
     22         Carp::confess("Boolean context on objnum $self->{objnum}");
     23         return $self;
     24       },
     25 );
     26
     27 my $objnum = 0;
     28
     29 sub new {
     30   my $class = shift;
     31   return bless {objnum => $objnum++}, $class;
     32 }
     33 __END__

This uses the overload pragma to do the operator overloading so if you're
using something else the results may be different.

Upon execution I got the following output:
Boolean context on objnum 0 at is_shift_boolean.pl line 22
        Snoop::__ANON__('Snoop=HASH(0x814ccd4)', 'undef', '') called at
is_shift_boolean.pl line 13

This reports only one boolean context in the if statement at line 13.  There
is no report of boolean context from line 10 where the shift is.

A mystery for the ages,

-- 
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20091223/0461b389/attachment.html>


More information about the spug-list mailing list