References and subroutines

R

ReaprZero

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
 
G

Gunnar Hjalmarsson

ReaprZero said:
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;
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top