in reply to Global vars?

This is the sort of thing that prototypes are good for. You get to pass variables seemingly by value, but it is really a reference that is passed. That allows the original variable to be modified.

sub mogrify (\$\$\$) { my ($xref, $yref, $zref) = @_; # do stuff to $$xref and friends 1; } mogrify $x, $y, $z;
That will mogrify the variables in place.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Global vars?
by tilly (Archbishop) on Oct 30, 2003 at 16:09 UTC
    Given the existence of virtually any alternative, I would recommend against prototypes.

    What if the person uses & in the function call? What if the person you are giving advice to uses it everywhere and then tries to learn how OO works? What if the person tries to put the variables into an array and then calls your function?

    Prototypes add a lot of gotchas and not a lot of benefits. Therefore I avoid recommending them even when they look like they might make sense. For instance the following is no more obscure, and has fewer potential issues:

    sub mogrify { my ($xref, $yref, $zref) = \@_[0..2]; # do stuff to $$xref and friends 1; }