what is "}{"?

Discussion in 'Perl Misc' started by zenny lenny, May 2, 2007.

  1. zenny lenny

    zenny lenny Guest

    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.
     
    zenny lenny, May 2, 2007
    #1
    1. Advertising

  2. On May 2, 6:39 pm, zenny lenny <> 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.
     
    Brian McCauley, May 2, 2007
    #2
    1. Advertising

  3. zenny lenny

    Klaus Guest

    On May 2, 7:39 pm, zenny lenny <> 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
     
    Klaus, May 2, 2007
    #3
  4. On 2 May 2007 11:01:09 -0700, Klaus <> 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,
     
    Michele Dondi, May 2, 2007
    #4
  5. On 2 May 2007 10:39:58 -0700, zenny lenny <>
    wrote:

    >Subject: what is "}{"?


    news://er8luv$268s$


    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,
     
    Michele Dondi, May 2, 2007
    #5
  6. zenny lenny

    Wade Ward Guest

    <> wrote in message
    news:...
    > On May 2, 10:39 am, zenny lenny <> 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
     
    Wade Ward, May 2, 2007
    #6
  7. zenny lenny

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, May 3, 2007
    #7
  8. On 03 May 2007 20:50:55 GMT, Abigail <> 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,
     
    Michele Dondi, May 3, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page