in reply to Having Access to a file two times

seek, or close and re-open the file. ...or make your filehandle lexically scoped to a block just outside your while loop (which will handle closing your file for you). In other words:

{ open my $fh, '<', $filename or die $! while ( <$fh> ) { # do your stuff. } # Note, your $fh is about to pass out of scope, which # will close the filehandle in cleanup. } { $open my $fh, '<', $filename or die $! while ( <$fh> ) { # do your other stuff. } } # Now the second filehandle fell out of scope and closed too.

Dave

Replies are listed 'Best First'.
Re^2: Having Access to a file two times
by sesemin (Beadle) on Sep 21, 2008 at 07:16 UTC
    Hi Dave,

    I check the perl doc for SEEK. after the first while but before closing the file. I Added,

    seek , 0, 1;

    while <FH> ...

    it did not work, Is this the right way of using SEEk?

      No: you need to specify a filehandle.
      seek FH, 0, 1;
      Take another look at the seek docs.

      It's also nice to check the return status of the file operation for success:

      seek FH, 0, 1 or die "seek failed: $!";
        You mean ..
        seek FH, 0, 0;
        .. assuming the aim is to rewind back to the start.