frodo2014 has asked for the wisdom of the Perl Monks concerning the following question:

As the title suggests, I (think) I know what I'm doing wrong, but I haven't got the foggiest idea how to fix it. I humbly present my .pl :
#!/usr/bin/perl use 5.014; use strict; use warnings; use autodie; use AppConfig qw(:all); use Data::Dumper; $AppConfig::configFile='/home/john/tmp/config.xml'; my %config=getAPPConfig();
and my .pm
#!/usr/bin/perl package AppConfig; use 5.014; use strict; use warnings; use autodie; use Exporter qw(import); # Module specific imports use XML::Twig; our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT_OK = qw(getAppConfig); our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, ); our $configFile; sub getAppConfig { my (%config,$twig,$root); say $AppConfig::configFile; $twig = XML::Twig->new(); $twig->parsefile->($AppConfig::configFile); $root = $twig->root; $config{'ppSK'}=$root->first_child('pp')->first_child('ppSK')->text; return \%config; } # End..... 1;
Now... when I run it, I get this....
/home/john/tmp/config.xml Use of uninitialized value in -f at /usr/share/perl5/XML/Twig.pm line +697. Couldn't open : No such file or directory at AppConfig.pm line 30 at AppConfig.pm line 30
So the variable scope seems to be ok because "say $AppConfig::configFile;" outputs.... but then the same variable passed to XML::Twig seems to cause all hell to break loose. So here I am, submitting myself to the lions as another newbie to be devoured.

Replies are listed 'Best First'.
Re: Another noob making a mess of variable scope....
by toolic (Bishop) on Feb 17, 2014 at 15:23 UTC
    The problem has nothing to do with scope. You are calling parsefile incorrectly. Change:
    $twig->parsefile->($AppConfig::configFile);

    to:

    $twig->parsefile($AppConfig::configFile);
Re: Another noob making a mess of variable scope....
by tobyink (Canon) on Feb 17, 2014 at 16:04 UTC

    As well as what toolic said, your function is returning a reference to a hash (i.e. a scalar), but when you call your function you assign it to a hash.

    Either replace return \%config with return %config, or replace my %config=getAPPConfig() with my %config=%{getAPPConfig()} (but don't do both).

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Another noob making a mess of variable scope....
by frodo2014 (Initiate) on Feb 17, 2014 at 21:09 UTC
    Thanks all !
Re: Another noob making a mess of variable scope....
by walto (Pilgrim) on Feb 18, 2014 at 17:28 UTC
    If you want a more object oriented approach you can modify App::Config like this
    #!/usr/bin/perl # # use strict; use warnings; package AppConfig; use 5.014; use strict; use warnings; use autodie; use Carp; use Exporter qw(import); # Module specific imports use XML::Twig; our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT_OK = qw(getAppConfig); our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, ); sub new { my $class = shift; my $self = {}; my %passed_params = @_; croak "Please provide filename\n" unless (defined $passed_params{f +ilename}); $self->{filename}= $passed_params{filename}; bless( $self, $class ); return $self; } sub getAppConfig { my ($self) = @_; my ( %config, $twig, $root ); $twig = XML::Twig->new(); $twig->parsefile->( $self->{filename} ); $root = $twig->root; $config{'ppSK'} = $root->first_child('pp')->first_child('ppSK')->text; return \%config; } # End..... 1;

    And use the module like this
    #!/usr/bin/perl use 5.014; use strict; use warnings; use autodie; use AppConfig qw(:all); use Data::Dumper; my $app_config = AppConfig->new('filename' => '/home/john/tmp/config.x +ml'); my %config=getAPPConfig();

Re: Another noob making a mess of variable scope....
by fishmonger (Chaplain) on Feb 17, 2014 at 16:18 UTC

    In addition to the already mentioned issues, you've declared $configFile but never assigned it a value so the $twig->parsefile statement won't have a file to parse.

    Oops, I missed the assignment statement in the script. Forget what I said.