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. Advertisements

  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. Advertisements

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. Roger Leigh
    Replies:
    8
    Views:
    707
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    704
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    842
    DanielEKFA
    May 16, 2005
  4. Ketema

    References Subroutines and Arrays

    Ketema, Mar 5, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    336
  5. Eric Martin
    Replies:
    16
    Views:
    358
    Eric Martin
    Jan 19, 2006
  6. Rainer Weikusat
    Replies:
    1
    Views:
    241
    Rainer Weikusat
    Jan 4, 2014
  7. Peter J. Holzer
    Replies:
    1
    Views:
    242
    Rainer Weikusat
    Jan 7, 2014
  8. Jürgen Exner
    Replies:
    9
    Views:
    270
    Charlton Wilbur
    Jan 13, 2014
Loading...