Dear David, Thanks for throwing light on the topic by citing the security implications of executing eval and also suggesting the industry standard 'JSON' for interoperability.
It is greatly appreciated. best, Shaji ------------------------------------------------------------------------------- Your talent is God's gift to you. What you do with it is your gift back to God. ------------------------------------------------------------------------------- ________________________________ From: David Precious <dav...@preshweb.co.uk> To: Perl Beginners <beginners@perl.org> Sent: Friday, 13 September 2013 6:01 PM Subject: Re: Help on saving and retrieving data structures On Fri, 13 Sep 2013 19:10:58 +0800 (SGT) *Shaji Kalidasan* <shajiin...@yahoo.com> wrote: > I am saving the data structure to a file and retrieving it back > again, but, when I 'use strict' it is giving the following error > message > > Global symbol "%game" requires explicit package name Others have pointed out that the problem will be solved by adding a "my %game" before you eval the contents of the file. However, I personally would suggest serialising the data to JSON and storing that in the file, rather than using Data::Dumper - that way you've got your data in a more standard format in case you want to work with it with other tools, and also you're not eval'ing code, so if someone had decided to edit that file and add, say, system('rm -rf /') in it, you won't suddenly execute things you didn't expect. It can be as simple as saying "use JSON" to load JSON.pm, then using JSON::to_json(\%game) to generate JSON to write to the file, then saying e.g. my $game = JSON::from_json($json) (where $json is the JSON you just read in from the file). Alternatively, you could even look at DBM::Deep, which would abstract away the saving/loading for you (although you'd lose the bonus of having stored the data in a standard widely-supported format). > > It is working fine without 'strict'. Please help > > [code] > #Code for saving the data structure > use strict; > use warnings; > use Data::Dumper; > > my %game = ( > worldcup => { > series => "WC 2011", > play => ["monday", "wednesday", "friday"], > players => [ > {name => "dhoni", role => "captain", country => > "india"}, {name => "tendulkar", role => "batsman", country => > "india"}, {name => "yuvraj", role => "batsman", country => "india"} > ], > }, > > ipl => { > series => "Edition 5", > play => ["saturday", "sunday"], > players => [ > {name => "sehwag", role => "captain", country => > "india"}, {name => "muralidharan", role => "batsman", country => > "srilanka"}, {name => "gayle", role => "batsman", country => > "westindies"} ], > }, > ); > > $Data::Dumper::Purity = 1; > open my $fout, '>', 'gameinfo.perldata' or die "Cannot open file > ($!)"; print $fout Data::Dumper->Dump([\%game], ['*game']); > close $fout or die "Cannot open file ($!)"; > [/code] > > [code] > #Code for reading the data structure. Please note that, I have > disabled 'strict' and 'warnings' > > #use strict; > #use warnings; > use Data::Dumper; > > open my $fin, '<', 'gameinfo.perldata' or die "Cannot open file ($!)"; > undef $/; #read in file all at once > eval <$fin>; > if($@) { > die "Can't recreate game data from gameinfo.perldata $@"; > } > close $fin or die "Cannot open file ($!)"; > > print "Name : ", $game{ipl}->{players}->[1]->{name}, "\n"; > print "Role : ", $game{ipl}->{players}->[1]->{role}, "\n"; > print "Country : ", $game{ipl}->{players}->[1]->{country}, "\n"; > [/code] > > I have also tried using 'do' using strict but in vain > > [code] > #use strict; > #use warnings; > use Data::Dumper; > > do "gameinfo.perldata" or die "Can't recreate gameinfo: $! $@"; > > print "Name : ", $game{ipl}->{players}->[1]->{name}, "\n"; > print "Role : ", $game{ipl}->{players}->[1]->{role}, "\n"; > print "Country : ", $game{ipl}->{players}->[1]->{country}, "\n"; > [/code] > > [output] > Name : muralidharan > Role : batsman > Country : srilanka > [/output] > > My question is, how to use the above program with strict enabled? > > Thank you. > > best, > Shaji > ------------------------------------------------------------------------------- > Your talent is God's gift to you. What you do with it is your gift > back to God. > ------------------------------------------------------------------------------- -- David Precious ("bigpresh") <dav...@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/