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

Hello, I am just wondering why the "use strict;" does not produce any errors when we use bareword file handle. For example, the following code is not throwing any errors. use strict; open FH, ">test.txt" or die "not able to open file for writing $!"; Thanks & Regards, Mohamed

Replies are listed 'Best First'.
Re: use strict and bareword filehandle
by toolic (Bishop) on Apr 11, 2012 at 18:30 UTC
    According to perldoc open:
    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.)
Re: use strict and bareword filehandle
by eyepopslikeamosquito (Archbishop) on Apr 12, 2012 at 02:35 UTC

    If you want to catch this sort of thing, you could try using Perl::Critic. For example, running the default perlcritic command on your example code:

    use strict; open FH, ">test.txt" or die "not able to open file for writing $!";
    produces three warnings, as shown below:
    # perlcritic blah.pl Bareword file handle opened at line 2, column 1. See pages 202,204 of + PBP. (Severity: 5) Two-argument "open" used at line 2, column 1. See page 207 of PBP. ( +Severity: 5) Code before warnings are enabled at line 2, column 1. See page 431 of + PBP. (Severity: 5)

Re: use strict and bareword filehandle
by Anonymous Monk on Apr 12, 2012 at 00:17 UTC

    Backwards compatibility :)