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.
-------------------------------------------------------------------------------