I'd like to a share a small snippet of code I've recently used not realizing that it's actually a small code pattern on its own. I needed to parse strings in form "a b c d" where these a b c d are arbitrary commands,
and each may or may not contain spaces depending on the command. F.ex. phrases "new item x" and "set password a b wwd dc" both contain three words, so simple split wouldn't do. So to parse them correctly, I cut off the first word, analyze it, cut off the next one etc. The part that cuts it off is actually what all this is about. Function "head" that cuts off the first word is very simple:
sub head($)
{
my ($x, $xs) = split( ' ', ${$_[0]}, 2);
${$_[0]} = $xs;
return $x;
}
however its use is interesting, because with a special formatting one may get an impression that \ is a binary operator. Here:
my $msg = "new item x";
my $cmd = head \ $msg;
if ( $cmd eq 'new') {
if ( head \ $msg eq 'item') {
...
}
} elsif ( $cmd eq 'set') {
...
}
where
head \ $msg indeed looks like a binary operation :)