in reply to Confused about typeglobs and references

You're doing something right, using strict to catch unintended global variables. You can say:

use vars qw( @a @b ); # or else for recent perl # our (@a, @b);

Beware *a and *b, you're treading near $a and $b, which are sacred to sort. Expect bizarre bugs in code that uses those names. Your example function is probably ok since it doesnt call sort and localizes the typeglob, but imagine what could happen if some function that sorts were called, and a user fed the function a pair of refs to scalars. It would compile, appear to work, and might even return correct values - for most inputs.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Confused about typeglobs and references
by jdporter (Paladin) on Nov 04, 2002 at 21:57 UTC
    Beware *a and *b, you're treading near $a and $b, which are sacred to sort.

    I think that's a little misleading. $a and $b are used by sort, but otherwise are not special to sort.

    Here's the deal:

    $a and $b are pre-declared package variables.
    That means you can use them -- without declaring them first -- even under strict.

    Of course, as with all global variables, safe programming means localizing.

    So if there's any chance that you've been called from within a sort (unlikely), localize $a and $b before touching them. And if there's any chance that you will call sort while working with $a or $b, be sure to localize them before passing control to where the sort might happen.

Re: Re: Confused about typeglobs and references
by Argel (Prior) on Nov 04, 2002 at 20:18 UTC
    Beware *a and *b, you're treading near $a and $b, which are sacred to sort. Expect bizarre bugs in code that uses those names.
    Wow! I guess somne of the examples in Effective Perl Programming might not be so effective after all! Thanks for the tip! -- Argel