Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to make a copy of filehandle

by loituma (Initiate)
on Apr 09, 2013 at 08:13 UTC ( #1027669=perlquestion: print w/ replies, xml ) Need Help??
loituma has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to make a copy of the filehandle, that is *not* sharing file position. I am parsing a big file, and at some point I want to keep a copy for later new parsing. I tried several things, but file positions keep getting shared. Here is an exemple of what I tried :

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";

I want $hdl_sauv to be a pointer to the position 5. This code isn't working, but I don't know how to write it. Any tip? Thanks!

Comment on How to make a copy of filehandle
Download Code
Re: How to make a copy of filehandle
by LanX (Canon) on Apr 09, 2013 at 08:19 UTC
    Are you looking for tell and seek ?

    They help getting and setting the "pointer" of a filehandle.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    PS: regarding your code

    >   open ($hdl_sauv, "<",$file) || die "cannot open $file\n";

    are you aware that $file is a filehandle and not a path?

    EDIT:

    If you wanna dup a filehandle you need something like "<&" as MODE. See open for details.

    Though don't think this handles position the way you want to.

Re: How to make a copy of filehandle
by hdb (Prior) on Apr 09, 2013 at 08:20 UTC

    Have a look at seek. You cannot store the file handle as you desire but the position in the file using tell and later go back using seek.

    UPDATE: A few seconds too late I was.

      Thanks to you two! I think this will do it. Easy :)

      ps : yes I am aware $file isn't file name, I just didn't know how to write what I wanted.

      > UPDATE: A few seconds too late I was.

      so what? =)

      No reason to edit or strike your post!!!

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re: How to make a copy of filehandle
by jwkrahn (Monsignor) on Apr 09, 2013 at 08:46 UTC
    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.

Re: How to make a copy of filehandle
by Rahul6990 (Beadle) on Apr 09, 2013 at 09:25 UTC
    This is because whenever you creates a file handle on a file it will point first line of the underlying file.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2015-07-01 20:35 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 (19 votes), past polls