Re: What does "}{" mean?

"Craig S. Cottingham" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <1076079356.12985.49.camel@scorpio>
On Fri, 2004-02-06 at 08:45, [email protected] wrote:
> 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?

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
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.