http://www.perlmonks.org?node_id=754438


in reply to Opening multiple files simultaneously

The "open $scalar" idiom takes an undefined scalar and gives it a value. That value is a particular filehandle. If you then open the same filehandle again, it closes the old file.

Try this.

sub open_new { my ( $filename ) = @_; open my $fh, '<', $filename or die "Can't read '$filename': $!"; return $fh; } push @filestack, open_new( 'test1.txt' ); push @filestack, open_new( 'test2.txt' );

Inside open_new, there's a new $fh containing undef every time.

Replies are listed 'Best First'.
Re^2: Opening multiple files simultaneously
by chrishowe (Acolyte) on Mar 31, 2009 at 15:42 UTC
    Superb, cheers :-)