in reply to Variable declaration in 'required' file doesn't work under 'strict'?

Can I not declare variables within a 'required' file when running under 'strict'??

You are being bitten by the unrelated lexical scope of required files. Declare the variables you are using in the config file as our variables, and declare them in the main file with our (or better, use vars) also, prior to the require "config.pl" statement. Since you are using them in two files, shared between them, introducing them to both parties using them is not only polite but sensible, and sometimes required, since our variables are lexically scoped, as are my variables - but our variables can be package globals.

Let's read the documentation of our again:

our associates a simple name with a package variable in the current package for use within the current scope. When use strict 'vars' is in effect, our lets you use declared global variables without qualifying them with package names, within the lexical scope of the our declaration. In this way our differs from use vars , which is package scoped.

Unlike my, which both allocates storage for a variable and associates a simple name with that storage for use within the current scope, our associates a simple name with a package variable in the current package, for use within the current scope. In other words, our has the same scoping rules as my, but does not necessarily create a variable.

I read that as meaning that if you don't declare our variables used elsewhere (in a required file) beforehand, they do not necessarily relate to package globals created in the calling scope after the require - when the scope of the required file is no longer in effect. At least, there's nothing in the docs that would guarantee such a behaviour. Update - see Why is 'our' good?.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Variable declaration in 'required' file doesn't work under 'strict'?
by agianni (Hermit) on Mar 12, 2007 at 01:41 UTC

    Rather than using the vars pragma, I would recommend using package variables:

    In config.pl:

    package MyConfig; our %state_name_for = ( AL => 'Alabama', AK => 'Alaska', AZ => 'Arizona' );

    In your main file:

    require 'config.pl'; for my $abbrev ( keys %MyConfig::state_name_for ){ print $MyConfig::state_name_for{$abbrev}; }

    Using package variables will make your code easier to read, as it will be more obvious where your variables are coming from than if you simply globalize them and will make it less likely that you accidentally update them in your script code.

    Better yet, follow The Damian's advice from Perl Best Practices and avoid using package variables, instead, use subroutines or methods in the package containing the variables to access them:

    package MyConfig; my %state_name_for = ( AL => 'Alabama', AK => 'Alaska', AZ => 'Arizona' ); sub state_name_for{ return \%state_name_for; }

    In your main file:

    require 'config.pl'; for my $abbrev ( keys %{MyConfig::state_name_for} ){ print $MyConfig::state_name_for{$abbrev}; }

    This will prevent you from accidentally updating your config variables from your script.

      Rather than using the vars pragma, I would recommend using package variables:
      In what way are variables declared with the vars pragma not package variables?
      This will prevent you from accidentally updating your config variables from your script.
      It will also prevent you from accessing the keys of the %state_name_for hash, since in your last example it is a lexical scoped my variable not visible in config.pl.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        In what way are variables declared with the vars pragma not package variables?

        Sorry, you're right, the vars pragma does indeed make the variables part of the main package. What I meant to suggest is that making them part of an explicit package other than main makes code more readable.

        it will also prevent you from accessing the keys of the %state_name_for hash, since in your last example it is a lexical scoped my variable not visible in config.pl.

        Again, I wasn't clear that I was suggesting that the MyConfig package is declared in the config.pl file. %state_name_for is declared as lexically scoped precisely to avoid making it available from outside of the package. Instead, package users should use the state_name_for subroutine to access it. This will help coders avoid this pitfall:

        if( $MyConfig::state_name_for{AL} = 'Alaska' ){ print "oops! we just assigned Alaska to the AL key!\n"; }