perlisok has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts,

I am new in Perl and DBI - I have been following book:

O'reilly - Programming Perl DBI - From the ...

I cannot deal with chapter:

2.5.1 The Perl Data::Dumper Module

I rewrote the code and I try to run it with my text-db xxx.txt : $name="MyName";$location="MyLoc";$mapRef="MyRef";$type="MyType";$description="MyDesc";

My code:

#!/usr/bin/perl use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Useqq = 1; $Data::Dumper::Purity = 1; die "Usage: updatemegadata <data file> <site name> <new map reference> +\n" unless @ARGV == 3; my $megalithFile = $ARGV[0]; my $siteName = $ARGV[1]; my $siteMapRef = $ARGV[2]; my $tempFile = "tmp.$$"; open MEGADATA, "<$megalithFile" or die "Can't open $megalithFile: $!\n +"; open TMPMEGADATA, ">$tempFile" or die "Can't open temporary file $temp +File: $!\n"; while ( <MEGADATA> ) { next unless m/\Q$siteName/; my $fields; eval $_; die if $@; my ( $name, $location, $mapref, $type, $description ) = @$fields; next unless $siteName eq $name; $fields = [ $name, $location, $siteMapRef, $type, $description ]; $_ = Data::Dumper->new( [ $fields ], [ 'fields' ] )->Dump(); $_ .= "\n"; } continue { print TMPMEGADATA $_ or die "Error writing $tempFile: $!\n"; } close MEGADATA; close TMPMEGADATA or die "Error closing $tempFile: $!\n"; unlink $megalithFile or die "Can't delete old $megalithFile: $!\n"; rename $tempFile, $megalithFile or die "Can't rename '$tempFile' to '$ +megalithFile': $!\n"; exit 0;
It gives no effects after:

perl myScript.pl xxx.txt MyName Lalala

Could you help me? Probably format of my file-db is wrong...

Replies are listed 'Best First'.
Re: Cannot update record in file with Dump module
by Athanasius (Archbishop) on Aug 15, 2012 at 16:06 UTC

    Hello perlisok, and welcome to the Monastery!

    In these lines:

    my $fields; eval $_; die if $@; my ( $name, $location, $mapref, $type, $description ) = @$fields;

    you create an empty variable $fields, then you set the values of $name, $location, etc., in the eval, but then you overwrite them in the very next line. This treats $fields as an array reference, but since $fields is still empty, dereferencing it as an array produces an empty list, and the 5 variables lose the values they were assigned in the eval!

    Here is the skeleton of a solution:

    #! perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Useqq = 1; $Data::Dumper::Purity = 1; my ($name, $location, $siteMapRef, $type, $description, $mapRef); while (<DATA>) { eval $_; die if $@; my $fields = [ $name, $location, $siteMapRef, $type, $description +]; $_ = Data::Dumper->new( [ $fields ], [ 'fields' ] )->Dump(); $_ .= "\n"; } continue { print $_; } __DATA__ $name="MyName";$location="MyLoc";$mapRef="MyRef";$type="MyType";$descr +iption="MyDesc";

    which produces the output:

    $fields = ["MyName","MyLoc",undef,"MyType","MyDesc"];

    Update: If you’d had use strict at the start of your script, it would have caught the problem for you. Always begin every script with:

    use strict; use warnings;

    See The strictures, according to Seuss. :-)

    Plus minor edits.

    Hope that helps,

    Athanasius <°(((><contra mundum

Re: Cannot update record in file with Dump module
by kcott (Archbishop) on Aug 15, 2012 at 17:39 UTC

    I'm not entirely sure what you mean by "It gives no effects after: ...". For instance, is $tempFile not created? If it is, then that would be an effect. Perhaps you mean $megalithFile is empty after the script completes; although, that seems unlikely unless you received some error message (which I'd also class as an effect).

    NOTE: I have the first edition of "Programming the Perl DBI" (February 2000), so what I'm looking at may differ from your copy if you have a more recent edition. The while loop you show is on page 24 of my copy - you may be able to use that as a reference point.

    The purpose of the examples you're referring to is to show how you could use Data::Dumper to format input records so that they may be evaled. On page 23, you'll see:

    my $dumpedValues = $dumper->Dump(); ... eval $dumpedValues;

    From your code above and your description of xxx.txt, you appear to be evaling the single record:

    $name="MyName";$location="MyLoc";$mapRef="MyRef";$type="MyType";$descr +iption="MyDesc";

    In order for

    my ( $name, $location, $mapref, $type, $description ) = @$fields;

    to be doing something useful, you should be evaling something that looks more like:

    $fields = [ qw{name location mapref type description} ];

    You appear to have removed all the comments that appear in the book. The comment regarding the eval should have provided a clue. My copy has:

    ### Evaluate perl record string to set $fields array reference my $fields; eval $_; die if $@;

    Two paragraphs before the while loop code, my copy has:

    "... use Data::Dumper to format the records ...

    I suggest you try doing that.

    If you're still having problems, please show your input and output within <code>...</code> tags (as opposed to a prosaic description).

    -- Ken