SPUG: itm 60 in effective perl programming

Yitzchak Scott-Thoennes sthoenna at efn.org
Thu Feb 24 01:57:47 PST 2005

On Thu, Feb 24, 2005 at 12:02:38AM -0800, Tim Maher wrote:
> On Wed, Feb 23, 2005 at 11:46:28PM -0800, Florentin Ionescu wrote:
> > perl -pe 's/\n/" " . <>/e' file
> > is presented in Effective perl programming as program to
> > join lines from a file. what does it mean <> in this context
> > ? - I looked into perlop and perlre but can't figure out haw
> > it works.
> Hi Florentin,
> <> is the "input operator" -- it's like the UNIX shell's "read"
> command, except it loads $_ with the next line of input rather
> than some other variable.  
> But IMHO, that's a pretty inscrutable way to join lines.  Here's a more
> "scrutable" way, based on the implicit loop (-p), assuming you really
> want a space between each pair of lines as shown above:
> perl -wpe 's/\n$/ /;' file	# replace newline with space

I don't have the book, so can't check what the <> version is
*supposed* to do, but what it *does* do is join every pair of lines
into one (and presumably the file is known to have an even number of
lines), while the proposed improvement makes one line out of the whole

Anyway, surely you meant:

perl -wpe'y/\n/ /' file

More information about the spug-list mailing list