in reply to Re: Perl 'grammar'
in thread Perl 'grammar'

why is
my $empl_name = shift; my ($last, $first) = $empl_name=~/^([^,]+), (.+)/ or return undef;
preferred over
my ($last, $first) = /(.*), (.*)/ or return undef;
(or perhaps /(.*),\s*(.*)/ ?)

Replies are listed 'Best First'.
Re^3: Perl 'grammar'
by gamache (Friar) on Jan 09, 2008 at 16:26 UTC
    Hell, why not sub format_name_for_AD {/(.+), (.)/ ? $2.$1 : undef}?
      Good point. You lose the uc bit, but its awfully tight.

      I think I was reacting more to the parameter unpacking. The sub is handed a compound, and the parameter is stored and then broken apart. I tend to think of that breaking as part of the unpacking process, so in essence get two params passed to the sub in a particular format, hence my "first" line produces those two variables.

      It all comes down to chasing errors, I suppose. I've gotten the habit of putting in a print (or warn) at subroutine entry with the parameters passed controlled by a debug variable. With the one-liner, you can't really do that. But then again, its so bloody simple, perhaps its not necessary.

      --woody

        I tend to write my subroutines in three parts: first, a preamble where I throw the passed args into named variables; second, all the guts; and last, the return value. I find it helps with extensibility (but only barely).
Re^3: Perl 'grammar'
by Porculus (Hermit) on Jan 11, 2008 at 21:00 UTC
    The two are not the same. Consider what happens when the sub is called as format_name_for_AD($foo).
      Ah, $_ != $_[0]. I did not think of that.