in reply to Dynamically changing the value of a passed variable into a sub

You'll need to pass a reference to the scalar variable as a parameter to the subroutine.

TimeStamp(\$data_created); sub TimeStamp { my ($dateRef) = @_; if ($$dateRef =~ /(\d{4})(\d{2})(\d{2})/) { $$dateRef = "$2/$3/$1"; } else { warn("..."); } }

Since Perl has call-by-value semantics, i.e. a copy of the variable is passed to the subroutine rather than the value itself, you have to pass the address of the variable (i.e. \$date_created) to the subroutine.

In the subroutine, you manipulate the value $$dateRef which is stored at address $dataRef.

Hope this helps, -gjb-

Replies are listed 'Best First'.
Re: Re: Dynamically changing the value of a passed variable into a sub
by Willman023 (Scribe) on Nov 05, 2002 at 13:01 UTC
    Thanks to everyone who helped out with this one, I always had trouble with pointers in C++, and I guess it rubbed off onto references in Perl. I used gjb's code example, and it worked great. But I'm wondering if I could have changed the line my($dateRef) = @_; to my(dateRef) = $_[0];

    Also thanks for pointing out my bad programming with the scope of the $_[0] in my first example.

    bW
    Learning something new everyday!