You are splitting on /,/ but your name and value are separated by a space.
This means $var_name is being set to "database $database" not "database"
and $var_value is undef (the undefined value).  This whole routine is
suspect though.  What if you want to pass an array or a hash?  You can't
pass them as a string.  Also, what is up with turning off strict?  Anytime
you feel turning off strict is a good idea, it is time revisit what you are
doing and why (there are reasons, but the chances of you stumbling into
them are slim; when time comes, you will know).

A better option is to pass either a hashref or (as I have done below) a
list into your logging function:

#!/usr/bin/perl

use strict;
use Data::Dumper;
use File::Path;
use warnings;

my $database = "db1";
my @matches = ("foo", "bar", "baz");
my %image = ( height => 10, width => 10, imgdata =>
"12122349129321938127387123" );

datadump('log_name', '>>',
database => $database,
matches  => \@matches,
image    => \%image,
);

sub datadump {
my $filename = shift;
my $mode     = shift;
my %vars     = @_;

mkpath "temp_logs";

local $Data::Dumper::Useqq = 1;

my $file = "temp_logs/TESTS-$filename.log";
open my $log, $mode, $file or die "could not open $file for writing: $!";

print $log Dumper \%vars or die "could not write to $file";

close $log or die "could not close $file: $!";
}


Which writes the following file:

$VAR1 = {
          "database" => "db1",
          "matches" => [
                       "foo",
                       "bar",
                       "baz"
                     ],
          "image" => {
                     "width" => 10,
                     "height" => 10,
                     "imgdata" => "12122349129321938127387123"
                   }
        };



On Fri, Feb 3, 2017 at 7:20 PM SSC_perl <p...@surfshopcart.com> 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;
> }
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to