my $filename = $ARGV[0]; my $nb_lines = 0; open (my $file, $filename) or die "Can't open '$filename': $!"; my $hdl_sauv; while (<$file>) { $nb_lines++ ; if ($nb_lines == 5) { open ($hdl_sauv, "<",$file) || die "cannot open $file\n"; } } my $saved_line = <$hdl_sauv>; print "$saved_line\n";
It looks like you want something like this:
my $filename = $ARGV[ 0 ] or die "Usage: $0 file\n"; open my $file, $filename or die "Can't open '$filename': $!"; while ( <$file> ) { last if $. == 5; } print;
I am trying to make a copy of the filehandle, that is *not* sharing file position.
Your code does not make a copy of the filehandle, nor does it share the file position. It tries to open the previous filehandle as if it were a file name. If you had opened $filename instead of $file you would have a different filehandle for the same file, but that still wold not solve your problem as it would be pointed at the beginning of the file.
In reply to Re: How to make a copy of filehandle
by jwkrahn
in thread How to make a copy of filehandle
by loituma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |