in reply to 'strict refs' + 'strict sub' = suck.

Don't use the antique 'open' method, use the IO::File module instead:

use strict; use IO::File; my $f = new IO::File "filetoread.something", "r" or die "Can not open input file"; while (<$f>) { # do stuff ... } undef $f; # close the file # note that you don't even have to do this # this is optional because the file will be # closed when Perl script finishes. I am just # forcing an early file closure here.

Replies are listed 'Best First'.
Re: Re: 'strict refs' + 'strict sub' = suck.
by chromatic (Archbishop) on Oct 25, 2003 at 17:39 UTC

    Why not use open? Why is it "antique"?

Re: Re: 'strict refs' + 'strict sub' = suck.
by vek (Prior) on Oct 25, 2003 at 20:52 UTC

    Roger, you've pimped the use of IO::File over open once before in Re: Removing duplicates from list yet you never state why you think IO::File is better. I'm honestly curious as to why you prefer one over the other. Is it just because IO::File uses OO syntax?

    -- vek --
      Since Perl 5.6.0 we can perform file operations using a scalar - which is quite convenient to pass around in function calls. For example -

      open my $fh, ">output.txt" or die "Can't create output: $!"; print {$fh} "Hello world!\n"; close $fh; # if don't close, then it will be closed # automatically when $fh is out of scope.
      However the above code is not valid in version of Perl prior to 5.6.0, which I am still using on some of the production boxes. However the following will work on Perl prior to 5.6.0.
      my $fh = IO::File "output.txt", "w" or die "Can not create output: $!"; print $fh "Hello world!\n"; undef $fh;
      My preference of IO::File over open is on the Perl version compatibility and also ease of use.

      This is only my personal perference however.