Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > function overload (not operator overload)

Reply
Thread Tools

function overload (not operator overload)

 
 
Ying-Chieh Liao
Guest
Posts: n/a
 
      10-11-2004
Can I do function overload in Perl (not operator) ?
I'd like to create a function, which accepts an object array or an
object iterator as argument ...

I've googled "perl function overload", but all I get is about operator
overload... what can I do in my case ? (except calling them by
func_array(@array) and func_iter($iter))
--
self-producing in perl :
$_=q(print"\$_=q($_);eval;");eval;
-- V Vinay
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-11-2004
Ying-Chieh Liao <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Can I do function overload in Perl (not operator) ?
> I'd like to create a function, which accepts an object array or an
> object iterator as argument ...


Well, you mention the keyword "object". In OO parlance this is
called method polymorphism.
Make two classes, one of arrays and another of iterators. Create
methods of the same name in each class that deal with their kind of
object. Then "$obj -> meth( ...)" will call the right method, whether
$obj is an array or an iterator.

> I've googled "perl function overload", but all I get is about operator
> overload... what can I do in my case ? (except calling them by
> func_array(@array) and func_iter($iter))


Without classes and objects, the best you can do is write a function
that looks at its argument and decides what kind it has been given,
then acts accordingly.

Anno
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-11-2004
Ying-Chieh Liao <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Can I do function overload in Perl (not operator) ?
> I'd like to create a function, which accepts an object array or an
> object iterator as argument ...


Well, you mention the keyword "object". In OO parlance this is
called method polymorphism. Make two classes, one of arrays and
another of iterators. Create methods of the same name in each class
that deal with their kind of object. Then "$obj -> meth( ...)" will
call the right method, whether $obj is an array or an iterator.

> I've googled "perl function overload", but all I get is about operator
> overload... what can I do in my case ? (except calling them by
> func_array(@array) and func_iter($iter))


Without classes and objects, the best you can do is write a function
that looks at its argument and decides what kind it has been given,
then acts accordingly.

Anno

 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      10-11-2004
Ying-Chieh Liao wrote:

> Can I do function overload in Perl (not operator) ?


Sort of - you can't create functions that share the same name, but have
different type signatures. But you *can* create a function that will
react differently according to what types of arguments were passed to it.

> I'd like to create a function, which accepts an object array or an
> object iterator as argument ...


Pass it an array reference, or an object reference, and check the
argument with ref():

sub overloaded {
my $self = shift;
my $argType = ref($_[0]);

if ($argType eq 'ARRAY') {
# First arg is array ref

} elsif ($argType eq 'IteratorClass') {
# First arg is an IteratorClass object

} else {
# It's something else - you can pass it to SUPER
return $self->SUPER:verloaded(@_);
}
}

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overload function call operator jr.freester@gmail.com C++ 3 11-04-2008 12:45 AM
How can we overload '=' operator using friend function c++ Mahain C++ 3 12-20-2007 06:29 PM
friend declaration or operator overload function cross namespace. Layton C++ 2 09-27-2006 10:38 PM
function template and operator () overload Fei Liu C++ 5 02-25-2006 12:28 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments