logo       

Re: [Boston.pm] meaning of "<>": msg#00002

Subject: Re: [Boston.pm] meaning of "<>"
  from perldoc perlop

       The null filehandle <> is special: it can be used to emulate the 
behavior of sed and awk.
       Input from <> comes either from standard input, or from each file listed 
on the command line.
       Here's how it works: the first time <> is evaluated, the @ARGV array is 
checked, and if it is
       empty, $ARGV[0] is set to "-", which when opened gives you standard 
input.  The @ARGV array is
       then processed as a list of filenames.  The loop

           while (<>) {
               ...                     # code for each line
           }

       is equivalent to the following Perl-like pseudo code:

           unshift(@ARGV, '-') unless @ARGV;
           while ($ARGV = shift) {
               open(ARGV, $ARGV);
               while (<ARGV>) {
                   ...         # code for each line
               }
           }

       except that it isn't so cumbersome to say, and will actually work.  It 
really does shift the
       @ARGV array and put the current filename into the $ARGV variable.  It 
also uses filehandle ARGV
       internally--<> is just a synonym for <ARGV>, which is magical.  (The 
pseudo code above doesn't
       work because it treats <ARGV> as non-magical.)

       You can modify @ARGV before the first <> as long as the array ends up 
containing the list of
       filenames you really want.  Line numbers ($.)  continue as though the 
input were one big happy
       file.  See the example in "eof" in perlfunc for how to reset line 
numbers on each file.


On Wed, Feb 01, 2006 at 01:39:06PM -0800, Stephen A. Jarjoura wrote:
> Hello, all;
> 
> I am debugging code written by someone else, and noticed the following:
> ===START=CODE===
> sub getParameters {
>    my $parm_list = do { local(@ARGV,$/); <> };
> }
> ===END=CODE===
> 
> This does what the author expected (slurp STDIN into the scalar "$parm_list") 
> but I was surprised by the "<>" as a stand-in for <STDIN> so I looked it up. 
> Here is what I found:
>    http://perldoc.perl.org/perlvar.html
> ===START=PASTE===
> ARGV
> 
> The special filehandle that iterates over command-line filenames in @ARGV . 
> Usually written as the null filehandle in the angle operator <> . Note that 
> currently ARGV only has its magical effect within the <> operator; elsewhere 
> it is just a plain filehandle corresponding to the last file opened by <> . 
> In particular, passing \*ARGV as a parameter to a function that expects a 
> filehandle may not cause your function to automatically read the contents of 
> all the files in @ARGV .
> 
> $ARGV
> 
> contains the name of the current file when reading from <>.
> ===END=PASTE===
> 
> I could find no reference to "<>" as a synonym for "<STDIN>" ... am I missing 
> something obvious? [The code works, so obviously it is my knowledge that is 
> faulty.] If someone could point me at a reference or doc or faq, I'd be 
> grateful.
> 
> Also, is there any particular reason (in the example given above) why '@ARGV' 
> would need to be localized, or is the author just being overly cautous?
> 
> Thanks, in advance, for your input!
> 
> ---
> Stephen ~runester~ Jarjoura
> http://runester.com
> http://boston.pm.org/kwiki/index.cgi?StephenJarjoura
> 
>  
>  
> _______________________________________________
> Boston-pm mailing list
> Boston-pm-PqP1ghmmPMdAfugRpC6u6w@xxxxxxxxxxxxxxxx
> http://mail.pm.org/mailman/listinfo/boston-pm
> 

-- 
     www.suave.net - Anthony Ball - ant-CWvJKW/e0ZpeoWH0uzbU5w@xxxxxxxxxxxxxxxx
        OSB - http://rivendell.suave.net/Beer
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Now cut that out!!" - Jack Benny


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
science.linguis...    culture.sf.lite...    video.mplayer.c...    yellowdog.gener...    ietf.rfc822/199...    emacs.help/2002...    redhat.release....    kernel.speakup/...    java.openejb.de...    debian.devel.gt...    xfree86.newbie/...    bug-tracking.ma...    pam/2003-05/msg...    games.devel.ope...    user-groups.lin...    music.pancham/2...    network.mq.deve...    web.html.genera...    arklinux.bugs/2...    linux.ecasound/...    qnx.openqnx.dev...    org.user-groups...    file-systems.sf...    trustix.contrib...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe