[Perl] split file into odd and even lines

Mikhael Goikhman migo at homemail.com
Sun Oct 6 05:28:42 PDT 2002


On 06 Oct 2002 15:20:31 +0300, Shlomo Yona wrote:
> 
> On Sun, 6 Oct 2002, Mikhael Goikhman wrote:
> 
> >   perl -pe'<>'<a>b
> >   perl -pe'$_=<>'<a>c
> 
> Nice.
> I was surprised by the behaviour of <> with the -p switch.
> It seems that in the first line, the result of <> is ignored by the -p switch
> although it is saved in $_ (or so I though).
> 
> In the 2nd line, it seems that the exmplicit assignment into $_ run-over the
> line read by -p.
> 
> I would love some explenation of how this works.

-p works this way. The input line is passed in $_ and the output line is
read from $_ as well. This makes it easy to filter the input line using
s/// or similar regexps.

So, in the first command above, the input (odd) line is not changed, just
the even line is read and dismissed. In the second line it is vice versa,
the original (odd) line is ignored and replaced with the even one.

See "man perlrun" or "perldoc perlrun" to learn about how "-p" works.

Regards,
Mikhael.



More information about the Perl mailing list