develooper Front page | perl.perl5.porters | Postings from February 2017

Re: [perl #130673] readline(FH) throws a warning, <FH> does not

Thread Previous
Dave Mitchell
February 1, 2017 14:18
Re: [perl #130673] readline(FH) throws a warning, <FH> does not
Message ID:
On Mon, Jan 30, 2017 at 08:44:36AM -0800, wrote:
> # New Ticket Created by  ROYAL, Ryan 
> # Please include the string:  [perl #130673]
> # in the subject line of all future correspondence about this issue. 
> # <URL: >
> A bare word file handle causes the readline(FH) function to emit a warning: Name "main::FH" used only once: possible typo at line 4.
> -The equivalent diamond operator <FH> used on the same bare word handle does not evoke the warning.

readline takes a typeglob as an argument, while the <> operator takes the
*name* of a typeglob, So, as the docs for readline say,

    $line = <STDIN>;
    $line = readline(*STDIN);    # same thing

Note that one has a '*', the other doesn't. However, readline will accept
a bareword string, and at runtime use it as the name of a typeglob to look

So at *compile* time (at which time 'only used once' warnings are checked
for and issued), 


is compiled as

    readline *FH

and a typeglob check is done; while

    readline FH

is compiled as 

    readline 'FH'

and no typeglob is involved or checked for,

Note that 'readline *FH' *will* give a warning.

It might be possible to change it so that in 'readline FH', the "FH"
string is converted to a typeglob at compile time, but personally I'm in
favour of leaving anything filehanldle/typeglob related well alone. It has
a ton of edge-case weird semantics inherited from perl4 days.

> -Using a lexically scoped file handle with readline( $fh ) also does not evoke the warning.

Lexicals don't give 'only used once' warnings in any situation, not just

I before E. Except when it isn't.

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About