References and subroutines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ReaprZero

    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
  • Gunnar Hjalmarsson

    #2
    Re: References and subroutines

    ReaprZero wrote:[color=blue]
    > 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?[/color]


    my $var = 1;

    increment(\$var );

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

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

    Comment

    Working...