Good days and bad

ziggy on 2002-04-30T21:02:05

Ever have one of those days when your fingers just can't seem to find the 'f' in shift? Or can't spell shift properly until the second or third time (each of three lines in a row)? [*]

Or maybe you've had one of those days when you wonder why the textarea in your browser window doesn't respect vi or emacs keybindings. It's not that you don't know that browsers are being a neutral party in the Editor Wars, but, dammit, why can't just do something more sensible than close the current window when you hit ^W?

[*] Perl6 RFC #1138: Replace the shift builtin with something that does the exact same thing (but does it in fewer characters), or just allow variables to be bound in the sub declaration already!


shift

vsergu on 2002-04-30T21:14:24

You could always abandon the $arg = shift idiom and use the golfish $arg = pop for one-argument subs (though golfers really only use pop for command-line arguments). Or just use @_ all the time (but that does require the shift key).

Re:shift

jdavidb on 2002-04-30T21:54:28

I nearly always use @_. When I learned Perl, I had too much confusion over prototypes (people shouldn't even see those until they've been in Perl for three years), locals, scoping, and so on that I finally just learned to start every sub with:

my($arg1, $arg2, @final_args) = @_;

That seemed to be the only way to make sure I never got confused. My two primary exceptions are the beginning of methods (my $self = shift; followed by the line above) and the extraordinarily rare cases where I have to return a changed value in a parameter. Once I put the values into named variables, I don't touch @_ again in any way for the duration.

When helping beginners, I tell them to use my cookie-cutter parameter line and not vary it until they know what they are doing. The ones that don't listen always burn themselves.

shift typoes

TorgoX on 2002-05-01T22:04:20

My favorite, from my QWERTY and MSDOS days: dickcopy