function overload (not operator overload)

Discussion in 'Perl Misc' started by Ying-Chieh Liao, Oct 11, 2004.

  1. 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
    Ying-Chieh Liao, Oct 11, 2004
    #1
    1. Advertising

  2. Ying-Chieh Liao

    Anno Siegel Guest

    Ying-Chieh Liao <> 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
    Anno Siegel, Oct 11, 2004
    #2
    1. Advertising

  3. Ying-Chieh Liao

    Anno Siegel Guest

    Ying-Chieh Liao <> 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
    Anno Siegel, Oct 11, 2004
    #3
  4. 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::eek:verloaded(@_);
    }
    }

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Oct 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    371
    Nick Hounsome
    Jan 25, 2004
  2. Fei Liu
    Replies:
    5
    Views:
    346
    Maxim Yegorushkin
    Feb 25, 2006
  3. Layton
    Replies:
    2
    Views:
    457
    Layton
    Sep 27, 2006
  4. Mahain
    Replies:
    3
    Views:
    1,014
    James Kanze
    Dec 20, 2007
  5. Replies:
    3
    Views:
    386
    Kai-Uwe Bux
    Nov 4, 2008
Loading...

Share This Page