Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Overloading oddity

by theguvnor (Chaplain)
on Jan 27, 2004 at 21:41 UTC ( #324542=perlquestion: print w/replies, xml ) Need Help??

theguvnor has asked for the wisdom of the Perl Monks concerning the following question:

Today I found something interesting. I've used overloading in the past without trouble but today wasn't getting stringification to work. I reduced my problem to the following test case:

package SomeClass; use overload '""'=>"name"; sub new { return bless {}, shift; } sub name { my $self = shift; $self->{NAME} = shift if @_; return $self->{NAME}; } 1;

And here's my test script to use the class above:

use lib '.'; use SomeClass; my $thing = SomeClass->new; $thing->name('TestObj'); print "Explicit: my name is ", $thing->name, "\n"; print "Implicit: my name is $thing\n"; exit;

The second print statement printed "Implicit: my name is " whereas the first print worked as desired, including the name.

I tested the above under both 5.8.0 Linux-multi-thread and 5.8.0 MSWin-multi-thread and eventually came to realize that I could get it to work by pointing my overload directive to a third function in SomeClass:

sub stringify { my $self = shift; return $self->{NAME}; }

Any ideas why my first attempt failed? I can't see any good reason why my stringify() method works but not name(). Thanks in advance.

[Jon]

Replies are listed 'Best First'.
Re: Overloading oddity
by BrowserUk (Pope) on Jan 27, 2004 at 22:33 UTC

    The problem is that your overload function does not take into account the differences when being called as a conventional method, and when it is called via the overloading. In the latter case, there is an extra 3rd parameter identifying the ordering of the operands for the operator.

    From the overload pod:

    Calling Conventions for Binary Operations The functions specified in the use overload ... directive are called with three (in one particular case with four, see Last Resort) arguments. If the corresponding operation is binary, then the first two arguments are the two arguments of the operation. However, due to general object calling conventions, the first argument should always be an object in the package, so in the situation of 7+$a, the order of the arguments is interchanged. It probably does not matter when implementing the addition method, but whether the arguments are reversed is vital to the subtraction method. The method can query this information by examining the third argument, which can take three different values: FALSE - the order of arguments is as in the current operation. TRUE - the arguments are reversed. undef - the current operation is an assignment variant (as in $a+=7), but the usual function is called instead.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!

      Thanks BrowserUK - I read perldoc overload but didn't recognize that this was the problem. I guess the only decent solution is to provide the stringify() function explicitly that returns the name without the extra functions in the name() mutator method.

      [Jon]

Re: Overloading oddity
by Ovid (Cardinal) on Jan 27, 2004 at 21:59 UTC

    I'm a bit confused because when overload is triggered, it appears to be passing 'undef':

    #!/usr/local/bin/perl + package SomeClass; use Data::Dumper; + use overload '""' => \&name; sub new { return bless {}, shift; } + sub name { print Dumper \@_; my $self = shift; $self->{NAME} = shift if @_; return $self->{NAME}; } + package main; my $thing = SomeClass->new; $thing->name('TestObj'); print "Implicit: my name is $thing\n";

    That gives the following output:

    $VAR1 = [ bless( {}, 'SomeClass' ), 'TestObj' ]; $VAR1 = [ bless( { 'NAME' => 'TestObj' }, 'SomeClass' ), undef, '' ]; Implicit: my name is

    We can clearly see 'undef' is being passed and that's what's throwing off your conditional. This is so counter-intuitive that I assume I'm missing something, though a quick scan through the docs doesn't reveal what that would be.

    Cheers,
    Ovid

    New address of my CGI Course.

      We can clearly see 'undef' is being passed and that's what's throwing off your conditional. This is so counter-intuitive that I assume I'm missing something, though a quick scan through the docs doesn't reveal what that would be.
      The relevant part of the documentation is:
      Calling Conventions for Binary Operations The functions specified in the "use overload ..." direc­ tive are called with three (in one particular case with four, see "Last Resort") arguments. If the corresponding operation is binary, then the first two arguments are the two arguments of the operation. However, due to general object calling conventions, the first argument should always be an object in the package, so in the situation of "7+$a", the order of the arguments is interchanged. It probably does not matter when implementing the addition method, but whether the arguments are reversed is vital to the subtraction method. The method can query this infor­ mation by examining the third argument, which can take three different values: FALSE the order of arguments is as in the current opera­ tion. TRUE the arguments are reversed. "undef" the current operation is an assignment variant (as in "$a+=7"), but the usual function is called instead. This additional information can be used to generate some optimizations. Compare "Calling Conventions for Mutators".
      The first argument is the object, no surprise here. Since the stringify operation is a unary operation, the second argument is undefined. The third argument is the empty string, which is false, but defined. And this fits the description of the documentation - the arguments aren't reversed, nor is the operation an assignment variant.

      Abigail

      Thanks for the idea to check what's being passed into name(). I confess I really have no idea why the undef is being passed in there either though.

      [Jon]

Re: Overloading oddity
by ysth (Canon) on Jan 27, 2004 at 23:01 UTC
    Looks to me like all overload routines will get the same 3 or 4 parameters. The conversion "operators" seem to follow the same rules as unary operators, which are the rules for binary operators tweaked a little: right (or only) operand, left operand (=undef if unary), reversed/assignment flags, and desired method (only if calling nomethod).
Re: Overloading oddity
by dragonchild (Archbishop) on Jan 28, 2004 at 03:56 UTC
    Try this:
    sub name { $_[0]{NAME} = $_[1] if defined $_[1]; $_[0]{NAME}; }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Good on you dragonchild. I tend to not work with elements of @_ directly as I'm always afraid that the "readability police" will get me ;-) but that snippet is fairly clean and clear. Always good to make the brain re-examine its assumptions. Thanks.

      [Jon]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://324542]
Approved by kvale
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2020-04-09 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (47 votes). Check out past polls.

    Notices?