Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to use empty "<>" (Diamond Operator) inside While loop

by slayedbylucifer (Scribe)
on Jun 25, 2012 at 08:58 UTC ( #978154=perlquestion: print w/ replies, xml ) Need Help??
slayedbylucifer has asked for the wisdom of the Perl Monks concerning the following question:

I am not getting "<>" diamond operator right. I want to understand how the "<>" (empty Diamond operator)" works in a "while"loop.

Here is my code ( the script is saved as diamond.pl):

#!/usr/bin/perl -w while (<>) { print ; }

Now i have two files called colors.txt and seasons.txt in the same directory as that of the above script. so if i run below command, it start printing the individual lines of both the .txt files:

perl diamond.pl colors.txt seasons.txt

and I understand above.

However, If I run below comamnds, it errors out;

perl diamond.pl these are words and not the file names

I get below eerror:

# perl diamond.pl these are words and not the file names Can't open these: No such file or directory at diamond.pl line 2. Can't open are: No such file or directory at diamond.pl line 2. Can't open words: No such file or directory at diamond.pl line 2. Can't open and: No such file or directory at diamond.pl line 2. Can't open not: No such file or directory at diamond.pl line 2. Can't open the: No such file or directory at diamond.pl line 2. Can't open file: No such file or directory at diamond.pl line 2. Can't open names: No such file or directory at diamond.pl line 2.

My undestanding was (and this is mentioned on numerous pages on hte internet), the empty "<>" checks for the @ARGV. So in above command, I was hoping to see the output as one word of the sequence "these are words and not the file names" printed on each line.

I am pretty sure am doing something really dumb here. could you explain how an "<>" (empty) operators works in a while loop. An example of the script and the command would be of great help.

Thanks.

Comment on How to use empty "<>" (Diamond Operator) inside While loop
Select or Download Code
Re: How to use empty "<>" (Diamond Operator) inside While loop
by choroba (Abbot) on Jun 25, 2012 at 09:04 UTC
    No, that is not how the diamond operator works. It checks for arguments, but only to treat them as file names. If there are no arguments, it reads its input from STDIN. You can try that with
    echo This is the standard input | perl diamond.pl
    See I/O Operators
Re: How to use empty "<>" (Diamond Operator) inside While loop
by Athanasius (Monsignor) on Jun 25, 2012 at 09:09 UTC
Re: How to use empty "<>" (Diamond Operator) inside While loop
by CountZero (Bishop) on Jun 25, 2012 at 09:19 UTC
    Thus sayeth the docs:
    The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them. 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.)
    Alwyas go back to the source before you trust random info on the internet.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: How to use empty "<>" (Diamond Operator) inside While loop
by Anonymous Monk on Jun 25, 2012 at 09:55 UTC
Re: How to use empty "<>" (Diamond Operator) inside While loop
by tobyink (Abbot) on Jun 25, 2012 at 09:57 UTC

    these are words and not the file names

    How do you expect Perl to tell the difference between words which are filenames and words which are not filenames?

    They're all treated as filenames. That's how <> works.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: How to use empty "<>" (Diamond Operator) inside While loop
by slayedbylucifer (Scribe) on Jun 27, 2012 at 03:04 UTC
    Hi Everyone, Thanks for your responses. I see now where I was going wrong. Thank you very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://978154]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-09-18 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (109 votes), past polls