Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Localizing Filehandles

by Adam (Vicar)
on Jul 13, 2000 at 23:07 UTC ( [id://22439]=perlmeditation: print w/replies, xml ) Need Help??

On a note quite similar to mikfire's The Clue by Four, and the excellent Before You Post ... by Ovid, I wanted to share the strugle of my last hour with you.

I am writting a recursive function that opens a file, but I couldn't remember how to localize a filehandle. So I started to write a detailed explanation of the problem when an idea struck me... I could kludge my way around this. That was a bad idea. The kludge worked, but it was both slow and ugly, so I starting trying to write my SOPW post again. But then, while staring at the title of that post, I realized that I hadn't looked in the index of my favorite perl reference for filehandles, localizing which I quickly did and found that on page 51 was the solution. A quick addition of one line (and the removal of the kludge) and my code was back up to speed and functional.

The moral of the story is that the more thought you put into a post, the more likely you are to figure out the answer yourself. And of course, simple things are usually simple, at least in perl.

By the way, "local *MYFILEHANDLE;" is how you localize a filehandle.

Replies are listed 'Best First'.
RE: Localizing Filehandles
by ahunter (Monk) on Jul 14, 2000 at 14:38 UTC
    ...unless you're using Perl 5.6, in which case:
    open my $fh, "<foo.txt";
    and similar now work (apparently, I've never tried it). And of course, there's always IO::Handle & friends.

    Andrew.

      Yes. Additionally you can use the FileHandle module:
      use FileHandle; sub Whatever { my $filehandle = new FileHandle; open $filehandle, $file or die "Failed to open $file, $!"; # do stuff. close $filehandle or die "Failed to close $file, $!"; }
      But that wasn't my point.
        I was going to add "which only goes to show that by posting here you find out several other ways to do the same thing", but it seems I forgot.

        Andrew.

RE: Localizing Filehandles
by BlueLines (Hermit) on Jul 15, 2000 at 01:15 UTC
    Don't ever try this if you play with threads in 5.005. I had a sub that needed to be run many times in parallel:
    sub foo { my ($file, $info) = @_; local *FH; open (FH, ">" . $file) or die "$!\n"; print FH $info; close FH; }
    Aparrently perl wasn't too happy with 4 seperate threads copying and writing to the same typeglob, and dumped core to show its' appreciation for the operation :-) I'm glad that  my FH is legit in perl 5.6, although i haven't played with the new threading implementation yet...
      Interesting. I'll have to watch for that. Although I suspect that it had more to do with perl threads not being fully stable and less to with different threads writing to the same filehandle, since they shouldn't be able to see each other's local typeglobs (they should be out of scope).
        The way my boss explained it to me was that local copies the global typeglob, grabs it for local use, and replaces it with a copy of what it grabbed. Thus, any read/write is done on the global FH, even though it is being used in a local scope. The next thread does the same thing, although it copies the typeglob that's being locally used by the first thread (it _is_ still the global one, it's just not being treated as such by the thread). So when the second thread grabs local *FH, it's not grabbing a clean undef typeglob. It's actually grabbing a copy of the typeglob that thread 1 is currently working with, with whatever I/O garbage thread one had in the FH at that particular time. Which is why local sucks in general, and why my rocks my world.

        Disclaimer:At least this is what i think he said.Most people that can detail the changes that occured in the low level file handling structure of perl when 3.0 was released tend to explain things in depths that this PH can't always understand:-)

        BlueLines

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://22439]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-16 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found