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.


In reply to How to use empty "<>" (Diamond Operator) inside While loop by slayedbylucifer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.