Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Answer: Subroutine args by-value vs. by-ref

by Anonymous Monk
on Aug 24, 2002 at 01:40 UTC ( #192509=categorized answer: print w/replies, xml ) Need Help??

Q&A > subroutines > Subroutine args by-value vs. by-ref - Answer contributed by Anonymous Monk

All your previous experience is confusing you.

Read perldata, perlsyn, perlref and perlsub (if perlsub exists).

There are no pointers in perl, there are only scalar's hashes's and array's, and references. Some might say there are also lists, and they're right.

subroutines get arguments via the magical @_ variable, meaning every time you say Foo(1,2,3), inside of sub Foo, the @_ array will contain 1,2 and 3 as it's value.

That's pass-by-value.

my @Arra = ( 1, 2, 3, 4 ); PassByReference(\@Arra); sub PassByReference { die "@_"; }
Here are some more examples.
use Data::Dumper; DIE(1..10); DIE(1,2,3,4,5,6,,8,9); DIE( [ 1..10 ] ); DIE( { 1..10 } ); my @ARRAY = 1..10; my %HASH = @ARRAY; DIE( @ARRAY ); DIE( %HASH ); DIE( \@ARRAY ); DIE( \%HASH ); DIE( @ARRAY, \%HASH ); DIE( \@ARRAY, \%HASH ); DIE( \@ARRAY, %HASH ); DIE( \&DIE ); ¨ DIE(); sub DIE { print Dumper(@_) };

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[moritz]: [ { a => [ { b => 1 } ] } ]
[choroba]: dereference ne creation
[choroba]: at least in my lingo
[moritz]: .oO( oh temps, oh moritz )
[GotToBTru]: you are correct, choroba, but of course there is no point in doing one without the other
[GotToBTru]: un less you're working on write-only memory
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: not indexing
[thepkd]: getting data out of the ds
[choroba]: it depends. Give more details, please

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2016-12-06 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.