For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2006 > 'use strict' and filehandles









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author 'use strict' and filehandles
Ted Fines

2006-08-31, 6:57 pm

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 ref
s' 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?

Thanks in advance,
Ted

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";

my $val = 1;
&something($val);
$val=2;
&something_else($val);
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");
}
}

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

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


Paul Lalli

2006-08-31, 6:57 pm

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 r

efs' 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

Timothy Johnson

2006-08-31, 6:57 pm

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@macalester.edu]=20
Sent: Thursday, August 31, 2006 12:17 PM
To: beginners@perl.org
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 =3D 'testinfo.log';
my $error_log =3D '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)=3D@_;
print $logfile "$msg\n";
}
------------------------------------------------------CODE SAMPLE A END




Paul Lalli

2006-08-31, 6:57 pm


Timothy Johnson wrote:
> If you want to pass a filehandle like that I would recommend using the
> FileHandle module.


Why? What's the advantage of that over just using lexical filehandles?

> As your script stands, however, you have no reason to pass the
> filehandle to the subroutine.


Yes he does.

> You can just do a "print ERRLOG $scalar" to print.


Except that he doesn't want the function to only print to ERRLOG, he
wants to choose which filehandle to print to based on what filehandle
is being passed to the function.

Paul Lalli

John W. Krahn

2006-08-31, 6:57 pm

Ted Fines wrote:
> Hi,


Hello,

> 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 "How do I pass filehandles between subroutines"

perldoc -q "How can I use a filehandle indirectly"



John
--
use Perl;
program
fulfillment
Ted Fines

2006-08-31, 6:57 pm


--- Original Message ---
> Ted Fines wrote:
>
> Hello,
>
>
> perldoc -q "How do I pass filehandles between subroutines"


Perfect. Perfect. Perfect. Thank you!

Ted

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com