Here's some free code to chew on. The thing to takeaway is that serializing moderate amounts of data in Perl just require Data::Dumper and the do() function. There's a lot of other stuff going on as well. E.g. the current run() method will overwrite the data with what's in the __DATA__ section every time. It would probably be more useful to have it pull in and build off what's already in your data file.<br>
<br># In a file called DataStuff.pm<br># perl DataStuff.pm to run<br>package DataStuff;<br>use strict;<br>use warnings;<br>use Data::Dumper;<br><br>__PACKAGE__->run if not caller;<br><br>sub run {<br> my( $class ) = @_;<br>
my( $dump ) = do { local $/; <DATA>; };<br> my $data1 = eval $dump or die $@;<br> $data1->{bum} = 42;<br> push @{ $data1->{aref} }, 11;<br> $class->save_data( '<a href="http://foo.pl">foo.pl</a>', $data1 );<br>
my $data2 = $class->get_data( '<a href="http://foo.pl">foo.pl</a>' );<br> print Dumper( $data2 );<br>}<br><br>sub save_data {<br> my( $class, $file_name, $data ) = @_;<br> local $Data::Dumper::Terse = 1;<br>
open my $DATA, '>', $file_name or die "$0 ERROR: write $file_name - $!";<br> print $DATA Dumper( $data );<br>}<br><br>sub get_data {<br> my( $class, $file_name ) = @_;<br> return do $file_name;<br>
}<br><br>1;<br><br>__DATA__<br><br>{<br> foo => 1,<br> bar => "baz",<br> aref => [ 1 .. 10 ],<br>};<br><br><br><div class="gmail_quote">On Tue, Sep 7, 2010 at 10:17 AM, thomas lems <span dir="ltr"><<a href="mailto:tmlems2000@yahoo.com" target="_blank">tmlems2000@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Good morning All,<br>
I’m new to Perl, and i used Perl to create a table with five columns and 30 rows. I was able to save the output to a text file, but I’m struggling on how to keep the data in the text file cause every time I run the script I lose the previous data.<br>
I believe the issue it’s in the Sub Save Data procedure.<br>
Here is my Sub SaveData procedure:<br>
sub SaveData {<br>
my ($file_name,$data,$rows,$cols) = @_;<br>
<br>
open(DATA,">$file_name") or die "$0 ERROR: write $file_name - $!";<br>
for (my $i=0;$i<$rows;$i++)<br>
{<br>
for (my $j=0;$j<$cols;$j++)<br>
{<br>
printf DATA ("<%s> ",$data->{"$i,$j"});<br>
}<br>
print DATA "\n";<br>
}<br>
close DATA;<br>
exit;<br>
}<br>
Please help!<br>
Thanks<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
Buffalo Perl Mongers Homepage<br>
<a href="http://buffalo.pm.org" target="_blank">http://buffalo.pm.org</a><br>
<br>
Buffalo-pm mailing list<br>
<a href="mailto:Buffalo-pm@pm.org" target="_blank">Buffalo-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/buffalo-pm" target="_blank">http://mail.pm.org/mailman/listinfo/buffalo-pm</a></blockquote></div><br>