Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Basic Objects with Overloaded Operators

by educated_foo (Vicar)
on May 23, 2002 at 11:37 UTC ( [id://168794]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Basic Objects with Overloaded Operators

In playing with overloading in the past, I noticed that you can overload the <> "iteration" operator. However, it's not clear (to me) from the docs just what your function is supposed to do. Has anyone out there ever overloaded this, and if so, what are the rules?

/s

  • Comment on Re: Basic Objects with Overloaded Operators

Replies are listed 'Best First'.
Re: Re: Basic Objects with Overloaded Operators
by Zaxo (Archbishop) on May 30, 2002 at 21:31 UTC

    The Camel, 3ed. has an example:

    package LuckyDraw; use overload '<>' => sub { my $self = shift; return splice @$self, rand @$self, 1; }; sub new { my class = shift; return bless [@_] -> $self; } 1;
    The book does not discuss the rules, but I'm sure this example breaks some of them. To work with while (<$foo>){}, I think the return value should be true in 'bool' context until the source is exhausted. To cope with my @shuffle=<$deck>; and with slices, a wantarray clause should be there to shuffle and return the whole $deck, leaving $deck empty.

    The rules you observe should only be the ones you need to observe.

    After Compline,
    Zaxo

      2

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168794]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.