in reply to Would someone mind helping me understand this Perl OO code?

If you assign a list of 3 values to a list of 4 scalars, the 4th scalar will be undefined.

( True, but irrelevant to the parent post. oops! )

  • Comment on Re: Would someone mind helping me understand this Perl OO code?

Replies are listed 'Best First'.
Re^2: Would someone mind helping me understand this Perl OO code?
by romandas (Pilgrim) on Dec 22, 2009 at 15:59 UTC
    Right. Which is what I meant by the 'one off' error - since $pkg is the first variable in the @_ argument list, it would mistakenly get $main::ip assigned to $Node::pkg, right? So is the code wrong? Like I said, it seems to work though.

      Node->new(...) implicitly passes the package name (Node) as the first argument to the constructor...  (so you have 4 args in the right slots, and everything is fine)

        I'm sure this behavior is documented somewhere, but didn't see it. Do you know where it can be found?

        Since new isn't a reserved word, how does perl know to pass the package name implicitly into that method in the first place? I could have just as badlyeasily named the constructor 'bargle'. Is the package name passed implicitly on every method call? Or just the one where the object is blessed?
      Oops, ignore my earlier post.
      Node->new($ip, $mac_address, $machine);
      is basically the same as
      Node::new('Node', $ip, $mac_address, $machine);
      The main difference is that it checks the parent classes for new if sub Node::new doesn't exist.