La12 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,
I've got a problem. Here's the skinny...
I have the following code
#!/usr/bin/perl use IO::File; my $file = "foo.txt"; my $foo = IO::File->new($file) or die "Can't open original file: $!\n" +; while (<$foo>){ print <$foo>; }; close $foo; exit;
The file foo.txt contains the following
1 2 3 4 5
When I run my script it prints out 2 thru 5. If I change the while statement to
while (1){
it will print 1 thru 5, but will then hang. Am I using the while statement incorrectly?

Thanks for your time...

Edit ar0n 2001-07-27 -- title change

Replies are listed 'Best First'.
Re: help with (Reading A File)
by tadman (Prior) on Jul 27, 2001 at 05:50 UTC
    The problem is that you're reading the $foo handle before printing. This has the effect of zapping the first line. Here's how it goes:
    while (<$foo>) # Read in a line to $_ { print <$foo>; # Print the rest of the file }
    In this case, the first line you read is not actually used. Instead of your suggestion of while(1), try this:
    while (<$foo>) # Read in a line to $_ { print; # Print $_ }
    Or, in a more terse fashion:
    print while (<$foo>);  # Read in lines and print them
    Or, even the approach you seem to have hit on by accident:
    print <$foo>;  # Print all lines from the file
    Those are all simple ways of doing what you are looking for.

    If you're using a book for this, I'm curious as to the name of it, because that is some pretty bizarre Perl. If, however, this is your own code, then it is good that you are willing to explore. I am just worried that this came from a book which advocates that kind of programming.

    What has me mystified in particular is the mixing of IO::Handle methods and the traditional reading techniques. Usually you're using IO::Handle for a reason, but in this case it seems you're just making your life difficult. Here's the program written two ways:
    # -- IO Implementation -------------------------------- use IO::File; my $file = 'foo.txt'; my $handle = new IO::File ($file) || die "Could not open $file\n"; print $handle->getlines(); $handle->close(); # -- Traditional -------------------------------------- my $file = 'foo.txt'; open (FILE, $file) || die "Could not open $file\n"; print <FILE>; close (FILE);
    The IO::Handle methods may have more appeal for Java people who want everything OO. Others find the traditional method more appealing. Both do the job, so the choice is yours.

    However, try and keep your approach "pure". In some cases, mixing different types of calls can cause subtle errors, often when two approaches are mostly, but not entirely equivalent.
      Hi tadman,
      First i'd like to thank everyone for their help. I'm reading Networking with Perl and Professional Perl. I'm writing a server & client and have been using IO::Socket. For the script above I figured i'd stick with same approach and use IO::File (I'm used to using open (FILE, $file)). The reason I wrote
      while (<$foo>){ print <$foo>; }
      is because of the IO:Socket programing
      while (<$connection>); do... }
Re: help with
by LD2 (Curate) on Jul 27, 2001 at 05:37 UTC
    try this: change  print <$foo>; to  print $_;
Re: help with
by voyager (Friar) on Jul 27, 2001 at 05:39 UTC
    You are close. The <$foo>reads a line from the file, by default into $_. You can print by simply saying print; or explicitly print $_;.
Re: help with
by rrwo (Friar) on Jul 27, 2001 at 05:46 UTC

    <$foo> reads from the file. So you're reading the first line in the file, then in print reading another one and printing it. Try using $_ instead.

    For clarity sake, especially in situations where speed is not as important, it's best to say something like:

    while (my $line = <foo>) { print $line, "\n"; }

    The code is a bit more self-explanatory to Perl newbies (and sleep/food-deprived programmers) and is less likely to be buggy if you start using things like grep that twiddle with the value of $_.

      The code you offer is certainly clearer to an individual who has perhaps first started writing in Perl, coming from another language, but I have to quibble with your statement that "it's best."

      Part of learning a new language is learning its idioms. The implicit $_ is probably one of the most widely used idioms in Perl, and appears in almost every well-written script. By encouraging a new programmer to "pass by" that idiom, I think you make it less likely that the person will learn its use.

      I would say that it is actually "best to say" what the new programmer will have to learn to program efficiently and to read scripts by other programmers. After all, particularly in this case, the implicit $_ is not a difficult concept. If the new programmer doesn't learn it here, then learning grep and map will only be that much more difficult.

      As for the argument that one may introduce bugs if one starts using things like grep, it only becomes that much more likely that a programmer who is unfamiliar with the implicit $_ will make such errors if you try to "hide" its use from him/her in simpler structures.

      -HZ
Re: help with
by jlongino (Parson) on Jul 27, 2001 at 09:31 UTC
    La12, Everyone else has clarified the first method you used, but just for the sake of completeness . . .

    The second method (while(1) {) translates roughly to "read and print <$foo> forever". Unfortunately, once you're finished reading the file the while statement doesn't terminate. That's why the program hangs.

Re: help with
by Anonymous Monk on Jul 27, 2001 at 11:31 UTC
    <$foo> reads in a line of code and stores it in $_ what you want is: while <$foo> { print $_; } close $foo; exit;
      To all,
      I'd like to thank everyone for not only showing me the correct code, but explaining to me what I was doing wrong.
      Thanks everyone.