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

In reply to (tye)Re: unexpected close() success on (non-) filehandle by tye
in thread unexpected close() success on (non-) filehandle by mkmcconn

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, details, 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, summary, 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.