Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

(Some) inconsistencies with IO built-ins, their documentation and filehandle names ("symbolic references")

by vr (Curate)
on Jun 09, 2020 at 11:34 UTC ( [id://11117865]=perlquestion: print w/replies, xml ) Need Help??

vr has asked for the wisdom of the Perl Monks concerning the following question:

I, certainly, always use lexical filehandles, and only noticed what follows, while poking with a stick at couple lines of code trying quick examples for foreach $1

The open explicitly states:

If FILEHANDLE is an undefined scalar variable (or array or hash element), a new filehandle is autovivified, meaning that the variable is assigned a reference to a newly allocated anonymous filehandle. Otherwise if FILEHANDLE is an expression, its value is the real filehandle. (This is considered a symbolic reference, so use strict "refs" should not be in effect.)

The print only says:

FILEHANDLE may be a scalar variable containing the name of or a reference to the filehandle, thus introducing one level of indirection.

And it also dies under use strict "refs" in case of "name".

Some of other built-ins seem to be quite tolerant (but why?) to expression producing a "symbolic reference"; what's worse is that their descriptions use what I'd call "vague" language.

close: FILEHANDLE may be an expression whose value can be used as an indirect filehandle, usually the real filehandle name or an autovivified handle.

(please, what's "unusual"?)

seek: FILEHANDLE may be an expression whose value gives the name of the filehandle

(that's clear enough)

readline: Reads from the filehandle whose typeglob is contained in EXPR

(actually, no -- EXPR also may give a name (and use strict "refs" doesn't matter). Or wait... Do they mean EXPR "contains typeglob" if it resolves to its name? English is not my mother tongue and I'm confused... H-mm)

read skips discussing what FILHANDLE may be alltogether... And so on.

Tangentially related, e.g. closedir dies angrily if expression resolves to plain string. Somewhat un-perlish, not "fail silently".

Replies are listed 'Best First'.
Re: (Some) inconsistencies with IO built-ins, their documentation and filehandle names ("symbolic references")
by ikegami (Patriarch) on Jun 09, 2020 at 17:27 UTC

    Things that can usually be used as a file handle:

    • A reference to an IO object (e.g. *STDOUT{IO}).
    • A glob containing a reference to an IO object (e.g. *STDOUT).
    • A reference to a glob containing a reference to an IO object (e.g. \*STDOUT).
    • A string naming a glob containing a reference to an IO object (e.g. "STDOUT").

    I would expect functions (named operators) to accept all of these. Subs may be more picky. In particular, subs don't normally support the last one.


    Language constructs (syntax) that can be usually be used to provide a file handle to a function (named operator):

    • A bareword naming a glob (e.g. STDOUT)
    • An expression evaluated in scalar context that returns something that can be used as a file handle.

    say, print and printf are notable exceptions. They require one of the following:

    • A bareword naming a glob (e.g. STDOUT)
    • A simple scalar that contains something that can be used as a file handle (e.g. $fh).
    • A block that evaluates to something that can be used as a file handle (e.g. { $fhs{$id} }).

    A bareword may be provided to subs using the «*» prototype (passing the bareword as a string).


    Update: What can be used as a handle vs what syntax can be used to provide it was poorly distinguished originally.

Re: (Some) inconsistencies with IO built-ins, their documentation and filehandle names ("symbolic references")
by LanX (Saint) on Jun 10, 2020 at 02:26 UTC
    I remember digging into it myself and becoming more and more confused in the past...

    Maybe some historical background is helpful.

    IIRC were lexical filehandles (with private variables and strict) only introduced with Perl5 (not even the first versions)

    They are held in one of the 6 slots of a typeglob and were referenced by a uppercase bare word like "FH" named like the glob.

    So writing FH meant using *FH{IO} like calling a sub "func()" meant using *func{CODE}->(). °

    In order to be able to pass a filehandle as argument to a sub, you had to write *FH.

    With Perl 5 came the need for lexical my $fh and avoiding typeglobs altogether while staying backwards compatible.

    Now that's how the mess with alternative syntax started, and the docs retain a lot of old wording.

    Ikegami already listed various different possibilities to denote a filehandle.

    DISCLAIMER:

    That's what I understood in the last 10 years lurking here in the monastery, I never coded Perl 4, so some details may be wrong.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) I think even this hash-like syntax to address slots in a glob didn't exist back then...

      Thank you for answers, ikegami and LanX, I understand the situation is pre-history heritage. Just curious, how it came that only open, opendir, print, printf actively object against "symbolic references" under use strict, in these Modern Perl times :). Even if it's too low priority to fix code, maybe stream-lining documentation would be not too much work -- now, where it's clearly stated strings are OK as indirection mechanism, the tone seems neutral or passively-encouraging :). Otherwise, to add to confusion, some built-ins (-X, etc.) accept both filehandles and strings (scalars/expressions), but here strings are filenames. Further curiosity, why all other /.*dir/ built-ins didn't possess indirection ability, in pre-history.

        I can't tell much about implementation or API, cause it's very complicated.

        You might want to discuss this on P5P.

        Otherwise ... in order to have deeper insight and better feedback here, could you please provide a list of hands on examples of what you see and what you want?

        > Even if it's too low priority to fix code, maybe stream-lining documentation would be not too much work

        Well I think you are always welcome to provide patches to perldoc.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11117865]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found