http://qs1969.pair.com?node_id=126815


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