RE: 'use strict' and filehandles



If you want to pass a filehandle like that I would recommend using the
FileHandle module.

As your script stands, however, you have no reason to pass the
filehandle to the subroutine. You can just do a "print ERRLOG $scalar"
to print.





-----Original Message-----
From: Ted Fines [mailto:fines@xxxxxxxxxxxxxx]
Sent: Thursday, August 31, 2006 12:17 PM
To: beginners@xxxxxxxx
Subject: 'use strict' and filehandles

Hi,

I'm running into a Cach-22 with 'use strict', and filehandles.

I have a program which opens two log files at the beginning:
open(INFLOG,">>$info_log") || die "Could not append to $info_log_file.
Quitting.\n";
open(ERRLOG,">>$error_log") || die "Could not append to $error_log_file.
Quitting.\n";

Then throughout the program, different subroutines print to the log
files, e.g. in code sample A below, ERRLOG and INFLOG are declared at
the start. A subroutine tries to pass 'ERRLOG' or 'INFLOG' to the 'log'
subroutine as a parameter. With 'strict refs' in use, this code
generates this error:
L:\>test.pl
Bareword "ERRLOG" not allowed while "strict subs" in use at L:\test.pl
line 22.

<snip>

CODE SAMPLE A
START------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

my $info_log = 'testinfo.log';
my $error_log = 'testerror.log';

open(INFLOG,">>$info_log") || die "Could not append to $info_log.
Quitting.\n";
open(ERRLOG,">>$error_log") || die "Could not append to $error_log.
Quitting.\n";

# <snip>

sub log {
my ($logfile,$msg)=@_;
print $logfile "$msg\n";
}
------------------------------------------------------CODE SAMPLE A END




.



Relevant Pages

  • Re: filehandle to a member of a zip archive
    ... way I can make my subroutine take in a barwood operator for the ... you meant "bareword filehandle" instead. ... You can pass a type glob, or a reference to a type glob as ... How can I make a filehandle local to a subroutine? ...
    (comp.lang.perl.misc)
  • Re: use strict and filehandles
    ... > subroutine identifier, in curly brackets or to the left of the ... Using a bare word in the filehandle ... In this case a bare word does everything I need ... I guess use strict is trying to make sure I ...
    (perl.beginners)
  • angle operator behavior with filehandles in @_
    ... I can get around this by storing the filehandle ... in a scalar that I define in the subroutine. ...
    (comp.lang.perl.misc)
  • Re: angle operator behavior with filehandles in @_
    ... filehandle to a scalar, and then using the angle operator ... I can get the angle operator to work as I ... >> that is passed to a subroutine. ...
    (comp.lang.perl.misc)
  • Re: Opening .dat file in perl
    ... use warnings; ... use strict; ... you are allowed to read from the $fh filehandle. ... perl fdmacdr.pl ...
    (perl.beginners)