470,613 Members | 2,245 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

References and subroutines

Hello,

I'm a C programmer who recently came to Perl so this is a little
confusing.

If I have a local variable, how do I pass it to a subroutine as a
reference (like a C pointer) and have the subroutine be able to modify
the contents of the variable? I've looked into references but all I
can find is stuff on passing array and hash references.

Thanks,
Alex
Jul 19 '05 #1
1 1350
ReaprZero wrote:
If I have a local variable, how do I pass it to a subroutine as a
reference (like a C pointer) and have the subroutine be able to
modify the contents of the variable?

my $var = 1;

increment(\$var);

sub increment {
my $ref = shift;
$$ref += 1;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by joe | last post: by
1 post views Thread by Augasm | last post: by
11 posts views Thread by Randy Yates | last post: by
3 posts views Thread by OpticTygre | last post: by
5 posts views Thread by Victor | last post: by
1 post views Thread by Wijaya Edward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.