function overload (not operator overload)

Y

Ying-Chieh Liao

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))
 
A

Anno Siegel

Ying-Chieh Liao said:
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
 
A

Anno Siegel

Ying-Chieh Liao said:
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
 
S

Sherm Pendley

Ying-Chieh Liao said:
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::eek:verloaded(@_);
}
}

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top