Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What does "}{" mean?

6 views
Skip to first unread message

ky...@panix.com

unread,
Feb 6, 2004, 9:45:43 AM2/6/04
to f...@perl.org

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

Greg Allen

unread,
Feb 6, 2004, 9:51:57 AM2/6/04
to ky...@panix.com, f...@perl.org
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

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.

Stefan Stiasny

unread,
Feb 6, 2004, 9:52:41 AM2/6/04
to f...@perl.org, ky...@panix.com

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.

Craig S. Cottingham

unread,
Feb 6, 2004, 9:55:55 AM2/6/04
to f...@perl.org

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 R. Allen

unread,
Feb 6, 2004, 9:51:16 AM2/6/04
to ky...@panix.com, f...@perl.org
I am appalled that that works. I thought perlrun was being figurative
when it described -p, but it seems not so.

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

Ian Phillipps

unread,
Feb 6, 2004, 9:55:12 AM2/6/04
to ky...@panix.com, f...@perl.org

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

ky...@panix.com

unread,
Feb 6, 2004, 10:37:01 AM2/6/04
to greg....@lehman.com, f...@perl.org

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 Allen

unread,
Feb 6, 2004, 10:40:57 AM2/6/04
to ky...@panix.com, f...@perl.org
Empty body to the while loop. In the spirit of perl, do what I mean, i.e.
nothing. It would appear that any block is allowed a continue clause.

Greg.

PS. I'd be appalled if this didn't work.

-----Original Message-----
From: ky...@panix.com [mailto:ky...@panix.com]

^^
??

ky...@panix.com

unread,
Feb 6, 2004, 12:22:39 PM2/6/04
to f...@perl.org


From: "Allen, Greg" <greg....@lehman.com>

It would appear that any block is allowed a continue clause.


That's what threw me off.

kj

Uri Guttman

unread,
Feb 6, 2004, 1:41:06 PM2/6/04
to ky...@panix.com, greg....@lehman.com, f...@perl.org
>>>>> "K" == <ky...@panix.com> writes:

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

Ton Hospel

unread,
Feb 6, 2004, 4:46:50 PM2/6/04
to f...@perl.org
See also:
echo | perl -lpe 'print q'

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

Leo

unread,
Feb 6, 2004, 5:56:16 PM2/6/04
to f...@perl.org
On Fri, Feb 06, 2004 at 09:46:50PM +0000, Ton Hospel scripsit:

> See also:
> 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????


> Golfers also use the fact that in a -p/-n program the next character
> is a ;, for example

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.


Jonathan Weed

unread,
Feb 6, 2004, 7:13:54 PM2/6/04
to f...@perl.org
> What does the construct "}{" mean?

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.


--
http://www.justicepoetic.net/

Matthew Byng-Maddick

unread,
Feb 7, 2004, 8:46:21 AM2/7/04
to f...@perl.org
On Fri, Feb 06, 2004 at 11:56:16PM +0100, Leo wrote:
> On Fri, Feb 06, 2004 at 09:46:50PM +0000, Ton Hospel scripsit:
> > See also:
> > 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????

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/

Peter Makholm

unread,
Feb 7, 2004, 8:47:04 AM2/7/04
to f...@perl.org
Leo <theh...@altern.org> writes:

> > 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

Michael W Thelen

unread,
Feb 7, 2004, 4:35:58 PM2/7/04
to f...@perl.org
* Leo <theh...@altern.org> [2004-02-07 13:22]:

> > Golfers also use the fact that in a -p/-n program the next character
> > is a ;, for example
>
> What do you mean by 'the next character is a ';''?

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

0 new messages