Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I think you *need* to rewind the file by calling seek FH, 0, 0; in the subroutine find.

The file operation in Perl is ultimately linked to the underlying unix file handles. When you read from a Perl file handle, Perl uses and updates the underlying unix file handle, thus giving the side effect.

I think ideally you are looking for the Perl's equivalent to the C stdio's dup function, to duplicate/copy an existing file handle into a new file handle (not just an alias to the existing file handle, but a new independent file handle), and operate on the second file handle in the subroutine. I am not aware of such facility in Perl 5 (my lack of research perhaps), but I think Perl 6 has implimented the dup function for the File Object.

Update:
Ok, I just learned that you can duplicate a file handle in Perl with open NEW, "<&OLD";. I quickly came up with the following code -

#!/usr/bin/perl -w use strict; sub find { local *IN = shift; my $find_me = shift; my $count = 0; # duplicate existing file handle open F2, '<&', *IN or die "Can not duplicate file handle"; my $tell = tell(F2); while (<F2>) { $count++ if (/$find_me/); } return "count: $count tell: $tell\n"; close F2; } open OUT, '>', 'tmp.txt' or die "$!\n"; print OUT while (<DATA>); close OUT or die "$!\n"; open IN, 'tmp.txt' or die "$!\n"; print find( *IN, 'a' ); print find( *IN, 'a' ); print find( *IN, 'd' ); print find( *IN, 'e' ); __DATA__ a a a c d e
The output is still -
#count: 3 tell: 0 #count: 0 tell: 13 #count: 0 tell: 13 #count: 0 tell: 13
That didn't work either! Ok, that taught me a lession - my assumption on the duplicated file handle could be wrong. I need some re-education.

Fellow monks, could you please tell me what is wrong with the duplicated file handle? Is perl actually creating a second independent file handle? Am I doing the right thing at all?

Thanks!


In reply to Re: desire to pass file handles by value by Roger
in thread desire to pass file handles by value by mandog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (2)
    As of 2019-06-17 04:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Is there a future for codeless software?



      Results (76 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!