SPUG: Strange code effect
Skylos
skylos at gmail.com
Wed Dec 23 16:19:40 PST 2009
Ooh good point Ben!
What is there was something else that hit the bool functionality of the
object though? It occurs to me that sorting can be a complex operation -
maybe something happened when it was sorted, like this mod of your lil test
script?
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 my @objects = sort reverse 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 q{cmp} => sub {
26 my $self = shift;
27 my $other = shift;
28 if ($self) {
29 Carp::confess "Did I bool in the cmp?";
30 }
31 return $self->{objnum} <=> $other->{objnum};
32 },
33 );
34
35 my $objnum = 0;
36
37 sub new {
38 my $class = shift;
39 return bless {objnum => $objnum++}, $class;
40 }
41 __END__
That outputs for me:
Boolean context on objnum 2 at testbool line 22
Snoop::__ANON__('Snoop=HASH(0x8924c8c)', 'undef', '') called at
testbool line 28
Snoop::__ANON__('Snoop=HASH(0x8924c8c)', 'Snoop=HASH(0x8918258)',
'') called at testbool line 7
One thing I've learned for sure now is that if you're going to use the
override pragma, you probably should define something for *all* of the
operators/contexts - otherwise, you risk the undefined operation error all
over the place! I couldn't sort the array without defining the cmp
operation...
But seriously, maybe thats how it happened, due to the lack of actual
feedback about where the error was, it perhaps *seemed* to be on the shift
when it was somewhere down in the sort?
An idea at any rate,
Skylos
"If only I could get rid of hunger by rubbing my belly" - Diogenes
On Wed, Dec 23, 2009 at 4:07 PM, BenRifkah Bergsten-Buret <
mail.spammagnet at gmail.com> wrote:
> 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/ff26f582/attachment-0001.html>
More information about the spug-list
mailing list