return value

Discussion in 'Perl Misc' started by Feyruz, Oct 7, 2005.

  1. Feyruz

    Feyruz Guest

    Hello,

    i have a question about a return value in the following code (this code
    is taken from a class:


    sub TIESCALAR
    {
    my $class = shift;
    $data =shift;
    return bless \$data, $class;
    }

    What i dont understand is, why do this method return a \$data and not
    $data (without reference backslash). Isn't $data already a reference?
    is this a reference to a reference or? a little bit confused in that
    point.

    For example when we define a class, we build the constructor this way:

    sub new
    {
    my self ={};
    bless($self);
    return $self;
    }

    and here at the end we write "return $self" and NOT "return \$self".
    Why is this different?

    thanks for your comments and help.
    Feyruz
     
    Feyruz, Oct 7, 2005
    #1
    1. Advertising

  2. "Feyruz" <> wrote in
    news::

    > sub TIESCALAR
    > {
    > my $class = shift;
    > $data =shift;
    > return bless \$data, $class;
    > }
    >
    > What i dont understand is, why do this method return a \$data and not
    > $data (without reference backslash).


    Read about TIESCALAR in perldoc perltie.

    > Isn't $data already a reference?


    No, $data is the variable to be 'tie'd.

    > sub new
    > {
    > my self ={};


    Post real code.

    > bless($self);
    > return $self;
    > }


    sub new {
    my $class = shift;
    bless { }, $class;
    }

    > and here at the end we write "return $self" and NOT "return \$self".
    > Why is this different?


    Because you explicitly created $self to be a reference to an anonymous
    hash.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 7, 2005
    #2
    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. PvdK
    Replies:
    0
    Views:
    3,002
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    521
    Richard Bos
    Jun 18, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    883
    Keith Thompson
    Mar 6, 2005
  4. CSINVA
    Replies:
    6
    Views:
    422
    Paul Delcogliano
    Nov 7, 2007
  5. Tim Fröglich

    Getting ID, calling url, search for value, return value

    Tim Fröglich, Jan 7, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    154
    Josh Twist
    Jan 10, 2006
Loading...

Share This Page