Re: What does the SHIFT bit do in a constructor
by arhuman (Vicar) on May 25, 2001 at 16:35 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
|
Note that many people who have used OO programming for a very long time (starting with merlyn who has been using it since the early 80's) detest the use of ref there. Object methods and instance methods are different, no need to confuse them. That said I would write the above as:
sub new {
my $self = bless {}, shift;
@$self{'PEERS', 'NAME', 'AGE'} = [];
return $self;
}
which looks cleaner to my eyes. YMMV. | [reply] [Watch: Dir/Any] [d/l] |
|
In the abstract, I understand and somewhat agree with this
criticism. But when you get into a specific object
implementation, it often makes a great deal of sense.
It comes down to whether the reader will clearly understand
what:
my $other= $object->new();
is meant to do. I find that for many specific object
implementations, this is a quite natural idiom. So I agree
that you should think about what that idiom is
supposed to mean in your specific class before you decide
either way!
A similar criticism is that you should not
name your constructors "new" either. Again, I agree with
this in the abstract but find that when I get down to real
cases, the criticism usually doesn't apply. For many
objects, trying to find a more descriptive name for the
constructor that describes how I want to go about
creating the object just adds confusion.
-
tye
(but my friends call me "Tye")
| [reply] [Watch: Dir/Any] [d/l] |
Re: What does the SHIFT bit do in a constructor
by davorg (Chancellor) on May 25, 2001 at 16:38 UTC
|
When you call a class method (for example
my $obj = MYObject->new), the class name is
automatically passed in as the first element of @_.
A similar thing happens when you call object methods, the
object is passed in as the first element of @_.
For more details see perlboot,
perltoot, perlobj and Damian Conway's
book Object Oriented Perl.
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] [Watch: Dir/Any] |
Re: What does the SHIFT bit do in a constructor
by japhy (Canon) on May 25, 2001 at 16:37 UTC
|
Constructors are usually called like so:
$obj = Class->new(...);
# or
$obj = new Class ...;
Either way, assuming there's no funny inheritance to deal with, you're doing the equivalent of
$obj = Class::new('Class', ...);
The same goes for when you're doing object calls (like $obj->method(...) or method $obj ...). Thus, the class name (or object) is the first argument to the function.
japhy --
Perl and Regex Hacker | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: What does the SHIFT bit do in a constructor
by Vynce (Friar) on May 25, 2001 at 17:01 UTC
|
the one thing i don't think anybody mentioned yet explicitly is that shift shifts @_ by default.
combine this with the fact that the OO syntax (Foo::Bar->new or new Foo::Bar) automatically puts the classname at the front of the argument list, and you see what's going on.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: What does the SHIFT bit do in a constructor
by jorg (Friar) on May 25, 2001 at 17:03 UTC
|
Leaving all this class constructor stuff alone :
if you use shift without an array as argument it will shift from @_ (which is the array of arguments passed to a sub) when called in a sub or from @ARGV (which is the array of arguments passed to the perl program) when called from the main program body.
Thus :
myshift("testme");
sub myshift{
print shift;
#same result
print shift @_;
}
Jorg
"Do or do not, there is no try" -- Yoda | [reply] [Watch: Dir/Any] [d/l] |
Re: What does the SHIFT bit do in a constructor
by diarmuid (Beadle) on May 25, 2001 at 16:51 UTC
|
Well that looks like a line you seen in alot of subroutines eg
sub do_something {
my $classname = shift;
....
}
do_somthing($something);
All it does is take the first element off the array @_ (which in this case contains the
elements of the array passed to the subroutine) and puts
that value into $classname
From the perldoc
See also `unshift', `push', and `pop'. `Shift()'
and `unshift' do the same thing to the left end of
an array that `pop' and `push' do to the right end.
Diarmuid | [reply] [Watch: Dir/Any] [d/l] |
|
Sorry, I just spotted that you mentioned "in a constructor",
well it's a similar idea. The guys above have explained it well.
Diarmuid
| [reply] [Watch: Dir/Any] |