globs, ref globs, and lexically scoped filehandles

Discussion in 'Perl Misc' started by Alex Hart, Jan 24, 2005.

  1. Alex Hart

    Alex Hart Guest

    2 questions, but somewhat related.

    What is the rule for when it's possible to use a glob, and when it's
    possible to use a ref glob. Can they always be used interchangably? The
    docs seem a little vague on this. Is this a property of the glob, or of
    the function being called?

    Also, concerning lexically scoped filehandles (which is really a ref
    glob, right?), can these be used anywhere a filehandle would be used
    (i.e. will they be auto-vivicated anywhere a file handle is expected?).
    I always see the example with open, but what about sysopen, or accept,
    or bind, or anything else. Can I always use a undefined scalar in these
    functions?

    Thanks.

    - Alex Hart
    Alex Hart, Jan 24, 2005
    #1
    1. Advertising

  2. Alex Hart

    Anno Siegel Guest

    Alex Hart <> wrote in comp.lang.perl.misc:
    > 2 questions, but somewhat related.
    >
    > What is the rule for when it's possible to use a glob, and when it's
    > possible to use a ref glob.


    The term is "globref", meaning a reference to a glob. "ref glob" doesn't
    make sense.

    > Can they always be used interchangably? The
    > docs seem a little vague on this. Is this a property of the glob, or of
    > the function being called?


    No, they're not always interchangeable. For one, a globref could be blessed
    into any class, and then the class determines its behavior.

    Otherwise, the automatic de-referencing of globrefs happens where it is
    convenient -- it's a function of the DWIM behavior of Perl. In fact,
    even the interpretation of a bare glob as a file handle is a DWIM action.
    It might as well be interpreted as something else, or not at all. Since
    there is little use for globs in modern Perl, I/O functions take it for
    granted that you mean the I/O slot.

    > Also, concerning lexically scoped filehandles (which is really a ref
    > glob, right?), can these be used anywhere a filehandle would be used
    > (i.e. will they be auto-vivicated anywhere a file handle is expected?).
    > I always see the example with open, but what about sysopen, or accept,
    > or bind, or anything else. Can I always use a undefined scalar in these
    > functions?


    There is no such thing as lexically scoped filehandles. A lexical variable
    can hold a *value* that is interpreted as a filehandle by I/O functions.
    This doesn't give the variable special properties of a filehandle, nor
    does it make the underlying filehandle "lexical".

    As for autovivifivation, I'd expect all methods to open a file handle to
    do it. The feature is indeed not described for "sysopen" the way it is for
    "open".

    Anno
    Anno Siegel, Jan 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sunshine

    Lexically scoped variables

    Eric Sunshine, Dec 19, 2003, in forum: Ruby
    Replies:
    6
    Views:
    130
    Robert Klemme
    Dec 26, 2003
  2. Gavin Sherlock
    Replies:
    1
    Views:
    92
    Uri Guttman
    Dec 10, 2003
  3. Alex Hart
    Replies:
    0
    Views:
    78
    Alex Hart
    Jan 24, 2005
  4. Derek Basch
    Replies:
    6
    Views:
    162
    Michele Dondi
    May 29, 2006
  5. Justin C
    Replies:
    1
    Views:
    179
    Justin C
    Oct 7, 2013
Loading...

Share This Page