----- Original Message ----- From: "Dermot" <paik...@googlemail.com>
Newsgroups: perl.beginners
To: "John W. Krahn" <jwkr...@shaw.ca>
Cc: "Perl Beginners" <beginners@perl.org>
Sent: Friday, November 27, 2009 12:29 PM
Subject: Re: a simple question about the line


2009/11/27 John W. Krahn <jwkr...@shaw.ca>:

Hello,


$ echo "12
23
34
45
56
67
78" | perl -lpe'$\=--$|?$,:$/'
1223
3445
5667
78

For the benefit of this Luddite, please explain?
Dp.

--$| toggles between 1 and 0. It is this variable's behavior when (--$| or $|--) $| is initially 0. (NOTE: this behavior doesn't happen with ++$| or $|++).

-l      chomps the line input and, for when --$| == 1, prints with $\ = $,
($, is by default the empty string). When --$| == 0, prints with $\ = $/
        ($/ is by default = "\n").
-p applies 'print()' as the last statement in the implicit 'while( ... )' loop

So. in effect, on the first line in, chomps the line. Then, since $|==1 in the first iteration, prints with a line end equal to $, (by default, the empty string).

In the next line, $| will equal 0, so the print will end with $/ (default value of "\n")

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to