Professional Web Applications Themes

Method invocation arrow (LPORM) - PERL Beginners

Hi all, I just work my way through "Learning Perl Objects, References & Modules". Now at one point, I am stuck: Randal introduces classes and methods in Chapter 8. He gives the following example for overriding methods: { package Mouse ISA = qw{Animal}; ... sub speak { my $class = shift; ... Animal::speak($class); ... } } Since there is a method speak in Mouse, it would override the parent's classes method Animal::speak if the latter were not called explicitly. But, as Randal points out, this forces Perl to look for speak in Animal andnowhere else - without the method invocation ...

  1. #1

    Default Method invocation arrow (LPORM)

    Hi all,

    I just work my way through "Learning Perl Objects, References & Modules". Now at one point, I am stuck: Randal introduces classes and methods in Chapter 8.

    He gives the following example for overriding methods:

    { package Mouse
    ISA = qw{Animal};
    ...
    sub speak {
    my $class = shift;
    ...
    Animal::speak($class);
    ...
    }
    }

    Since there is a method speak in Mouse, it would override the parent's classes method Animal::speak if the latter were not called explicitly.

    But, as Randal points out, this forces Perl to look for speak in Animal andnowhere else - without the method invocation arrow, it cannot check ISA for ancestor classes.

    So far, I get the point. But then he introduces the following solution:

    $class->Animal::speak(_);

    Apart from the fact that _ should be unnecessary here (or did I get something wrong), this should expand to:

    Mouse::Animal::speak("Mouse");

    And when Perl does not find Animal::speak in Mouse, to:

    Animal::Animal::speak("Mouse";

    and to higher classes if speak is not found there. Now my question:

    Does Perl reduce Animal::Animal::speak("Mouse") automatically to Animal::speak("Mouse"). And, if speak is not in Animal, how does it handle an expression like:

    LivingCreature::Animal::speak("Mouse");

    It seems to me that the hard-coded package name we got rid of by using method calls just got back into our syntax. At the same time, I am sure the code works (have not tried it yet) and Perl does as it should.

    But how does this work?

    Thanks for any explanations,

    Jan
    --
    There's no place like ~/
    Jan Eden Guest

  2. #2

    Default Re: Method invocation arrow (LPORM)

    On Feb 7, Jan Eden said:
    >I just work my way through "Learning Perl Objects, References & Modules".
    >Now at one point, I am stuck: Randal introduces classes and methods in
    >Chapter 8.
    Hopefully I'll answer before Randal. ;)
    >{ package Mouse
    > ISA = qw{Animal};
    > ...
    > sub speak {
    > my $class = shift;
    > ...
    > Animal::speak($class);
    > ...
    > }
    >}
    I was about to ask why it's written this way, but upon checking the
    source, I see that this is the way the first example on overriding a
    method looks.
    >Since there is a method speak in Mouse, it would override the parent's
    >classes method Animal::speak if the latter were not called explicitly.
    >
    >But, as Randal points out, this forces Perl to look for speak in Animal
    >and nowhere else - without the method invocation arrow, it cannot check
    >ISA for ancestor classes.
    Right. I was about to say "why is it written this way?" but then I saw
    the rest of the examples (and read the rest of your email).
    >So far, I get the point. But then he introduces the following solution:
    >
    >$class->Animal::speak(_);
    >
    >Apart from the fact that _ should be unnecessary here (or did I get
    >something wrong), this should expand to:
    >
    >Mouse::Animal::speak("Mouse");
    No, it does not. If $class is 'Mouse', then

    $class->method(_);

    will try looking for Mouse::method(), and if not, it will look through
    Mouse's ISA for a class that DOES supply method(). BUT HERE, we're using

    $class->OtherClass::method(_);

    which says explicitly to start looking for method() in OtherClass (and if
    it fails there, look in OtherClass's ISA). So

    $class->Animal::speak();

    in your case becomes

    Animal::speak($class);

    except that it becomes that DYNAMICALLY.
    >And when Perl does not find Animal::speak in Mouse, to:
    Ah, here's the confusion. This isn't looking for a method named
    'Animal::speak' in 'Mouse'; it's looking for a method named 'speak' in
    'Animal'.

    Only the right-most part of a Thing::Like::this denotes the name of a
    method.

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]


    Jeff 'Japhy' Pinyan Guest

  3. #3

    Default Re: Method invocation arrow (LPORM)


    Jeff 'japhy' Pinyan wrote:
    >BUT HERE, we're using
    >
    >$class->OtherClass::method(_);
    >
    >which says explicitly to start looking for method() in OtherClass
    >(and if it fails there, look in OtherClass's ISA). So
    >
    >$class->Animal::speak();
    >
    >in your case becomes
    >
    >Animal::speak($class);
    >
    >except that it becomes that DYNAMICALLY.
    I see. So Animal just marks the starting point for the search, and it mightbecome LivingCreature::speak($class) later on if speak is not found in Animal.
    >
    >>And when Perl does not find Animal::speak in Mouse, to:
    >
    >Ah, here's the confusion. This isn't looking for a method named
    >'Animal::speak' in 'Mouse'; it's looking for a method named 'speak'
    >in 'Animal'.
    >
    >Only the right-most part of a Thing::Like::this denotes the name of
    >a method.
    That's exactly the information I was looking for. Thank you, Japhy.

    - Jan
    --
    These are my principles and if you don't like them... well, I have others. - Groucho Marx
    Jan Eden Guest

  4. #4

    Default Re: Method invocation arrow (LPORM)

    On Feb 7, 2004, at 10:02 AM, Jeff 'japhy' Pinyan wrote:
    > On Feb 7, Jan Eden said:
    >
    >> { package Mouse
    >> ISA = qw{Animal};
    >> ...
    >> sub speak {
    >> my $class = shift;
    >> ...
    >> Animal::speak($class);
    >> ...
    >> }
    >> }
    >
    > I was about to ask why it's written this way, but upon checking the
    > source, I see that this is the way the first example on overriding a
    > method looks.
    I was going to ask this too. I'm glad I finished reading the email.

    I'm not familiar with the book though, so I will ask this? Does it
    eventually go on to introduce SUPER::? That's what I would prefer here
    and I just want to make sure you're aware of the option.

    James

    James Edward Gray II Guest

  5. #5

    Default Re: Method invocation arrow (LPORM)

    James Edward Gray II wrote:
    >On Feb 7, 2004, at 10:02 AM, Jeff 'japhy' Pinyan wrote:
    >
    >> On Feb 7, Jan Eden said:
    >>
    >>> { package Mouse
    >>> ISA = qw{Animal};
    >>> ...
    >>> sub speak {
    >>> my $class = shift;
    >>> ...
    >>> Animal::speak($class);
    >>> ...
    >>> }
    >>> }
    >>
    >> I was about to ask why it's written this way, but upon checking the
    >> source, I see that this is the way the first example on overriding a
    >> method looks.
    >
    >I was going to ask this too. I'm glad I finished reading the email.
    >
    >I'm not familiar with the book though, so I will ask this? Does it
    >eventually go on to introduce SUPER::? That's what I would prefer here
    >and I just want to make sure you're aware of the option.
    Yes, SUPER:: is introduced just two paragraphs later. I read on, but the uncertainty about $class->Animal::speak kept bugging me. ;)

    - Jan
    --
    These are my principles and if you don't like them... well, I have others. - Groucho Marx
    Jan Eden Guest

  6. #6

    Default Re: Method invocation arrow (LPORM)

    James Edward Gray II wrote:
    >On Feb 7, 2004, at 10:02 AM, Jeff 'japhy' Pinyan wrote:
    >
    >> On Feb 7, Jan Eden said:
    >>
    >>> { package Mouse
    >>> ISA = qw{Animal};
    >>> ...
    >>> sub speak {
    >>> my $class = shift;
    >>> ...
    >>> Animal::speak($class);
    >>> ...
    >>> }
    >>> }
    >>
    >> I was about to ask why it's written this way, but upon checking the
    >> source, I see that this is the way the first example on overriding a
    >> method looks.
    >
    >I was going to ask this too. I'm glad I finished reading the email.
    >
    >I'm not familiar with the book though, so I will ask this? Does it
    >eventually go on to introduce SUPER::? That's what I would prefer here
    >and I just want to make sure you're aware of the option.
    Yes, SUPER:: is introduced just two paragraphs later. I read on, but the uncertainty about $class->Animal::speak kept bugging me. ;)

    - Jan
    --
    These are my principles and if you don't like them... well, I have others. - Groucho Marx
    Jan Eden Guest

  7. #7

    Default Re: Method invocation arrow (LPORM)

    On Feb 7, 2004, at 2:20 PM, Jan Eden wrote:
    > Yes, SUPER:: is introduced just two paragraphs later. I read on, but
    > the uncertainty about $class->Animal::speak kept bugging me. ;)
    Excellent. I figured that was the case, but I was just making sure.

    James

    James Edward Gray II Guest

  8. #8

    Default Re: Method invocation arrow (LPORM)

    On 2/7/2004 1:23 PM, James Edward Gray II wrote:
    > On Feb 7, 2004, at 10:02 AM, Jeff 'japhy' Pinyan wrote:
    >
    >> On Feb 7, Jan Eden said:
    >>
    >>> { package Mouse
    >>> ISA = qw{Animal};
    >>> ...
    >>> sub speak {
    >>> my $class = shift;
    >>> ...
    >>> Animal::speak($class);
    >>> ...
    >>> }
    >>> }
    >>
    >>
    >> I was about to ask why it's written this way, but upon checking the
    >> source, I see that this is the way the first example on overriding a
    >> method looks.
    >
    >
    > I was going to ask this too. I'm glad I finished reading the email.
    >
    > I'm not familiar with the book though, so I will ask this? Does it
    > eventually go on to introduce SUPER::? That's what I would prefer here
    > and I just want to make sure you're aware of the option.
    Hmm, I think a good exercise would be to write code snippets to
    demonstrate the differences between the four method calls:

    Package::method()
    $class->method()
    $class->Package::method()
    $class->SUPER::method()

    or five if you count:

    method Package ...


    Randy W. Sims Guest

  9. #9

    Default Re: Method invocation arrow (LPORM)

    On Feb 8, 2004, at 6:51 AM, Randy W. Sims wrote:
    > Hmm, I think a good exercise would be to write code snippets to
    > demonstrate the differences between the four method calls:
    >
    > Package::method()
    Is this really a "method call"? I think of it as a package qualified
    subroutine call.
    > $class->method()
    > $class->Package::method()
    > $class->SUPER::method()
    This one should only be used inside an overriden method, I think.
    > or five if you count:
    >
    > method Package ...
    Let's not. <laughs>

    James

    James Edward Gray II Guest

  10. #10

    Default Re: Method invocation arrow (LPORM)

    >>>>> "Jan" == Jan Eden <listsjan-eden.de> writes:

    Jan> Yes, SUPER:: is introduced just two paragraphs later. I read on, but the uncertainty about $class->Animal::speak kept bugging me. ;)

    Thank you for the feedback. I'll note that for a future edition of the book.
    That's also very similar to the way I have it in "perlboot", but nobody
    noted it there yet. :(

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal L. Schwartz Guest

  11. #11

    Default Re: Method invocation arrow (LPORM)

    On Feb 9, Randal L. Schwartz said:
    >>>>>> "Jan" == Jan Eden <listsjan-eden.de> writes:
    >
    >Jan> Yes, SUPER:: is introduced just two paragraphs later. I read on, but the uncertainty about $class->Animal::speak kept bugging me. ;)
    >
    >Thank you for the feedback. I'll note that for a future edition of the book.
    >That's also very similar to the way I have it in "perlboot", but nobody
    >noted it there yet. :(
    I would have, but I didn't think it necessary. When the question was
    asked, I went to the perlboot doc since I figured that was where the
    chapter originated.

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

Similar Threads

  1. AxisFault errors in Web Service Invocation
    By cflenster in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: July 7th, 09:21 PM
  2. Web Service invocation from behind proxy behind firewall
    By Kumarforg in forum ASP.NET Web Services
    Replies: 0
    Last Post: August 3rd, 07:15 AM
  3. Replacement for PerlCOM.Script invocation
    By David Jameson in forum PERL Beginners
    Replies: 0
    Last Post: December 4th, 12:35 AM
  4. Is there any Web Services Invocation Framework for .NET
    By NoSpammingToMePlease@bahnhofbredband.se in forum ASP.NET Web Services
    Replies: 0
    Last Post: October 11th, 03:41 PM
  5. Help with left arrow down & right arrow down
    By Anthony in forum Macromedia Director Lingo
    Replies: 8
    Last Post: September 15th, 09:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139