Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > what is "}{"?

Reply
Thread Tools

what is "}{"?

 
 
zenny lenny
Guest
Posts: n/a
 
      05-02-2007
I DID check the FAQ first.

Two questions:

1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'

What is the purpose of "}{"?

2. Is there any way to search groups or the web for symbols like "}
{"? Google seems to ignore symbols like this.

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-02-2007
On May 2, 6:39 pm, zenny lenny <(E-Mail Removed)> wrote:
> I DID check the FAQ first.
>
> Two questions:
>
> 1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'
>
> What is the purpose of "}{"?


It is the end of one block (executed once per line) and the start of
another block (executed once).

It makes sense if you know what -n actually does.

The above is simply short for:

perl -le ''LINE: while (<>) {$x+=$_}{print $x;}'

> 2. Is there any way to search groups or the web for symbols like "}
> {"?


Not that I know of.

However you could perhaps grep the Perl manual - this trick may be
mentioned in there somewhere.

 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      05-02-2007
On May 2, 7:39 pm, zenny lenny <(E-Mail Removed)> wrote:
> 1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'
> What is the purpose of "}{"?


You are using "-n" in a very clever way indeed:

perldoc perlrun:
>> ====================
>> -n
>> causes Perl to assume the following loop around
>> your program, which makes it iterate over filename
>> arguments somewhat like sed -n or awk:
>>
>> LINE:
>> while (<>) {
>> ... # your program goes here
>> }
>> ====================


....so with '$x+=$_}{print $x;'
your program will be...

LINE:
while (<>) {
$x+=$_}{print $x;
}

....or reformatted...

LINE:
while (<>) {
$x+=$_
}
{ print $x; }

-- Klaus

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-02-2007
On 2 May 2007 11:01:09 -0700, Klaus <(E-Mail Removed)> wrote:

>> 1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'
>> What is the purpose of "}{"?

>
>You are using "-n" in a very clever way indeed:


s/You are/Someone is/; #


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-02-2007
On 2 May 2007 10:39:58 -0700, zenny lenny <(E-Mail Removed)>
wrote:

>Subject: what is "}{"?


news://er8luv$268s$(E-Mail Removed)


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Wade Ward
Guest
Posts: n/a
 
      05-02-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On May 2, 10:39 am, zenny lenny <(E-Mail Removed)> wrote:
>> I DID check the FAQ first.
>>
>> Two questions:
>>
>> 1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'
>>
>> What is the purpose of "}{"?
>>
>> 2. Is there any way to search groups or the web for symbols like "}
>> {"? Google seems to ignore symbols like this.

>
>
>
> Heh, that's intentional obfuscation. Abigail explains it
> here (see the "Counting Lines" section):
>
> http://ucan.foad.org/~abigail/Perl/Talks/Japhs/
>
> The -n switch (see 'perldoc perlrun') wraps your
> code, similar to -p but without the continue and
> print portion.
>
> You can also use a module to make things clearer:
>
> $ perl -MO=Deparse -nle '$x+=$_}{print $x;'
>
> BEGIN { $/ = "\n"; $\ = "\n"; }
> LINE: while (defined($_ = <ARGV>)) {
> chomp $_;
> $x += $_;
> }
> {
> print $x;
> }
> -e syntax OK

Aren't they just curly braces that are guaranteed to be unmatched? A google
search would hit every entry that has two or more sets of curly braces.
--
Wade Ward


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-03-2007
zenny lenny schreef:

> 1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'
>
> What is the purpose of "}{"?


Try -MO=Deparse like this:

perl -MO=Deparse -ne'$x+=$_}{print $x'

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-03-2007
On 03 May 2007 20:50:55 GMT, Abigail <(E-Mail Removed)> wrote:

>== What is the purpose of "}{"?
>==
>
>
>Self reflection. Do you see a vase, or two faces looking at each other?


A yo-yo. With no string.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments