What kind of variables? If just plain out scalars (number/string), then you have to pass a reference. Same for an array or hash. If you have an array ref or hash ref (or object), it's already a reference.
use Data::Dumper;
foo();
sub foo {
my $x = 3;
my @y = ( 1 .. 3 );
my $yy = [ 1 .. 3 ];
my %z = ( a => 1, b => 2 );
my $zz = { a => 1, b => 2 };
my $obj = My::Obj->new; # e.g. bless'd hashref
print Dumper [ $x, \@y, $yy, \%z, $zz, $obj ];
bar( \$x, \@y, $yy, \%z, $zz, $obj );
print Dumper [ $x, \@y, $yy, \%z, $zz, $obj ];
}
sub bar {
my @args = @_;
${$args[0]} += 2; # add 2 to x
$args[1]->[1]++; # 2->3 in y
$args[2]->[1]++; # 2->3 in yy
$args[3]->{a}++; # 1 -> 2 in z
$args[4]->{a}++; # 1 -> 2 in zz
}
You can also do this:
$x = 4;
warn $x;
blah($x);
warn $x;
sub blah {
$_[0] += 3;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.