in reply to \ as a binary operator

You do realise that $_[0] is an alias to the item passed to the subroutine so you don't need to pass a reference to do the same thing:

$ perl -le' sub head($) { my ($x, $xs) = split( " ", ${$_[0]}, 2); ${$_[0]} = $xs; return $x; } my $msg = "new item x"; my $cmd = head \ $msg; print for $cmd, $msg; ' new item x $ perl -le' sub head { my ($x, $xs) = split( " ", $_[0], 2); $_[0] = $xs; return $x; } my $msg = "new item x"; my $cmd = head $msg; print for $cmd, $msg; ' new item x

Replies are listed 'Best First'.
Re^2: \ as a binary operator
by dk (Chaplain) on Jul 01, 2009 at 23:11 UTC
    You're right. Not only that $_[0] is an alias, and the original implementation is redundant because of it, but also that it could be implemented much more concisely, f.ex. sub head($) { $_[0] =~ s/^(\S+)\s+// and $1 }

    However, with these changes, the very point of the post would be missed. There was a reason that I put it in "obfuscation" - because "head $msg" is trivial and "head \ $msg" is fun :)