I don't get what this is de-referencing.

G

grocery_stocker

I have question about line 10 for the following code.

1 #!/usr/bin/perl
2
3 { package Horse;
4 @ISA = qw(Animal);
5 sub sound {"neigh" }
6 sub names {
7 print $_[0] , "\n";
8 $self = shift;
9 #print @_ , "\n";
10 $$self;
11 }
12 sub named {
13 $class = shift;
14 $name = shift;
15 bless \$name, $class;
16 }
17 }
18
19 my $tv_horse = Horse->named("Mr. Ed");
20 print $tv_horse->names, "\n";
21 print Horse->named("Mr. Ed"), "\n";

What is $$self actually de-referencing? I thought it was de-referencing
$name. But when I replace $$self; with $self->{'name'};, I get "Not
HASH reference at ./9-horse.pl line 10."

Chad
 
T

Tassilo v. Parseval

Also sprach grocery_stocker:
I have question about line 10 for the following code.

1 #!/usr/bin/perl
2
3 { package Horse;
4 @ISA = qw(Animal);
5 sub sound {"neigh" }
6 sub names {
7 print $_[0] , "\n";
8 $self = shift;
9 #print @_ , "\n";
10 $$self;
11 }
12 sub named {
13 $class = shift;
14 $name = shift;
15 bless \$name, $class;
16 }
17 }
18
19 my $tv_horse = Horse->named("Mr. Ed");
20 print $tv_horse->names, "\n";
21 print Horse->named("Mr. Ed"), "\n";

What is $$self actually de-referencing? I thought it was de-referencing
$name. But when I replace $$self; with $self->{'name'};, I get "Not
HASH reference at ./9-horse.pl line 10."

$$self denotes that $self is a reference to a plain scalar. Note that in
Horse::named() (your constructor), you do:

bless \$name, $class;

That means you are not blessing a reference to a hash but a reference to
a scalar.

Tassilo
 
P

Paul Lalli

Tassilo v. Parseval said:
Also sprach grocery_stocker:
I have question about line 10 for the following code.

1 #!/usr/bin/perl
2
3 { package Horse;
4 @ISA = qw(Animal);
5 sub sound {"neigh" }
6 sub names {
7 print $_[0] , "\n";
8 $self = shift;
9 #print @_ , "\n";
10 $$self;
11 }
12 sub named {
13 $class = shift;
14 $name = shift;
15 bless \$name, $class;
16 }
17 }
18
19 my $tv_horse = Horse->named("Mr. Ed");
20 print $tv_horse->names, "\n";
21 print Horse->named("Mr. Ed"), "\n";

What is $$self actually de-referencing? I thought it was de-referencing
$name. But when I replace $$self; with $self->{'name'};, I get "Not
HASH reference at ./9-horse.pl line 10."

$$self denotes that $self is a reference to a plain scalar. Note that in
Horse::named() (your constructor), you do:

bless \$name, $class;

That means you are not blessing a reference to a hash but a reference to
a scalar.

If I'm not mistaken, this is code from Randal Schwartz's "Learning Perl
Objects References and Modules". I would encourage the OP to continue
reading. The author introduces classes with this concept of creating a
class object as a reference to a scalar, but quickly moves on to the far
more common practice of using references to hashes instead.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top