jimmie78
Technical User
- Jun 25, 2007
- 2
* Perl version (Posted below)
* Perl error (Posted below)
* Perl code (Posted below)
Objective: Seeking help to pinpoint and resolve my coding issues. I'm no expert but I'm sure it's a simple problem someone else out there will be able to resolve in 2 seconds.
Issue: Foreach block of codes isn't working as per my requirements and is producing the posted "Bash Shell Output" error message (see below). Is this a variable scope issue? If so, how do resolve it?
Summary
-------
1. @log_cache is already declared with the "my" statement near the top(see below).
2. @log_cache will hold 2 array elements on initial run. (Tested/Verified as true with test print statements placed inside and outside the "foreach" block of code)
3. "Bash Shell Output" error message(see below)is reported upon run.
4. Corresponding @log_cache log entries are not appended to the /projects/test/log/getlist.log file (see below) per my expectations by the "foreach" block.
5. Declaring @log_cache with "my" once again in the "foreach" block will eliminate the "Bash Shell Output" error message (see below) but it look as though it will becomes a local array to the "foreach" block as no array elements will exist inside the block once this is done. (Tested/Verified with test print statements placed inside and outside the "foreach" block of code).
------------------------------------------------------------
Perl Version
------------
linux:/# perl -v
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
-----------------------------------------------------------
Perl Code
---------
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Cwd;
use File:
ath;
use Log::Handler;
system('clear');
my $working_dir = "/projects/test/"; # Download directory.
my $logdir = "$working_dir"."log/"; # Log directory.
my $logfile1 = "$logdir"."getlist.log"; # getlist.pl log file.
my @log_cache;
# Check directories exists else if not create it.
if (not(-d $working_dir)) { # Check the download directory exists else if not create it.
push(@log_cache,"Creating directory $working_dir");
mkpath (
$working_dir,
{verbose => 1}
) or die "Cannot make path $working_dir: $!";
}
if (not(-d $logdir)) { # Check the log directory exists else if not create it.
push(@log_cache,"Creating directory $logdir");
mkpath (
$logdir,
{verbose => 1}
) or die "Cannot make path $logdir: $!";
}
my $log = Log::Handler->new( # Call new() to create a new log handler object.
filename => $logfile1,
mode => 'append',
permissions => 644,
newline => 1,
maxlevel => 7,
minlevel => 0
);
foreach my $i (@log_cache) { # BUG: Variable scope issue. Bug need to still be resolved.
log->info($i);
}
$log->info("Changing to $working_dir");
chdir($working_dir); # Change to the working directory first before copying.
$log->info("--- End ---");
-----------------------------------------------------------
Bash Shell Output
-----------------
mkdir /projects/test/
mkdir /projects/test/log/
Use of uninitialized value in log at /projects/getlist.pl line 55 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Uncaught exception from user code:
Can't take log of 0 at /projects/getlist.pl line 55.
at /projects/getlist.pl line 55
linux:/#
------------------------------------------------------------
/projects/test/log/getlist.log
------------------------------
Jun 25 23:10:47 [INFO] Changing to /projects/test/
Jun 25 23:10:49 [INFO] --- End ---
------------------------------------------------------------
Regard,
Jimmie.
* Perl error (Posted below)
* Perl code (Posted below)
Objective: Seeking help to pinpoint and resolve my coding issues. I'm no expert but I'm sure it's a simple problem someone else out there will be able to resolve in 2 seconds.
Issue: Foreach block of codes isn't working as per my requirements and is producing the posted "Bash Shell Output" error message (see below). Is this a variable scope issue? If so, how do resolve it?
Summary
-------
1. @log_cache is already declared with the "my" statement near the top(see below).
2. @log_cache will hold 2 array elements on initial run. (Tested/Verified as true with test print statements placed inside and outside the "foreach" block of code)
3. "Bash Shell Output" error message(see below)is reported upon run.
4. Corresponding @log_cache log entries are not appended to the /projects/test/log/getlist.log file (see below) per my expectations by the "foreach" block.
5. Declaring @log_cache with "my" once again in the "foreach" block will eliminate the "Bash Shell Output" error message (see below) but it look as though it will becomes a local array to the "foreach" block as no array elements will exist inside the block once this is done. (Tested/Verified with test print statements placed inside and outside the "foreach" block of code).
------------------------------------------------------------
Perl Version
------------
linux:/# perl -v
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
-----------------------------------------------------------
Perl Code
---------
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Cwd;
use File:
use Log::Handler;
system('clear');
my $working_dir = "/projects/test/"; # Download directory.
my $logdir = "$working_dir"."log/"; # Log directory.
my $logfile1 = "$logdir"."getlist.log"; # getlist.pl log file.
my @log_cache;
# Check directories exists else if not create it.
if (not(-d $working_dir)) { # Check the download directory exists else if not create it.
push(@log_cache,"Creating directory $working_dir");
mkpath (
$working_dir,
{verbose => 1}
) or die "Cannot make path $working_dir: $!";
}
if (not(-d $logdir)) { # Check the log directory exists else if not create it.
push(@log_cache,"Creating directory $logdir");
mkpath (
$logdir,
{verbose => 1}
) or die "Cannot make path $logdir: $!";
}
my $log = Log::Handler->new( # Call new() to create a new log handler object.
filename => $logfile1,
mode => 'append',
permissions => 644,
newline => 1,
maxlevel => 7,
minlevel => 0
);
foreach my $i (@log_cache) { # BUG: Variable scope issue. Bug need to still be resolved.
log->info($i);
}
$log->info("Changing to $working_dir");
chdir($working_dir); # Change to the working directory first before copying.
$log->info("--- End ---");
-----------------------------------------------------------
Bash Shell Output
-----------------
mkdir /projects/test/
mkdir /projects/test/log/
Use of uninitialized value in log at /projects/getlist.pl line 55 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Uncaught exception from user code:
Can't take log of 0 at /projects/getlist.pl line 55.
at /projects/getlist.pl line 55
linux:/#
------------------------------------------------------------
/projects/test/log/getlist.log
------------------------------
Jun 25 23:10:47 [INFO] Changing to /projects/test/
Jun 25 23:10:49 [INFO] --- End ---
------------------------------------------------------------
Regard,
Jimmie.