Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: passing variables between subroutines

by g0n (Priest)
on Jan 16, 2006 at 15:45 UTC ( [id://523525]=note: print w/replies, xml ) Need Help??


in reply to passing variables between subroutines

If your variable is a simple scalar, you can pass it to the sub as follows:

my $return = mailoff($myvar); sub mailoff { my $var = $_[0]; #do stuff return $vartoreturn }

If you want to pass arrays and hashes around, you can pass them as a list:

mailoff(@array); sub mailoff { my @array = @_; } .. mailoff(%hash) sub mailoff { my %hash = @_; }
But a) you can only pass one array or hash that way, and b) it's a copy, not the original, so changes won't be seen in your main program (that's true in the first example as well, hence the return variable).

Alternatively, you could look at perlref, and pass references around. There's some simple introduction to that technique in Why use references?.

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."

John Brunner, "The Shockwave Rider".

Replies are listed 'Best First'.
Re^2: passing variables between subroutines
by Grygonos (Chaplain) on Jan 16, 2006 at 15:51 UTC
    It's best to avoid passing non-scalars around that way. I think you're generally better served by passing the references
    my @test = (1,2); my @test2 = (3,4); foo(\@test,\@test2); sub foo { my ($array,$array2) = @_; print $_ ."\n" for(@{$array}); print $_ ."\n" for(@{$array2}); }
      It's best to avoid passing non-scalars around that way

      I have to say I disagree with that as stated. Certainly, there are many cases where passing a reference is preferable, but it depends on the situation. My default is to pass a simple list and switch to passing references when there's a clear need. I'd even go so far as to say it's best to err on the side of not making references when they're not needed.

        So, can you give me an example of when it would not be a good idea to pass a reference to a non-scalar? I'm asking in earnest. I truly want to know.
      It's best to avoid passing non-scalars around that way. I think you're generally better served by passing the references

      First of all the person you're replying to already pointed out that possibility in his post. Said this, I beg to differ with your claim and to stress that it is actually a possibility, albeit one that -as he also pointed out- turns into a necessity under certain circumstances. But generally speaking there are situations in which passing a reference is preferrable and others in which it is preferrable not to. However, stating a claim like that may easily turn into cargo culting the use of referencing and dereferencing all the time, which has already been observed around.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://523525]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found