Functions return value

A

a22catcher

Hi,
I have the following function, that returns a reference to a local (my)
variable:

sub getSomething {
my %something = ();
...
...
return \%something;
}

Is it valid? (when I checked the return value from the outside, it
seemed right,
but it seems wrong to return a reference to a local variable - or is it
just C thinking?).
What's the prefered way to do something like this?

Thanks a lot
 
L

Lukas Mai

Hi,
I have the following function, that returns a reference to a local
(my) variable:

sub getSomething {
my %something = ();
...
...
return \%something;
}

Is it valid? (when I checked the return value from the outside, it
seemed right, but it seems wrong to return a reference to a local
variable - or is it just C thinking?). What's the prefered way to do
something like this?

It is valid. The name "%something" disappears when you leave the
function, but the actual variable stays in memory as long as there is a
reference to it.

Oh, and "my" creates a new variable each time it is executed, so all of
this pretty much Just Works(tm).

HTH, Lukas
 
B

Bart Lateur

I have the following function, that returns a reference to a local (my)
variable:

sub getSomething {
my %something = ();
...
...
return \%something;
}

Is it valid?

It is valid.
(when I checked the return value from the outside, it
seemed right,
but it seems wrong to return a reference to a local variable - or is it
just C thinking?).

It's just C thinking.

That's the difference between a fast, low level language like C, and a
slower, higher level language like Perl. You get a much smarter
environment in Perl, at the price of speed.
 
A

a22catcher

thanks everybody for the replys.
and another question:
What would happend, If the functioned returned the parameter itself.

i.e

sub getSomething {
my %something = ();
...
...
return %something;
}

Would it make a copy of something? and if so, will it be a deep copy or
shalow one?
thanks
 
A

Anno Siegel

thanks everybody for the replys.

Quote some context, please. Without going back over the thread, most
people will have no idea what you are talking about.
and another question:
What would happend, If the functioned returned the parameter itself.

i.e

sub getSomething {
my %something = ();
...
...
return %something;
}

Would it make a copy of something? and if so, will it be a deep copy or
shalow one?

It returns what a hash evaluates to in scalar or list context, depending
on how the sub is called. (perldoc perldata).

Anno
 
D

David Squire

thanks everybody for the replys.
and another question:
What would happend, If the functioned returned the parameter itself.

i.e

sub getSomething {
my %something = ();
...
...
return %something;
}

Would it make a copy of something? and if so, will it be a deep copy or
shalow one?

RTFM: perldoc -q return value
 
T

Tad McClellan

David Squire said:


That's good advice...

perldoc -q return value


.... but that is not.

I don't see that any of the 8 questions it shows addresses
the OP's question.

RTFM is less than helpful when it does not come with which FM to read.


The right FM to read when you have questions about subroutines is:

perldoc perlsub
 
D

David Squire

Tad said:
That's good advice...




... but that is not.

I don't see that any of the 8 questions it shows addresses
the OP's question.


Hmmm. I *did* test this before posting :)

The first question I see addressed is "Is it safe to return a reference
to local or lexical data?", which answers the OP's first question (in a
preceding post, and makes a start on this one). The fourth is "How can I
pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?",
which hints at the answer to this question (in part), and points to perlsub.

One of my gripes with the commonly seen advice of the form "perldoc
perlsub" is that it does not indicate to newbies that there is a way in
without knowing that the section "perlsub" exists. I think it is useful
to show people the FAQKeyword form too. Helps with bootstrapping.

DS
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top