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

I am porting a Java class that calculates RealChallenge for RTSP. That package uses both Java operator >> and >>>. When I port it to Perl, I tried overloading. But I came to some questions.

I can mimic >> and >>> easily with use integer:

use strict; use warnings; print unsigned_shift(-4, 2), "\n"; print signed_shift(-4, 2), "\n"; print unsigned_shift(-4, 2), "\n"; print signed_shift(-4, 2), "\n"; print unsigned_shift(-4, 2), "\n"; print signed_shift(-4, 2), "\n"; sub signed_shift { use integer; return $_[0] >> $_[1]; } sub unsigned_shift { return $_[0] >> $_[1]; }

But the following does not work, but gives no warning/error. I guess you cannot overload core function, or did I do soemthing wrong?

use Data::Dumper; use strict; use warnings; use overload '>>' => \&singed_shift; #use overload 'main::>>' => \&singed_shift; #print Dumper(\%OVERLOAD); print -4 >> 2; sub signed_shift { use integer; return $_[0] >> $_[1]; }

I can put this in a package which will work:

package Number; use strict; use warnings; use overload '>>' => \&signed_shift; sub new { my $self; $self->{"number"} = $_[1]; bless $self; return $self; } sub signed_shift { my ($self, $n) = @_; use integer; return ($self->{"number"} >> $n); } 1; use Data::Dumper; use Number; use strict; use warnings; my $n = new Number(-4); print $n >> 2;

The other question is whether I can "overload" an operator that does not exist. for example whether I can '>>>' => \&blah. When I tried it, it didn't work (didn't like >>>). I don't think I can do that.

Replies are listed 'Best First'.
Re: overloading core function and non-existing operator
by Zaxo (Archbishop) on Aug 07, 2005 at 22:34 UTC

    Spelling, s/singed/signed/.

    You're correct that the Perl5 parser cannot manage undefined operators. You only get to overload existing operators. Perl6 is supposed to remedy that.

    In any language, operator overloading is something that needs to be done with taste and discretion.

    After Compline,
    Zaxo

      You're correct that the Perl5 parser cannot manage undefined operators. You only get to overload existing operators. Perl6 is supposed to remedy that.
      Wasn't there a post recently (on another topic) about creating subroutine names out of any charater string, and not just the standard set? (Maybe it was in obfuscation, but I can't find it.)

      Update: Added quote, plus followup:

      Here it is: What is this doing, and why is this a good method of doing it?, and where Zaxo describes what it does.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: overloading core function and non-existing operator
by tlm (Prior) on Aug 07, 2005 at 22:38 UTC

    I think you're misunderstanding the meaning of overloading, at least in Perl. Overloading does not mean (re)-defining a core operator, but rather extending it to work on instances of particular classes. IOW, what you found with your Number class is exactly the way overloading is supposed to work in Perl.

    the lowliest monk

      Just want to let my mind fly freely for a moment, with Perl and yet not ...

      Say it is more consistent, and main scope itself is also a package just like any other package. Will that help to make ++ overloadable in main? My answer to myself was probably no, as the other side of this issue, probably a more important one is not about scope, but about "native data type". Even if you can overload with main scope, it makes sense not to let one overload operators against native data type, for example scalar.

        and main scope itself is also a package just like any other package. Will that help to make ++ overloadable in main?

        sure, if you see it that way... you just have to bless your variable, just like you did with your Number-package.

        use overload ...; my $n = bless {number => 23}, "main"; print $n >> 2;
        i think that's not what you want, just wanted to make clear that it's not a problem of the package name but that you must have a blessed object.