globs, ref globs, and lexically scoped filehandles

A

Alex Hart

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
 
A

Anno Siegel

Alex Hart said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top