Why does the following statement work ?

Discussion in 'Perl Misc' started by toralf, Nov 11, 2008.

  1. toralf

    toralf Guest

    I'm asking , b/c the 2nd "}" is not matched and the 2nd "{" too :

    echo -e "A\nB\nA\n" |\
    perl -wne ' $h{$_}++; }; foreach $s (keys %h) { print "$h{$s}\t$s" '

    --
    MfG/Sincerely

    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
    toralf, Nov 11, 2008
    #1
    1. Advertising

  2. toralf <> writes:

    > I'm asking , b/c the 2nd "}" is not matched and the 2nd "{" too :
    >
    > echo -e "A\nB\nA\n" |\
    > perl -wne ' $h{$_}++; }; foreach $s (keys %h) { print "$h{$s}\t$s" '


    Because of the wrapping implied by the -n switch. See perldoc perlrun
    or use B::Deparse:

    $ perl -MO=Deparse -wne ' $h{$_}++; }; foreach $s (keys %h) { print \
    "$h{$s}\t$s" '
    BEGIN { $^W = 1; }
    LINE: while (defined($_ = <ARGV>)) {
    ++$h{$_};
    }
    foreach $s (keys %h) {
    print "$h{$s}\t$s";
    }
    -e syntax OK


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Nov 11, 2008
    #2
    1. Advertising

  3. toralf

    Guest

    toralf <> wrote:
    > I'm asking , b/c the 2nd "}" is not matched and the 2nd "{" too :
    >
    > echo -e "A\nB\nA\n" |\
    > perl -wne ' $h{$_}++; }; foreach $s (keys %h) { print "$h{$s}\t$s" '


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

    And it means this quite literally. "Your program" is not something that
    gets executed as the block inside the while loop. Is it text that gets
    wrapped up in the implicit text and compiled. So "your program" is
    perfectly free to use a "}" to close the implicitly opened "while (<>) {",
    but of course that means it needs to provide a unbalanced "{" to for the
    implicit "}" to close.

    I tend not to use this trick, and would usually write it more like:

    perl -wne ' $h{$_}++; END {foreach $s(keys %h){print "$h{$s}\t$s"}}'


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Nov 11, 2008
    #3
  4. toralf

    toralf Guest

    wrote:


    > I tend not to use this trick, and would usually write it more like:
    >
    > perl -wne ' $h{$_}++; END {foreach $s(keys %h){print "$h{$s}\t$s"}}'
    >
    >
    > Xho
    >


    Thx for both answers - B::Deparse I should really give a try - and the
    "END" statement of @xhoster looks like what I'll use in future to
    clarify such things.


    --
    MfG/Sincerely

    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
    toralf, Nov 12, 2008
    #4
    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.
Similar Threads
  1. Petterson Mikael

    test of following-sibling[2] does not work

    Petterson Mikael, Feb 6, 2006, in forum: XML
    Replies:
    1
    Views:
    839
    George Bina
    Feb 6, 2006
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    452
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  5. Chad
    Replies:
    12
    Views:
    635
Loading...

Share This Page