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/

Reply via email to