[Edinburgh-pm] perl autosplit

Aaron Crane perl at aaroncrane.co.uk
Fri Jun 1 11:37:26 PDT 2007


asmith9983 at gmail.com writes:
> I was trying to use autosplit to extract fields from a file with the
> fields separated by "|" and suspect I may have found a bug
> 
>   perl  -a -F '|' -ne '{print $F[0];};'
> 
> which replied
> 
>   Can't open perl script "|": No such file or directory

That's saying that where Perl expected your command line to have the name
of a file containing your program, it instead found '|'.  So why did Perl
want your command line to mention the script at that point?

The documentation for -F is in 'perldoc perlrun'; that says:

  -Fpattern
       specifies the pattern to split on if -a is also in effect

That's subtly but distinctly different from the documentation for, say, -e:

  -e commandline
       may be used to enter one line of program

That is, -F insists the autosplit pattern be part of the same command-line
element as the -F itself (unlike -e, where you can optionally put it in a
separate element).  So, if your separator were a colon, you could say

  perl -F: -ane 'print "$F[0]\n"'

However, that's not quite good enough when the separator is a vertical-bar,
because the autosplit pattern is a pattern (regex), not a literal string, so
you need to first backslash any regex metacharacters, and then protect the
result from interpretation by the shell:

  perl -F'\|' -ane 'print "$F[0]\n"'

You can simplify that a little further:

  perl -F'\|' -lane 'print $F[0]'

> awk and cut did the job without needing me to make much effort and less
> typing :-
> 
>   awk -F'|' '{print $1};'
>   cut -d'|' -f 1

It's certainly reasonable to pick the best tool for each job, rather than
forcing everything to be the nail to Perl's hammer.  And, yes, in this
case either of those produces a shorter command.  But you may find in the
future that, as you learn more Perl, it becomes more natural to write such
text-manipulation pipelines with Perl.  For tasks like this, I personally
find Perl easier for all but the simplest tasks.  I suspect I'd use cut in
this case, but something only a little more awkward might well see me
reaching for Perl.  But then, I probably have more experience with Perl
than with anything else.

-- 
Aaron Crane


More information about the Edinburgh-pm mailing list