return value

F

Feyruz

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
 
A

A. Sinan Unur

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top