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

Discussion in 'Perl Misc' started by grocery_stocker, Jan 10, 2005.

  1. 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
     
    grocery_stocker, Jan 10, 2005
    #1
    1. Advertising

  2. 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jan 10, 2005
    #2
    1. Advertising

  3. grocery_stocker

    Paul Lalli Guest

    "Tassilo v. Parseval" <> wrote in
    message news:...
    > 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
     
    Paul Lalli, Jan 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DP
    Replies:
    0
    Views:
    1,170
  2. James McGuigan
    Replies:
    6
    Views:
    639
    James McGuigan
    Jul 21, 2003
  3. Olivier Feys

    get objects referencing another one

    Olivier Feys, Feb 5, 2007, in forum: Python
    Replies:
    2
    Views:
    298
    Bruno Desthuilliers
    Feb 5, 2007
  4. jalkadir
    Replies:
    2
    Views:
    401
  5. Jason
    Replies:
    0
    Views:
    221
    Jason
    Jul 6, 2004
Loading...

Share This Page