in reply to Re: For the life of me, i can't figure out this "use strict" variable problem
in thread For the life of me, i can't figure out this "use strict" variable problem

Ahhh ok. I thought that I would no longer be able to call $cfg once inside the 'unless'.. Well that fixed the problem. Thank you. I still have another problem specific to this module.. maybe youve seen something simular before. here is my current code
#!/usr/local/bin/perl -w use strict; use Config::Simple; my $config_file = shift; if (!$config_file) { $config_file = "etc/allweb-backup.conf"; } my $cfg = new Config::Simple($config_file); my (%Config); unless (%Config = $cfg->vars()) { die "CRITICAL: $config_file is not a valid INI file!\n"; }
I tried the unless statement wrapped around the $cfg line, and that gave me an error.. In the above I wrapped my unless around the %Config line, that also gave the same error.. I guess neither is returning false when it craps out. Any idea how I can get it to die and print my message instead of the message in the module... this is the error message I get when running this code.
Something went wrong. No supported configuration file syntax found at +/usr/local/lib/perl5/site_perl/5.8.2/Config/Simple.pm line 184, <FH> +line 16.

Replies are listed 'Best First'.
Re^3: For the life of me, i can't figure out this "use strict" variable problem
by Golo (Friar) on Oct 24, 2004 at 21:19 UTC
    thats most likely because you cannot call $cfg->vars() if your Config::Simple object could not be created (although, the error message doesn't match to your example code).

    You should check for and handle the error on the line you create the Config::Simple object (no later):
    my $cfg = new Config::Simple($config_file) or die "aaarg $!";
      I gave both of those a try, but didn't seem to have any luck
      my $cfg = new Config::Simple($config_file) || die "ARGG!"; my %Config; eval { %Config = $cfg->vars() }; if ($0) { die "CRITICAL: $config_file is not a valid INI file!\n"; }
      ... any other ideas? Thanks a bunch BTW :-)
        ARGG! at D:\Perl-1.pl line 9, <FH> line 3.
        dies fine for me (suppling a bogus text file as config). Maybe your script dies at a later point. Maybe the problem lies in your supplied config file?

        Take a close look at your config file (line 16 as C::S suggests).
        Try to nail the problem down to the real line using the debugger or by adding checkpoint prints.
        You have made a typing error in the if statement: $0 should be $@
Re^3: For the life of me, i can't figure out this "use strict" variable problem
by Arunbear (Prior) on Oct 24, 2004 at 21:19 UTC
    You need eval:
    eval { %Config = $cfg->vars() }; if($@) { die "CRITICAL: $config_file is not a valid INI file!\n" }
Re^3: For the life of me, i can't figure out this "use strict" variable problem
by Nkuvu (Priest) on Oct 25, 2004 at 11:13 UTC
    Unrelated to your scoping issue (once again it's been answered well by others before this) but there's a Perl idiom for this tidbit of code:
    my $config_file = shift; if (!$config_file) { $config_file = "etc/allweb-backup.conf"; }
    which is a simple: my $config_file = shift || "etc/allweb-backup.conf"; Same results, cleaner code (IMHO).
Re^3: For the life of me, i can't figure out this "use strict" variable problem
by revdiablo (Prior) on Oct 25, 2004 at 19:22 UTC
    I thought that I would no longer be able to call $cfg once inside the 'unless'

    You seem to have figured this out, but I'd just like to reinforce the point. Scalar variables are accessible from inner scopes, but not from outer scopes. So if you scope a variable in the top-level of a subroutine, then any nested blocks (if statements, while loops, etc) will have access to the variable. But anything outside of the subroutine will not. Here's the obligatory code example:

    sub foo { my $foo = 1; if (1 == 1) { # just to demonstrate the point $foo++; # inside foo, works fine } print $foo; bar(); } sub bar { print $foo; # outside foo, blows up } foo();

    Also keep in mind that the inner/outer determination is done "physically". That is, it's not based on the call stack. It's based on the location in the actual code. This is different from dynamic scoping (i.e. local in Perl), which is based on the execution path. For this reason, lexical scoping is much more predictable and understandable. Here's an example:

    sub foo { local $foo = 1; if (1 == 1) { # just to demonstrate the point $foo++; } print "From foo: $foo\n"; bar(); } sub bar { # this variable will be set to whatever it was # before we got called print "From bar: $foo\n"; } $foo = 5; foo(); bar();