Daniel Staal wrote:
> 
> --On Tuesday, October 14, 2003 15:59 -0700 "[EMAIL PROTECTED]"
> <[EMAIL PROTECTED]> wrote:
> >
> > Also in a slightly different scenario, how can i change the value
> > of the parameter itself with a sub.
> >
> > $a="ca";
> > toUpper($a);  #change the $a value itself
> > print $a;      #I want it to print "CA"
> 
> Ok, I'll assume this is a made-up example...  (Since it would just be
> easy to write 'print toUpper($a);', using the return value.)
> 
> The way to do this would be to pass references to the parameters.
> You would have to pass the references, and work with them.

There is no need to use references because the values of @_ are aliased
to the variables passed to the sub so modifying the values in @_ will
also modify the values of the variables.

> So, the function call would become:
> 
> toUpper(\$a);
> 
> The sub would be:
> 
> sub toUpper { return "\U$$_[0]"; }  # (Hey, we have linenoise!)

That still doesn't modify the value in $a.  You would have to write it
like:

sub toUpper { return $$_[0] = "\U$$_[0]"; }

Or:

sub toUpper { return $_->[0] = "\U$_->[0]"; }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to