Code Comments
Programming Forum and web based access to our favorite programming groups.Greetings: When you invoke a method, the first argument to the method equal to the meth od's invocant is automatically inserted. i) Is this essentially the same as a C++ "this" pointer? ii) Apparently, the first argument can be a reference to its object, or the object. iii) There is an example in the Camel book, in the Object-Oriented-Programmi ng Chapter (ch. 12), where accessor methods are being generated automatical ly; in this example, it seems that the invocant is explicity placed into the first argument. If you pu t in the this pointer, does PERL know not to put in another one? matthew s --------------------------------- Yahoo! Mail Stay connected, organized, and protected. Take the tour
Post Follow-up to this messageMatthew Sacks wrote:
>
> Greetings:
>
> When you invoke a method, the first argument to the method equal to the me
thod's invocant is automatically inserted.
>
Correct.
> i) Is this essentially the same as a C++ "this" pointer?
>
Yes, the Perl idiom generally looks like:
sub method {
my $self = shift;
my @args = @_;
..
}
Where the first line, $self = shift, removes the first argument and
stores it to $self for future access. Then the rest of the passed
argumetns show up in the rest of @_, storing them to another location is
by choice.
perldoc -f shift
> ii) Apparently, the first argument can be a reference to its object, or th
e object.
>
In Perl those are really the same. An object is really just a blessed
reference,
perldoc -f bless
Though there can be the distinction of a class method vs. an object
method, where the first argument won't be a reference/object, but
instead the name of the class. This is basically how a constructor is
written, though any other class method would do the same.
> iii) There is an example in the Camel book, in the Object-Oriented-Programming Cha
pter (ch. 12), where accessor methods are being generated automatically; in this ex
ample, it seems that the invocant is explicity placed into the first argument. If y
ou
put in the this pointer, does PERL know not to put in another one?
>
My camel is still packed so I can't address this specific issue. Perl
automatically adds the first argument whenever a subroutine is called in
method format. It won't check the other arguments to see if they are
references/class names, so for sure it can't tell. Having said that
depending on the method used for accessor generation it might need this
for some reason.
You might also be interested in the OOP docs that come with Perl,
perldoc perl
For a list of those.
http://danconia.org
p.s. Perl is the language, perl is the interpreter, PERL does not
compute ;-)....
Post Follow-up to this messageOn Jun 2, Matthew Sacks said:
> When you invoke a method, the first argument to the method equal to the
>method's invocant is automatically inserted.
That sounds about right. The first argument to a method is either a class
name or the object, depending on whether it's a class method or an object
method:
my $foo = SomeClass->new('blah');
The arguments to the 'new' method are ('SomeClass', 'blah').
$foo->some_method(10, 50);
The arguments to the 'some_method' method are ($foo, 10, 50).
> i) Is this essentially the same as a C++ "this" pointer?
Yes; the first argument to an object method is the object, which is
synonymous to the 'this' pointer in C++.
> ii) Apparently, the first argument can be a reference to its object, or
>the object.
Uh, that sounds wrong. Objects *are* references in Perl, just a special
kind of reference. An object is a reference that has been "blessed" into
a particular package/class/namespace:
package SomeClass;
sub new {
my $pkg = shift;
# $pkg is the class to bless into... it is not
# NECESSARILY SomeClass, because a class could
# be inheriting its new() method from SomeClass
my $obj = bless [@args], $pkg;
return $obj;
}
A reference to an object is not necessarily an object. That is:
$foo->some_method(...);
works, but
my $ref_to_foo = \$foo;
$ref_to_foo->some_method(...);
does NOT work.
> iii) There is an example in the Camel book, in the
>Object-Oriented-Programming Chapter (ch. 12), where accessor methods are
>being generated automatically; in this example, it seems that the
>invocant is explicity placed into the first argument. If you put in the
>this pointer, does PERL know not to put in another one?
I don't have my Camel at hand. Is this an AUTOLOADing issue?
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.