SPUG: itm 60 in effective perl programming

Tim Maher tim at consultix-inc.com
Thu Feb 24 00:02:38 PST 2005

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

This is like a UNIX "sed" command, and it means "replace the newline
at the end of the current line with a space, print it, and then
repeat until all lines have been processed."

| Tim Maher, PhD     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
|      Watch for my upcoming book: "Minimal Perl for UNIX/Linux People"    |

More information about the spug-list mailing list