perl polymorphic behavoir ?

Discussion in 'Perl Misc' started by surf, Feb 7, 2006.

  1. surf

    surf Guest

    I'm new at trying OO in perl and somewhat rusty on OO in general, but
    is there a limitation here ? Below I call foo2() from a subclass.
    foo2() in the base class calls foo3(). Since my instance is the
    subclass I think it should call the subclass foo3() but it calls the
    base class. If there isn't a way to do this, I would think perl is
    lacking
    in OO features, am I wrong ?





    package A;

    sub foo2 {

    print("Inside A::foo\n");
    foo3();
    }


    sub foo3
    {
    print("foo3 A\n");
    }

    package B;

    @ISA = (A);


    sub foo3
    {
    print("foo3 B\n");
    }

    sub foo {

    print("Inside B::foo\n");

    }



    package main;

    B->foo2();
    surf, Feb 7, 2006
    #1
    1. Advertising

  2. surf

    Guest

    "surf" <> wrote:
    > I'm new at trying OO in perl and somewhat rusty on OO in general, but
    > is there a limitation here ?


    You are not using OO in perl. Or at least you are not doing so at the
    point where the problem occurs.

    > Below I call foo2() from a subclass.
    > foo2() in the base class calls foo3(). Since my instance is the


    What instance? You are not using an instance.

    > subclass I think it should call the subclass foo3() but it calls the
    > base class. If there isn't a way to do this, I would think perl is
    > lacking
    > in OO features, am I wrong ?
    >
    > package A;
    >
    > sub foo2 {
    >
    > print("Inside A::foo\n");
    > foo3();


    This is just a plain old subroutine call. It is not a method call.
    A method call would look something like:

    $_[0]->foo3(); # usually $self->foo3(), but since you don't create $self...


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Feb 7, 2006
    #2
    1. Advertising

  3. surf

    Lukas Mai Guest

    surf <> schrob:
    > I'm new at trying OO in perl and somewhat rusty on OO in general, but
    > is there a limitation here ? Below I call foo2() from a subclass.
    > foo2() in the base class calls foo3(). Since my instance is the
    > subclass I think it should call the subclass foo3() but it calls the
    > base class. If there isn't a way to do this, I would think perl is
    > lacking
    > in OO features, am I wrong ?
    >


    (note: code rearranged)

    Missing:
    use warnings;
    use strict;

    > package B;
    >
    > @ISA = (A);

    ^ You forgot to quote this string.
    use strict would have told you that.
    ^ @ISA is implicitly global.
    use strict would have told you that.

    > sub foo3
    > {
    > print("foo3 B\n");
    > }
    >
    > sub foo {
    >
    > print("Inside B::foo\n");
    >
    > }
    >
    > package main;
    >
    > B->foo2();


    This is a class method call. B::foo2 doesn't exist, so it follows @ISA
    and calls A::foo2 (as &A::foo2('B')).

    > package A;
    >
    > sub foo3
    > {
    > print("foo3 A\n");
    > }


    > sub foo2 {
    >
    > print("Inside A::foo\n");
    > foo3();
    > }


    This is an ordinary function call. We're in package A, so it calls
    A::foo3(), period. If you want method call semantics, you need to use ->
    (as in INVOCANT->METHOD()). If you want to use the same class that was
    passed in, er ... just do it:

    $_[0]->foo3();

    It's more readable to use an extra variable, though:

    my $class = shift;
    $class->foo3;
    Lukas Mai, Feb 7, 2006
    #3
  4. "surf" <> wrote in news:1139330403.956594.253620
    @g44g2000cwa.googlegroups.com:

    > I'm new at trying OO in perl and somewhat rusty on OO in general, but
    > is there a limitation here ? Below I call foo2() from a subclass.
    > foo2() in the base class calls foo3(). Since my instance is the
    > subclass I think it should call the subclass foo3()


    You are not calling an instance method. You are calling a class method.
    But that has nothing to do with the problem.

    but it calls the
    > base class. If there isn't a way to do this, I would think perl is
    > lacking in OO features, am I wrong ?


    You are correct. You cannot do OO in Perl. You should switch to PHP.

    use strict;
    use warnings;

    missing.

    > package A;
    >
    > sub foo2 {
    >
    > print("Inside A::foo\n");
    > foo3();
    > }
    >
    >
    > sub foo3
    > {
    > print("foo3 A\n");
    > }
    >
    > package B;
    >
    > @ISA = (A);
    >
    >
    > sub foo3
    > {
    > print("foo3 B\n");
    > }
    >
    > sub foo {
    >
    > print("Inside B::foo\n");
    >
    > }
    >
    >
    >
    > package main;
    >
    > B->foo2();


    Your subs are not methods. A method pays attention to the first argument
    in the invocation.

    You can figure out the rest.

    I have yet to see from you an attempt to put some time and effort into
    your questions. You do not follow the posting guidelines, and your code
    is awfully formatted.

    Sinan


    --
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Feb 7, 2006
    #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. Markus Franz

    Processes with strange behavoir

    Markus Franz, Apr 4, 2004, in forum: Python
    Replies:
    2
    Views:
    326
    Peter Otten
    Apr 4, 2004
  2. Joachim Schmitz

    sscanf behavoir

    Joachim Schmitz, Apr 19, 2007, in forum: C Programming
    Replies:
    7
    Views:
    432
    Gunvant Patil
    Apr 20, 2007
  3. Replies:
    5
    Views:
    126
    YANAGAWA Kazuhisa
    Mar 8, 2006
  4. Sy Ys

    BackgrounDRb behavoir

    Sy Ys, May 27, 2007, in forum: Ruby
    Replies:
    1
    Views:
    94
    Ezra Zygmuntowicz
    May 27, 2007
  5. Daniel Rucareanu

    Weird behavoir while using function aliases

    Daniel Rucareanu, Sep 19, 2006, in forum: Javascript
    Replies:
    22
    Views:
    204
    Richard Cornford
    Sep 25, 2006
Loading...

Share This Page