When using a scalar variable, open (invisibly) returns an IO::Handle object. IO::Handle, as it happens, generates an "anonymous" typeglob using the Symbol module.

Perl doesn't actually have true anonymous typeglobs (as they must be (er.. make that) start as (see Update2 below) part of a symbol table), so what the Symbol module does to create pseudo-anonymous typeglobs is create a unique, unused-by-anything-else entry in the symbol table for the Symbol:: package. It then returns a (typeglob) reference to that symbol table entry.

So, uh, I guess the answer is: yes, it is really a typeglob, but no, it's not really lexically scoped (well, the ref is, but not the actual typeglob that it points to).

Update: Well, ok. There's some internal magic that mucks about with the name of the typeglob entry (as evidenced by Juerd's post), but the principle is the same.

Update2: Well, as tye points out, the internal magic of open uses the magic of local to break the glob away from its symbol table of origin (which also, incidentally, happens to be the same package the open statement is in, instead of a seperate package like Symbol) and make it truly lexical.

This has the added benefit that the name of the symbol used is irrelevent, so open uses the name of the original variable (which, of course, Symbol wouldn't have access to anyway). This allows warnings and errors to report the name of the opened filehandle as the name of the variable used to read from the file (and be right... most of the time).

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.


In reply to (bbfu) (IO::Handle and Symbol) Re: (Ovid) Re: I just realized that FILEHANDLES violate use strict by bbfu
in thread I just realized that FILEHANDLES violate use strict by princepawn

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.