Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: How to make a copy of filehandle

by jwkrahn (Monsignor)
on Apr 09, 2013 at 08:46 UTC ( #1027681=note: print w/replies, xml ) Need Help??

in reply to How to make a copy of filehandle

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027681]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2016-10-26 19:09 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (346 votes). Check out past polls.