method call works with non-existent invocant

Discussion in 'Perl Misc' started by Unknown Poster, Jun 16, 2004.

  1. I don't see how this not only compiles under Perl 5.6.1, but the call to
    init() in the Parent class works, with a scalar or bareword that refers
    to nothing. (It fails if there is no invocant at all.) Why is the call
    seen below considered OK by the compiler?

    -------------------

    package Child;

    use base("Parent");
    use strict;
    use warnings;

    sub new
    {
    my $i = shift;
    my $hr = x->SUPER::init(); #or even $x
    my $s = {%$hr, count => 0, label => 'none'};
    return bless $s, ref($i) || $i;
    }

    ..
    ..
    ..
     
    Unknown Poster, Jun 16, 2004
    #1
    1. Advertising

  2. Also sprach Unknown Poster:

    > I don't see how this not only compiles under Perl 5.6.1, but the call to
    > init() in the Parent class works, with a scalar or bareword that refers
    > to nothing. (It fails if there is no invocant at all.) Why is the call
    > seen below considered OK by the compiler?
    >
    > -------------------
    >
    > package Child;
    >
    > use base("Parent");
    > use strict;
    > use warnings;
    >
    > sub new
    > {
    > my $i = shift;
    > my $hr = x->SUPER::init(); #or even $x
    > my $s = {%$hr, count => 0, label => 'none'};
    > return bless $s, ref($i) || $i;
    > }


    First of all, this is not so much an issue of the compiler. Perl's
    compiler just creates an op-tree which wont fail as long as your script
    is syntactically correct. If the above was an error, it would be
    detected by the interpreter at runtime.

    However, the above is totally ok. Using SUPER:: will change Perl's
    notion of method dispatch. Which class' method is invoked now no longer
    depends on the invocant but rather on the first value in @Child::ISA:

    package A;
    sub init { print "@_\n" }
    package B;
    @B::ISA = qw/A/;
    x->SUPER::init(1, 2, 3);
    __END__
    x 1 2 3

    This makes sense as you explicitely asked to call the init() method of
    the superclass. In this case perl wont look at the invocant at all.
    Instead it will eventually translate the above to:

    A::init("x", 1, 2, 3);

    The following example shows more clearly that perl ignores the type of
    invocant when SUPER:: was given:

    package A;
    sub init { print "A::init: @_\n" }

    package B;
    @B::ISA = qw/A/;
    sub init { print "B::init: @_\n" }

    $x = bless { } => __PACKAGE__;
    $x->init(1, 2, 3);
    $x->SUPER::init(1, 2, 3);
    __END__
    B::init: B=HASH(0x8139c84) 1 2 3
    A::init: B=HASH(0x8139c84) 1 2 3

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jun 16, 2004
    #2
    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. Paul W

    Non-existent datagrid?

    Paul W, Nov 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    332
    Paul W
    Nov 10, 2004
  2. ElGordo
    Replies:
    2
    Views:
    6,136
    Ersin Gençtürk
    Apr 29, 2005
  3. metaperl
    Replies:
    6
    Views:
    245
    metaperl
    Sep 12, 2006
  4. Les Caudle
    Replies:
    1
    Views:
    346
    Cowboy \(Gregory A. Beamer\)
    Sep 19, 2006
  5. Wolfgang Nádasi-donner

    Non Existent Method Dir.empty? Description via "ri"

    Wolfgang Nádasi-donner, Aug 12, 2007, in forum: Ruby
    Replies:
    23
    Views:
    302
    Bertram Scharpf
    Aug 16, 2007
Loading...

Share This Page