Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot update record in file with Dump module

Status
Not open for further replies.

yesilikeit

Programmer
Aug 15, 2012
1
IT
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 :

Code:
$name="MyName";$location="MyLoc";$mapRef="MyRef";$type="MyType";$description="MyDesc";

My code:

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 $tempFile: $!\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...
 
First, I'm rather pedantic and tend to use explicit variables rather that playing with $_, except in very well defined and brief instances. And, drop some print statements in a few places to see what's working and what's not.

Code:
while ( my $line = <MEGADATA> ) {
	print "LINE: $line";
	next unless ($line =~ m/\Q$siteName/);

	my $fields;
	eval $line;  # Not sure what you're doing, here.
	die if $@;

	my ( $name, $location, $mapref, $type, $description ) = split /some delimiter/, $line;
	print "NAME: $name\n";
	unless ($name =~ /\w/) { die "Apparently don't have a name and we should.\n"; }
and so on....


'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top