References and subroutines

Discussion in 'Perl' started by ReaprZero, Dec 4, 2003.

  1. ReaprZero

    ReaprZero Guest

    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
    ReaprZero, Dec 4, 2003
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Dec 4, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ketema

    References Subroutines and Arrays

    Ketema, Mar 5, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    135
  2. Eric Martin
    Replies:
    16
    Views:
    156
    Eric Martin
    Jan 19, 2006
  3. Rainer Weikusat
    Replies:
    1
    Views:
    122
    Rainer Weikusat
    Jan 4, 2014
  4. Peter J. Holzer
    Replies:
    1
    Views:
    79
    Rainer Weikusat
    Jan 7, 2014
  5. Jürgen Exner
    Replies:
    9
    Views:
    88
    Charlton Wilbur
    Jan 13, 2014
Loading...

Share This Page