On Fri, Feb 3, 2017 at 7:18 PM, SSC_perl <[email protected]> wrote:
> I have the following sub that creates a log file using
> Data::Dumper. It also lists the name of each of the variables that it’s
> fed. It works great except for one problem - every line is appended with “
> = undef;” (minus the quotes).
>
> I’ve tried stripping it out of $var_value with regex but that
> doesn’t work, so it appears that Data::Dumper is adding that text during
> the print command.
>
> Does anyone know how I can eliminate that extra text from being
> printed? My searches have come up empty.
>
> Thanks,
> Frank
>
> --------------------------------------------
>
> datadump('log_name', '>>',
> "database $database",
> "matches $matches",
> "image $image",
> );
>
> sub datadump {
> my $filename = shift;
> my $write = shift;
> my @var_info = @_;
> no strict;
> open(my $log, $write, "temp_logs/TESTS-$filename.log");
> use Data::Dumper;
> foreach my $var (@var_info) {
> my @vars = split (/,/ => $var);
> my $var_name = '$'.$vars[0];
> my $var_value = $vars[1];
> print {$log} Data::Dumper->Dump( [$var_value], [$var_name] );
> $count++;
> }
> close($log);
> use strict;
> }
>
> Are you expecting variables $database, $matches, and $image to have commas
in them?
If not, your split command is returning a one element array - printing the
one element ($var_name) and then printing undef for $var_value.
For instance, run the following script:
[Start Code]
use strict;
use warnings;
my $count = 0;
my $matches = 'first,second,third,fourth';
my $image = "abc";
my $database = 'theDatabase';
datadump('log_name', '>>',
"database $database",
"matches $matches",
"image $image",
);
sub datadump {
my $filename = shift;
my $write = shift;
my @var_info = @_;
no strict;
# open(my $log, $write, "temp_logs/TESTS-$filename.log");
use Data::Dumper;
foreach my $var (@var_info) {
my @vars = split (/,/ => $var);
my $var_name = '$'.$vars[0];
my $var_value = $vars[1];
# print {$log} Data::Dumper->Dump( [$var_value], [$var_name] );
print Data::Dumper->Dump( [$var_value], [$var_name] );
print "\n\nvar_value: $var_value\nvar_name: $var_name\n\n"; # line 29
$count++;
}
# close($log);
use strict;
}
[End Code]
I get the following output:
$database theDatabase = undef;
Use of uninitialized value $var_value in concatenation (.) or string at
testit.pl line 29.
var_value:
var_name: $database theDatabase
$matches first = 'second';
var_value: second
var_name: $matches first
$image abc = undef;
Use of uninitialized value $var_value in concatenation (.) or string at
testit.pl line 29.
var_value:
var_name: $image abc
HTH, Ken