What does the construct "}{" mean? As in
$ perl -pe ' } { $_="foo\n"' /dev/null
foo
I figure it has to do with how the -p switch affects the script that
is passed to the interpreter. Is this documented anywhere?
kj
This might reveal what is happening:
perl5 -MO=Deparse -pe ' } { $_="foo\n"' /dev/null
LINE: while (defined($_ = <ARGV>)) {
;
}
{
$_ = "foo\n";
}
continue {
print $_;
}
-e syntax OK
Greg
kj
------------------------------------------------------------------------------
This message is intended only for the personal and confidential use of the
designated recipient(s) named above. If you are not the intended recipient of
this message you are hereby notified that any review, dissemination,
distribution or copying of this message is strictly prohibited. This
communication is for information purposes only and should not be regarded as
an offer to sell or as a solicitation of an offer to buy any financial
product, an official confirmation of any transaction, or as an official
statement of Lehman Brothers. Email transmission cannot be guaranteed to be
secure or error-free. Therefore, we do not represent that this information is
complete or accurate and it should not be relied upon as such. All
information is subject to change without notice.
from the perlrun manpage:
-p causes Perl to assume the following loop around your
program, which makes it iterate over filename argu
ments somewhat like sed:
LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}
cheers
sc
--
stefan stiasny <s...@gimp.org>
In an evolving universe, who stands still moves backwards.
perldoc perlrun:
-n causes Perl to assume the following loop around your
program, which makes it iterate over filename argu-
ments somewhat like sed -n or awk:
LINE:
while (<>) {
... # your program goes here
}
Note that the lines are not printed by default. See
-p to have lines printed.
The docs aren't speaking metaphorically; the while construct is
*literally* assembled around your code. So the example you gave above
results in:
LINE:
while (<>) {
} { $_="foo\n"
}
I think this might be documented more explicitly in perlfaq somewhere.
For a practical example, try this:
perl -pe '}{print "$.\n"' .bashrc
(Substitute another filename for ".bashrc" if appropriate.)
--
Craig S. Cottingham
cr...@cottingham.net
daniel@D:~/work$ perl -MO=Deparse -pe ' } { $_="foo\n"' /dev/null
LINE: while (defined($_ = <ARGV>)) {
();
}
{
$_ = "foo\n";
}
continue {
print $_;
}
-e syntax OK
daniel@D:~/work$ perl -MO=Deparse -pe '$_="foo\n"' /dev/null
LINE: while (defined($_ = <ARGV>)) {
$_ = "foo\n";
}
continue {
print $_;
}
-e syntax OK
Yes, in perlrun under "-p" (And -n)
Where it says that your program is surrounded by a loop, take that
absolutely literally. Write out your program inside the loop, and the
meaning of '}{' will become apparent - if not necesssarily clear :-)
Ian
X-Original-To: ky...@panix.com
Mailing-List: contact fwp-...@perl.org; run by ezmlm
From: "Allen, Greg" <greg....@lehman.com>
Date: Fri, 6 Feb 2004 14:51:57 -0000
X-WSS-ID: 6C3D75871563186-01-01
X-BigFish: v
X-SMTPD: qpsmtpd/0.26, http://develooper.com/code/qpsmtpd/
It is the secret "eskimo greeting" operator.
This might reveal what is happening:
perl5 -MO=Deparse -pe ' } { $_="foo\n"' /dev/null
LINE: while (defined($_ = <ARGV>)) {
;
}
{
$_ = "foo\n";
}
continue {
print $_;
}
-e syntax OK
I figured that much, but then I'm left with interpreting a construct
of the form:
while(defined($_ = <ARGV>)){}{$_ = "foo\n"}continue{print $_}
^^
??
I didn't think such a construct would be legal. I'm not sure what it
means.
Greg.
PS. I'd be appalled if this didn't work.
-----Original Message-----
From: ky...@panix.com [mailto:ky...@panix.com]
^^
??
From: "Allen, Greg" <greg....@lehman.com>
It would appear that any block is allowed a continue clause.
That's what threw me off.
kj
K> I figured that much, but then I'm left with interpreting a construct
K> of the form:
K> while(defined($_ = <ARGV>)){}{$_ = "foo\n"}continue{print $_}
K> ^^
K> ??
K> I didn't think such a construct would be legal. I'm not sure what it
K> means.
you have it backwards from the string in the subject. replace that with
}{ and it works. see all the other responses.
uri
--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
which outputs:
}continue{print or die qq(-p destination: $!\n)
Golfers also use the fact that in a -p/-n program the next character
is a ;, for example
#!perl -alp0
s!.+!$;[@$z{$&,$_}=%$z]=$z=$_.$&for@F,@F!eg;$_=pop@
as solution to
http://terje2.perlgolf.org/~pgas/score.pl?func=rules&hole=53&season=1
What do you mean by 'the next character is a ';''?
> #!perl -alp0
> s!.+!$;[@$z{$&,$_}=%$z]=$z=$_.$&for@F,@F!eg;$_=pop@
>
Wonderfull, I do not understant _how_ it works (I didn't even try right now,
is almost midnight here and I'm quite tired....) but I realy like it (I like
golf basically:-) )
Leo
--
Leo "TheHobbit"
One Ring to rule them all, One Ring to find them,
One Ring to bring them all and in the darknes bind them
In the land of Mordor where the Shadow lie.
From perlrun:
"-p causes Perl to assume the following loop around your program, which
makes it iterate over filename arguments somewhat like sed:
LINE:
while (<>) {
...# your program goes here
} continue {
print or die "-p destination: $!\n";
}
"
The closing brace indicates that the while loop is over, and the opening
brace starts a new, bare block to which the "continue" is attached.
LINE:while(<>){print q;}continue{print or die qq(-p destination: $!\n);}
^^^^^^^
Does that help?
MBM
--
Matthew Byng-Maddick <m...@colondot.net> http://colondot.net/
> > echo | perl -lpe 'print q'
> >
> > which outputs:
> >
> > }continue{print or die qq(-p destination: $!\n)
> >
> Well, I had no problem with the thread.... until now! How the f... does this
> work????
It gets converted to:
LINE: while (defined($_ = <ARGV>)) {
print q;}continue {die "-p destination: $!\n" unless print $_;
}
';' is a legal character to use with the quote-like operators. So
q;something; is the same as 'something'. So the manual-page shouldnt
be taken too literal either.
--
Peter Makholm | Sit back and watch the messages. This is actually
pe...@makholm.net | more important than one might think as there is a
http://hacking.dk | bug in GNU Mach whereby hitting a key during the
| boot process causes the kernel to panic
| -- GNU Hurd Installation Guide
As some others have pointed out, not only is the loop literally constructed
around your code, but a semicolon is also inserted. That is why the "print q"
trick works, and it's also why you can omit the final semicolon of @; at the
end of Ton's example program:
> > #!perl -alp0
> > s!.+!$;[@$z{$&,$_}=%$z]=$z=$_.$&for@F,@F!eg;$_=pop@
-- Mike
--
Michael W. Thelen
A satirist is a man who discovers unpleasant things about himself and then
says them about other people.
-- Peter McArthur