Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Overloading Subs in Perl

by cjf-II (Monk)
on Dec 04, 2002 at 06:02 UTC ( [id://217423]=note: print w/replies, xml ) Need Help??


in reply to Prototypes allow overloading subs?

If you want clean function overloading in Perl take a look at Class::Multimethods.

There's also the appropriately named overload module, which I have yet to use but looks interesting.

Update: Here's an example with Class::Multimethods:

use Class::Multimethods; multimethod add => ('#', '#') => sub { $ans = $_[0] + $_[1]; print $ans, "\n" }; multimethod add => ('$', '$') => sub { $ans = $_[0] . $_[1]; print $ans, "\n" }; add(1, 2); add("Foo", "Bar");

Is this what you're thinking of?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://217423]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found