On Tue, Oct 14, 2003 at 06:24:07PM -0500, Daniel Staal wrote:
> --On Tuesday, October 14, 2003 15:59 -0700 "[EMAIL PROTECTED]" 
> >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.  So, the 
> function call would become:
> 
> toUpper(\$a);
> 
> The sub would be:
> 
> sub toUpper { return "\U$$_[0]"; }  # (Hey, we have linenoise!)
> 
> In general, this is to be avoided, just because it makes things more 
> complicated than they really need to be (and it breeds linenoise). 

That really is needlessly complicated...

And besides, it doesn't actually modify the argument.  :-)

You could have written:

  sub to_upper { 
    my $ref = shift;
    $$ref = uc $$ref;
  }

But the reference is some kind of red herring.

Probably you were thinking about how you need to pass array and
hash references in order to modify the original array or hash from
inside a subroutine.  But scalars are easy -- you just modify the
elements of @_.

  sub to_upper { $_[0] = uc $_[0] }

-- 
Steve

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

Reply via email to