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

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

Thread Previous
From:
Dave Mitchell
Date:
February 1, 2017 14:18
Subject:
Re: [perl #130673] readline(FH) throws a warning, <FH> does not
Message ID:
20170201141827.GI8158@iabyn.com
On Mon, Jan 30, 2017 at 08:44:36AM -0800, ROYAL@rt.perl.org 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: https://rt.perl.org/Ticket/Display.html?id=130673 >
> 
> 
> A bare word file handle causes the readline(FH) function to emit a warning: Name "main::FH" used only once: possible typo at readline_test.pl 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
up.

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

    <FH>

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
readline.

-- 
I before E. Except when it isn't.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About