Re: 'use strict' and filehandles



Ted Fines wrote:
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.
Execution of L:\test.pl aborted due to compilation errors.

OK. I've tried a few things to work around this, but no matter what I've tried I just get an error that I can't do something with strict something-else (refs or subs) is in use.

If I turn off strict it all just works, of course. Would someone please share how to solve this problem, while using the 'strict' directive?

perldoc -q filehandles

will show you both how to pass filehandles to subroutines, and the
right way to declare filehandles to begin with. Basically, use lexical
filehandle references rather than global barewords.

#!/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";

open my $INFLOG, '>>', $info_log
or die "Could not append to $info_log: $!";
open my $ERRLOG, '>>', $error_log
or die "Could not append to $error_log: $!";


my $val = 1;
&something($val);

Don't call subroutines with the & if you don't know what that does.

$val=2;
&something_else($val);

Ditto.

close ERRLOG;

sub something {
my ($val)=@_;
my $bad = 1;
my $good= 2;
if ($val==$bad) {
&log(ERRLOG,"Oh no! val $val is $bad, should be $good");

log($ERRLOG, "Oh no! ....");

}
}

sub something_else {
my ($val)=@_;
my $bad = 1;
my $good= 2;
if ($val != $bad) {
&log(INFLOG,"Great! val $val is not $bad");

log($INFLOG, "Great!! ...");

}
}

sub log {
my ($logfile,$msg)=@_;
print $logfile "$msg\n";

This stays the same.

}


Paul Lalli

.



Relevant Pages

  • updating tools
    ... use strict; ... sub choose_vi { ... When I use backspace to correct my mistake, vi does not open the proper ... It can't find the file and opens a new one. ...
    (comp.lang.perl.misc)
  • Re: Reading and from more than one filehandle / memory question
    ... put the filehandles inside another filehandles while loop. ... use strict; ... sub read_the_files{ ... "\nOpen successful"} ...
    (perl.beginners)
  • RE: Tracking Log In and Log Out times of Users
    ... AuditLogger is a Sub, not a module. ... A Report module is the same, except it is specific to that report. ... A module cannot have a Sub or Function ... I have a start-up form that opens when the database opens. ...
    (microsoft.public.access.modulesdaovba)
  • Re: delete rows
    ... 'delete it in the close it will exist only for that workbook. ... Private Sub Workbook_Open ... but will be available to anyone who opens the ... Dim Rng As Range, Rng1 As Range ...
    (microsoft.public.excel.programming)
  • RE: My Problem - Help Please
    ... On Error GoTo errorHandler 'Set error capture ... 'ON THE CODE LINE DIRECTLY BELOW, THE NEW INSTANCE OF EXCEL IS OPENED ... 'THIS LINE OF CODE OPENS THE NEW INSTANCE OF EXCEL. ... End Sub ...
    (microsoft.public.excel.worksheet.functions)