Re: Help on saving and retrieving data structures
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/
Re: Help on saving and retrieving data structures
Hi Shaji, On Sat, 14 Sep 2013 16:19:26 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: 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. One should note that JSON is not the only sane option for serialisation and deserialisation in Perl. There are also: * https://metacpan.org/release/Storable - a binary format that is specific for Perl, which works very well. Using it avoids some JSON-specific limitations such as no references to scalars. Make sure you use nstore and friends instead of store. * http://blog.booking.com/the-next-sereal-is-coming.html - there's also this, but I have no experience with it. * There are other formats such as YAML, but YAML should be avoided because it's too complex and quirky (at least for serialisation). Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Optimising Code for Speed - http://shlom.in/optimise Whitespace in Python is not a problem: just lay out all the whitespace first, then add the code around it. — sizz on Freenode’s #perl Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
++Storable On 14 September 2013 09:39, Shlomi Fish shlo...@shlomifish.org wrote: Hi Shaji, On Sat, 14 Sep 2013 16:19:26 +0800 (SGT) *Shaji Kalidasan* shajiin...@yahoo.com wrote: 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. One should note that JSON is not the only sane option for serialisation and deserialisation in Perl. There are also: * https://metacpan.org/release/Storable - a binary format that is specific for Perl, which works very well. Using it avoids some JSON-specific limitations such as no references to scalars. Make sure you use nstore and friends instead of store. * http://blog.booking.com/the-next-sereal-is-coming.html - there's also this, but I have no experience with it. * There are other formats such as YAML, but YAML should be avoided because it's too complex and quirky (at least for serialisation). Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Optimising Code for Speed - http://shlom.in/optimise Whitespace in Python is not a problem: just lay out all the whitespace first, then add the code around it. — sizz on Freenode’s #perl Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Yes, I agree. Top answer! Can also consider YAML if you wanted to do a lot of manual editing of data, but probably JSON is best. Dr Jimi C Wills David Precious wrote in message news:20130913133147.0b88fbeb@columbia... 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/linkedinwww.preshweb.co.uk/facebook www.preshweb.co.uk/cpanwww.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/
Help on saving and retrieving data structures
Dear Perlers, 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 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. ---
Re: Help on saving and retrieving data structures
On Fri, Sep 13, 2013 at 07:10:58PM +0800, *Shaji Kalidasan* wrote: Dear Perlers, 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 This message means you haven't declared the game hash. In the first section of code you declare the game hash and populate it. In the second section of code you haven't decared the game hash at all. You can declare it using my %game; Then you actually have to populate it with the data you have read from the file before you can get any useful output. You might want to read up on autovivication - particularly with reference to hashes. ( http://en.wikipedia.org/wiki/Autovivification ) regards L. 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. --- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Hi Lesley, Thanks for the explanation and pointing me to further resources (Wikipedia). best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- From: 'lesleyb' lesl...@herlug.org.uk To: beginners@perl.org Sent: Friday, 13 September 2013 5:26 PM Subject: Re: Help on saving and retrieving data structures On Fri, Sep 13, 2013 at 07:10:58PM +0800, *Shaji Kalidasan* wrote: Dear Perlers, 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 This message means you haven't declared the game hash. In the first section of code you declare the game hash and populate it. In the second section of code you haven't decared the game hash at all. You can declare it using my %game; Then you actually have to populate it with the data you have read from the file before you can get any useful output. You might want to read up on autovivication - particularly with reference to hashes. ( http://en.wikipedia.org/wiki/Autovivification ) regards L. 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. --- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Help on saving and retrieving data structures
Hi Jimi, Thanks for your explanation and pointing me exactly where I am going wrong. best, Shaji --- Your talent is God's gift to you. What you do with it is your gift back to God. --- From: Dr Jimi-Carlo Bukowski-Wills jbwi...@staffmail.ed.ac.uk To: *Shaji Kalidasan* shajiin...@yahoo.com Sent: Friday, 13 September 2013 5:12 PM Subject: Re: Help on saving and retrieving data structures Hi It’s because the output file has no (my) in it and you haven’t declared the variable scope in the code that reads the data. Try adding my %game; before the line eval $fin; The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
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/linkedinwww.preshweb.co.uk/facebook www.preshweb.co.uk/cpanwww.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/
Re: Help on saving and retrieving data structures
On Fri, Sep 13, 2013 at 01:31:47PM +0100, David Precious wrote: 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). A shame we can't rate answers on beginners but that is an excellent answer and really informative. Thanks David. snip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/