in reply to Trouble working with Config::Simple

Well thinks to everyones help and some tinkering I got it to create the file and write to it, but now I have having trouble getting it to read from the file
sub load_game { $cfg->read("'$ENV{HOME}/.kenesis'"); @player = $cfg->param("player"); @enemy = $cfg->param("enemy"); $completed_quests = $cfg->param("quest"); $special_item = $cfg->param("items"); $class = $cfg->param("class"); #main_menu(); print "@player\n"; print "@enemy\n"; print "$completed_quests\n"; print "$special_item\n"; print "$class\n"; exit; }
I get the following error Can't call method "read" on an undefined value at kenesis.pl line 93, <STDIN> line 1.. The thing is all of my variables are defined
our @player; our $completed_quests = 0; our $special_item = "none"; our @enemy; our $class = "null"; our $cfg;
.
The line that the error is complaining about, line 93, is this line $cfg->read("'$ENV{HOME}/.kenesis'");

Replies are listed 'Best First'.
Re^2: Trouble working with Config::Simple
by parv (Parson) on Jul 09, 2008 at 03:23 UTC

    Quite possibly &load_game is being called before $cfg becomes a Config::Simple object.

    [...] all of my variables are defined
    our @player; our $completed_quests = 0; our $special_item = "none"; our @enemy; our $class = "null"; our $cfg;

    From that snippet, I see some of variables being declared but not defined, namely $cfg. Missing also from the snippet are the stages at which &load_game and &Config::Simple::new are being called.

    $cfg->read("'$ENV{HOME}/.kenesis'")

    What's with two kinds of quotes around file name? Is a file name actually being surrounded by single quotes (on the file system)?

      The variables are defined before everything else, the only reason the the arrays are not defined there is because they are defined later after the class as been selected. the &load_game code comes after everything is defined. The two quotes around the file name $cfg->read("'$ENV{HOME}/.kenesis'") is the only way I was able to get $cfg->read to see the .kenesis file, I'm not sure why I had to though.
      I guess it would be easier for people to review the whole code, I have the project hosted on Google Code and the file can be viewed here. This should help everyone see where everything is at.

        Tell me this: at what point does $cfg get a value from &Config::Simple::new when the program is requested to load a game via load game string coming in from STDIN?

        Between declaration of $cfg variable on line 31 and call to &load_game sub on line 83, $cfg remains undefined. And, what is very first statement of &load_game? It is a call to a(n) (unknown) method read on yet-to-be-defined $cfg. That is the reason for the error message.

        $cfg needs to be defined (even) in &load_game sub before a method could be called on it, much like in the first statement of &new_game sub on line 107. You can do one of at least two things ...

        sub load_game { # No need for separate "$cfg->read( ... )". $cfg = Config::Simple->new( 'file' ); # or ... $cfg = Config::Simple->new; $cfg->read( 'file' ); ... }

        Note that the solution in both forms is already presented in "READING THE CONFIGURATION FILE" section of pod.