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

(tye)Re: unexpected close() success on (non-) filehandle

by tye (Sage)
on Nov 21, 2001 at 22:24 UTC ( [id://126815]=note: print w/replies, xml ) Need Help??


in reply to unexpected close() success on (non-) filehandle

Did you know that open "E", ... works just fine? That seems like it should be a symbolic reference, doesn't it? In other words, yes, stringy file handles suck and use strict doesn't always protect you from them. And that protection is even inconsistant, as you noted.

I'd say you've found a bug. It isn't one I care much about, though. (:

The real solution to your problem is to not simply use things like *E as file handles.

What you want is a reference to a glob (not just a glob) because it is easy to distinguish a reference to a glob from a string while distinguishing a glob from a string is a bit of a trick (I'm not even sure it is always possible). Globs are what Perl4 used in place of references and are really only in Perl5 for backward compatability.

And you want that glob to not currently be in the current symbol table. You can use Symbol's gensym() to get a reference to a glob in that module's symbol table. I tend to prefer:     my $fh= do { local(*FILE); \*FILE }; (which gives you a reference to a glob that was only briefly in the current symbol table) or to use IO::Handle.

Any of these three approaches will prevent the casual string from being successfully used as a file handle. Note that there are lots of other problems with having globs in your current symbol table that are open file handles. For example, open(CGI,... seriously breaks things...

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: unexpected close() success on (non-) filehandle
by mkmcconn (Chaplain) on Nov 21, 2001 at 23:29 UTC
    yes, stringy file handles suck and use strict doesn't always protect you from them. And that protection is even inconsistant, as you noted... tye

    That's what I wanted to know tye, thanks. That is, I recognized that the behavior I illustrated was caused by the way that the hash was constructed and used, which magnified the ambiguity of globs and strings (which I did to try to clarify my question). My focus was on why the 'protection' of the strict pragma "didn't work" sometimes. I assumed that use strict;, turning on perl's ability to decide the difference between 'F' and *main::F, would always work. When it didn't work with complete consistency, I wanted to understand why.

    So, I guess what I've learned from the discussion here is that there are some limits to perl's amazing context sensing abilities, but there is no need to approach that horizon, even when those limits are restrained by strict. Saying more explicitly what I mean can be an important help in assuring that the code will do exactly what I mean

    Thanks to all for all of your patience and helpful comments
    mkmcconn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found