CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
<> operator

 



perlwatcher
User

Jun 30, 2009, 11:10 AM

Post #1 of 22 (2457 views)
<> operator Can't Post

I am just confused with <> operator when there are two or more files opened for example

Code
open(FH,"C:\\test.txt"); 
open (FH1,"C:\\test1.txt");
while (<>){

if (eof ()){
print "File is reaching end \n";
}
print $_."\n";

}


Mentioning the <> operator in while loop does it print all the contents of both files ?

Thanks


KevinR
Veteran


Jun 30, 2009, 2:49 PM

Post #2 of 22 (2449 views)
Re: [arunbhargav] <> operator [In reply to] Can't Post

This should be very easy to test. Run your code and see what happens.
-------------------------------------------------


perlwatcher
User

Jun 30, 2009, 10:34 PM

Post #3 of 22 (2441 views)
Re: [KevinR] <> operator [In reply to] Can't Post

I ran the code and nothing came out... and it seems its in an infinite loop.


vikas.deep
User

Jun 30, 2009, 11:55 PM

Post #4 of 22 (2438 views)
Re: [arunbhargav] <> operator [In reply to] Can't Post

hi Arun
You may find the following link very useful. First just go through it (at least once).

Quote
http://perldoc.perl.org/functions/eof.html

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jun 30, 2009, 11:56 PM)


KevinR
Veteran


Jul 1, 2009, 12:02 AM

Post #5 of 22 (2435 views)
Re: [arunbhargav] <> operator [In reply to] Can't Post


In Reply To
I ran the code and nothing came out... and it seems its in an infinite loop.


There you go, you answered your own question:


Quote
Mentioning the <> operator in while loop does it print all the contents of both files ?



The answer is no, it does not.
-------------------------------------------------


vikas.deep
User

Jul 1, 2009, 12:10 AM

Post #6 of 22 (2434 views)
Re: [arunbhargav] <> operator [In reply to] Can't Post


Quote
An eof without an argument uses the last file read. Using eof() with empty parentheses is very different. It refers to the pseudo file formed from the files listed on the command line and accessed via the <> operator. Since <> isn't explicitly opened, as a normal filehandle is, an eof() before <> has been used will cause @ARGV to be examined to determine if input is available. Similarly, an eof() after <> has returned end-of-file will assume you are processing another @ARGV list, and if you haven't set @ARGV , will read input from STDIN ; see ""I/O Operators"" in perlop.

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


perlwatcher
User

Jul 1, 2009, 1:58 AM

Post #7 of 22 (2430 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post

Actually I know the definition of eof ... my concern was if I dont mention any File handle inside the <> operator when there are several file handles open and I haven't used any select function to select any particular file handle... in this context , what does <> operator do ?


vikas.deep
User

Jul 1, 2009, 2:50 AM

Post #8 of 22 (2427 views)
Re: [arunbhargav] <> operator [In reply to] Can't Post


Quote
my concern was if I dont mention any File handle inside the <> operator when there are several file handles open and I haven't used any select function to select any particular file handle... in this context , what does <> operator do ?


Dear Arun
I hope you are clear now and further You might (already) be knowledgeable about the following but just for that <> thing. Try the following

Code
#! usr/bin/perl 
use strict;
use warnings;
open(my $FH,"text");
open (my $FH1,"text1");
while (<>){
print $_."\n";
}


You will again get into that infinite loop and you will have to use Ctrl+C to get out .

Now do the same but this time give the file name from command line like this (text is a file name)

Quote
[root@localhost Documents]# perl hell.pl text

it should be clear that <> means <STDIN> unless you specify file handle.
If you use <> in your code then that $FH and $FH1 file handles and in fact open function becomes useless.

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


perlwatcher
User

Jul 1, 2009, 3:11 AM

Post #9 of 22 (2425 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post

thanks Vikas,

I was just confused with <> operator at one point ...

it should be clear that <> means <STDIN> unless you specify file handle.
If you use <> in your code then that $FH and $FH1 file handles and in fact open function becomes useless.

Quote

but the above information helped me


KevinR
Veteran


Jul 1, 2009, 8:54 AM

Post #10 of 22 (2415 views)
Re: [perlwatcher] <> operator [In reply to] Can't Post

You can do this:


Code
@ARGV =  ("C:\\test.txt", "C:\\test1.txt");  
while (<>){
if (eof ()){
print "File is reaching end \n";
}
print;
}


This is a bit of perl magic. Look up @ARGV for details.
-------------------------------------------------


vikas.deep
User

Jul 2, 2009, 3:56 AM

Post #11 of 22 (2404 views)
Re: [KevinR] <> operator [In reply to] Can't Post


Quote
@ARGV

The array @ARGV contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself. See $0 for the command name.


I read the above but i cannot correctly print $#ARGV irrespective of the arguments supplied to @ARGV I am always getting "-1". So in the following I was expecting 1 as the answer but I got "-1". (One print has been deliberately commented out )


Code
#! usr/bin/perl 
use strict;
use warnings;
@ARGV = ("text", "text1");
while (<>){
if (eof ()){
print "File is reaching end \n";
}
#print;
}print $#ARGV;
print $0
#The output is
#perl hell.pl
#File is reaching end
#-1hell.pl[root@localhost Documents]#


Next if I said


Code
@ARGV =  ("text", "text1","text2");


Again i get


Code
perl hell.pl 
File is reaching end
-1hell.pl[root@localhost Documents]#

This time I was expecting 2 and not "-1"

Could you please explain

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jul 2, 2009, 3:59 AM

Post #12 of 22 (2402 views)
Re: [perlwatcher] <> operator [In reply to] Can't Post

Hi
Dear Mr. Arun Bhargava

I see yesterday you were Arun but today you have become "perlwatcher". Arun is a very good name why you want to be "perlwatcher" but it is entirely up to you (and you must be having reasons for that). Anyway


Code
@ARGV =  ("C:\\test.txt", "C:\\test1.txt");

You can use the above as suggested by Kevin but it takes away your freedom of using any file you want at run time because here you are pre defining elements of @ARGV i.e. once you have defined your @ARGV as above you cannot open say "text3" or "text4".

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on Jul 2, 2009, 4:04 AM)


perlwatcher
User

Jul 2, 2009, 6:43 AM

Post #13 of 22 (2395 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post

Here when you are passing text and text1 ... the interpreter is actually checking for the files text and text1 , which I think aren't present in the directory from which you are running the script and hence nothing goes in to @ARGV array.

Therefore the -1 is printed for $#ARGV.

the <> operator deals with filehandles but you have mentioned @ARGV here ... so the <> checks with @ARGV and finds theres no valid data there ... so

the while loop is futile here ... nothing comes out of it ...

Larry or Kevin... please correct me If i am wrong ...


vikas.deep
User

Jul 2, 2009, 6:52 AM

Post #14 of 22 (2392 views)
Re: [perlwatcher] <> operator [In reply to] Can't Post

The relevant directory had both "text" and "text1" no doubt about it whatsoever and


Quote


Larry or Kevin... please correct me If i am wrong ...



saying this you are limiting your chances to receive a response to your post. In a public forum you should invite everybody or anybody to reply to our post; for what if Larry and/or Kevin are busy. No doubt it is always of benefit if you can get these experienced persons to reply you but that is not always possible.

Further somebody may take it the wrong way.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


perlwatcher
User

Jul 2, 2009, 7:43 AM

Post #15 of 22 (2388 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post

you are right ... itsn't working for me as well...

any help is appreciated


KevinR
Veteran


Jul 2, 2009, 10:37 AM

Post #16 of 22 (2381 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post

vikas,

you are printing the value of $#ARGV in the wrong place. What happens is that @ARGV is shifted when each file is opened with <>, so after all the files are opened there is nothing left in @ARGV so the value is -1. You can test that:


Code
#! usr/bin/perl  
use strict;
use warnings;
chdir('c:/Users/Kevin/Documents') or die "$!";
@ARGV = ("text.txt", "text1.txt");
print "The filenames are: @ARGV\n";
print "The last index of \@ARGV is: $#ARGV\n\n";
while (<>){
if (eof ()){
print "File $ARGV is reaching end \n";
}
print;
}
print "\nThe filenames are: @ARGV\n";
print "The last index of \@ARGV is: $#ARGV\n\n";


What you may find surprising is that eof() is not true until the last file is being read.
-------------------------------------------------


vikas.deep
User

Jul 5, 2009, 10:22 PM

Post #17 of 22 (2361 views)
Re: [KevinR] <> operator [In reply to] Can't Post

Kevin R
Thank you for your time and also for taking pains to post a detailed code here. Now I am clear about $#ARGV.

Quote
What you may find surprising is that eof() is not true until the last file is being read.

Yes I was surprised but then I re-read eof documentation a bit more carefully and I noted

Quote
In a while (<>) loop, eof or eof(ARGV) can be used to detect the end of each file, eof() will only detect the end of the last file.

Now things are clear to me.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


KevinR
Veteran


Jul 5, 2009, 11:05 PM

Post #18 of 22 (2355 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post

You're welcome
-------------------------------------------------


shawnhcorey
Enthusiast


Jul 6, 2009, 5:43 AM

Post #19 of 22 (2347 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post


In Reply To
it should be clear that <> means <STDIN> unless you specify file handle.
If you use <> in your code then that $FH and $FH1 file handles and in fact open function becomes useless.


Actually, <> means <ARGV> or realine() or readline( ARGV ) and eof() means eof( ARGV ). When you write something like:

Code
while( <> ){

The variable $ARGV is assign to each of @ARGV as it proceeds through the array. If @ARGV is empty, then ARGV is assigned to STDIN. You can get your program to read from STDIN in @ARGV by using '-' to indicate it should read from STDIN until a Control-D (or Control-Z in Windows) is encountered.

BTW, the program was not in an infinite loop; it was waiting for input. If you had typed Control-Z, it would have finished.

To summarize: @ARGV is a list of files to read through using <>, $ARGV is the name of each file, and ARGV is the file handle.

See:
perldoc -f readline
perldoc perlvar and search for /ARGV/

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


vikas.deep
User

Jul 6, 2009, 11:56 PM

Post #20 of 22 (2337 views)
Re: [shawnhcorey] <> operator [In reply to] Can't Post

Dear shawnhcorey

Thank you for your detailed post over here and providing further clarity. All suggestions and more importantly criticism is always very welcome. I must say that this particular post on your part was really hair-splitting.

Having said that I must say a greater part of your post was already clear to me.

However the fact that <> does not mean <STDIN> was really a finer detail for me. I read the following

Quote
"It also uses filehandle ARGV internally--<> is just a synonym for <ARGV>, which is magical."

from "http://perldoc.perl.org/perlop.html#I%2fO-Operators"

and also

"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. "


My understanding is as above and also as you have said


Quote
"If @ARGV is empty, then ARGV is assigned to STDIN. "


This means that when one is entering filename from STDIN the filename is first assigned ARGV and then it should be called and understood as <ARGV> and not <STDIN>. Thank you for this finer detail. But as you know better this is an internal magic and I was ignorant about this intricacy. When I said <> means <STDIN> I only meant to say that the following script works well if you enter your name from STDIN

Code
#! usr/bin/perl 
use strict;
use warnings;
print "Enter your name";
while (<>) {
chomp;
print $_;
last;
}


but i never knew that internally <ARGV> is happening and not <STDIN>. Now it looks very obvious because even if filename is entered from STDIN. The needs a Filehandle to open the said file and that file handle is actually ARGV.

Infact <STDIN> should not be taken literally.

Thankyou

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


shawnhcorey
Enthusiast


Jul 7, 2009, 12:28 AM

Post #21 of 22 (2332 views)
Re: [vikas.deep] <> operator [In reply to] Can't Post


In Reply To
This means that when one is entering filename from STDIN the filename is first assigned ARGV and then it should be called and understood as <ARGV> and not <STDIN>.


I don't understand what you're saying here. Normally the filenames are entered in the command line and are assigned to @ARGV when perl starts. STDIN can be re-directed by pipe-lining in the shell:

Code
ls | myperlscript foo.txt - bar.txt

In this case, <> will read foo.txt, then the directory listing, then bar.txt (note the minus sign between the text files).

Don't confuse the terminal with STDIN. Most of the time they are connected but if you write a CGI script, there is no terminal, just STDIN. The file test, -t, is used to determine if STDIN, STDOUT and STDERR are connected to a terminal.

Code
if( -t 0 ){ 
# STDIN is connected to a terminal
}

See `perldoc perlfunc` and search for /-X/.

And to add more confusion, the command can be put in a batch file (a shell script file) and executed from the command line or as a CGI.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


vikas.deep
User

Jul 7, 2009, 12:51 AM

Post #22 of 22 (2329 views)
Re: [shawnhcorey] <> operator [In reply to] Can't Post

I thank you for this latest post even as it might be late night in your part of world I will re-read this and post my response if any after lunch. here it is lunch hour.
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives