in reply to undef a List of Variables

It's easy to define a function that works like undef but allows multiple arguments. For example:

sub undeff { @_[0..@_-1] = (); }; $foo = 5; $bar = 8; undeff($foo, $bar); # this undefs both of them print "foo=[$foo] bar=[$bar]\n"; # prints `foo=[] bar=[]' and two warn +ings

Replies are listed 'Best First'.
Re^2: undef a List of Variables
by dragonchild (Archbishop) on Jan 31, 2005 at 17:31 UTC
    sub undeff { @_[0..-1] = (); };

    -1 is the last index ... much easier to read. (And faster, too.)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Yes, that works in most other languages. In ruby, arr[0..-1] returns all elements of the array (and is an lvalue too). In python, arr[0:-1] is all but the last element, arr[0:] or arr[:] is all elements. In octave, you don't have negative array indexes, but you have avec(:) for all of the vector, but also no special notation for all the elements starting the n'th (like arr[n:] in python). In mathematica, you have arr[[All]] for all elements of the array, but also no starting slice notation. (These are only a few languages of course.)

      Anyway, you can write 0..$#_ or even 0..@_ as the index, or avoid indexing completely and say sub undeff { undef $_ for @_ }.

      But it does not work.
      0..-1 is zero elements.