Learning Perl Objects, References amp;amp; Modules [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Learning Perl Objects, References amp;amp; Modules [Electronic resources] - نسخه متنی

Randal L. Schwartz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید














9.3 Accessing the Instance Data


Because you get the instance as the
first parameter, you can now access the instance-specific data. In
this case, let's add a way to get at the name:

{ package Horse;
@ISA = qw(Animal);
sub sound { "neigh" }
sub name {
my $self = shift;
$$self;
}
}

Now you call for the name:

print $tv_horse->name, " says ", $tv_horse->sound, "\n";

Inside Horse::name,
the @_ array contains just
$tv_horse, which the shift
stores into $self. It's
traditional to shift the first parameter into a variable named
$self for instance methods, so stay with that
unless you have strong reasons otherwise. Perl places no significance
on the name $self, however.[5]

[5] If you
come from another OO language background, you might choose
$this or $me for the variable
name, but you'll probably confuse most other Perl
OO-hackers.


Then $self is dereferenced as a scalar reference,
yielding Mr. Ed. The result is:

Mr. Ed says neigh.



/ 199