Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Prototypes allow overloading subs?

by Dog and Pony (Priest)
on Dec 04, 2002 at 14:48 UTC ( #217497=note: print w/replies, xml ) Need Help??

in reply to Prototypes allow overloading subs?

Well, yes, sometimes. Especially since I see so much code trying to emulate this behaviour instead, checking if the first argument is a hashref, do this, if it is an arrayref, do that, if it is a coderef, do that etc.

Technically it would not be so much of a difference in most cases, codewise, if you had prototypes (*not* the normal Perl prototypes) do this, as most such overloaded functions do some quick reformatting and then passes the data on to the next in line. So it is really the same, but it is cleaner and easier to read/write. For those that are used to it. :)

But I don't really like that feature when I don't have default arguments to go with it (like C has and Java don't). So that would have to be included, because even though the workaround is simple, it is annoying. :)

Apart from the multimethod module suggested above (which looks pretty cool too), I guess one could do some "preprocessing" magic with a Filter module or something as an alternative. Like I said, having that kind of methods is very similar to do typechecking inside one function instead, so a source filter could probably accomplish this. If someone is bored enough to write it. :)

You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
Re: Re: Prototypes allow overloading subs?
by BUU (Prior) on Dec 05, 2002 at 02:38 UTC
    My immediate thought when you suggested the source filter module, is that it wouldnt be that hard, as you could just steal the method used by c compiler things (if i recall correctly...) that internally turn say "baz(int,int)" into "baz_int_int()", then when you do like "baz(1,1)", it knows how to pass it. The first step seems relatively simple, rewriting sub like "sub foo($,@)" into "sub foo_scalar_array", but that breaks down when you try to determine what the hell the sub is being passed. @x=(1,2); add(@x)?
      Perl is just too dynamic. The biggest obstacle is that you can often not know whether you're getting a list or scalar - much like in your example. It gets a whole lot worse when you say something like add(numbers($foo)) and numbers() can return anything from one invocation to the next.

      Makeshifts last the longest.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-06-06 05:40 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (26 votes). Check out past polls.