Beginner wrote:
I thought that the code snip below should work but I am not getting
my text into the the file. The file is created but it's empty.
Ideally I want to create the log file outside any sub routines so
that $log to be available to any sub I have created and hence not
scoped to any one sub routine.
Is my understanding not correct here? Should I be doing this in
another way?
Hi Dermot. You're a little confused, but you're not far out!
#!/bin/perl
package Images;
use strict;
use warnings;
use vars qw($log);
$| = 1;
No need to autoflush.
our $log;
There is no need to declare the $log variable with 'our' here (or to
'use vars' above): a 'my' variable will do the job fine.
my $logpath = 'mylog.txt';
open($log,">>$logpath") or die "Can't append to $logpath: $!\n";
open my $log, '>>', $logpath or die "Can't append to $logpath: $!";
mysub();
sub mysub {
my $var = 'Hello';
This variable is never used after it is declared.
print $log "Starting mysub with val\n";
}
This looks fine, except that you haven't called the subroutine you've
declared!
This is what it looks like after those changes:
use strict;
use warnings;
my $logpath = 'mylog.txt';
open my $log, '>>', $logpath or die "Can't append to $logpath: $!";
mysub();
sub mysub {
print $log "Starting mysub with val\n";
}
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/