Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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.


Comment on Re: How to make a copy of filehandle
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-07-08 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls