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

Hi Perl Monks,
Just a quick question for you gurus, can anyone tell me how I can use strict in my script, yet still import a hash using the require statement?

Basically I have include.pl which includes a %SETTINGS hash. I want to use this in script.pl. However, use strict requires that I scope all my variables in script.pl... so how can I get around this? If I scope in script.pl

I.e. my %SETTINGS;

The include doesn't work properly, however if I scope in include.pl it doesn't work either.

What can I do?

Thanks,
Tom

Replies are listed 'Best First'.
Re: use strict and require
by dragonchild (Archbishop) on Mar 12, 2003 at 22:42 UTC
    This can be seen as a possible springboard to modules. Convert include.pl to include.pm as such:
    package include; use vars qw(@ISA @EXPORT) use Exporter; @ISA = qw(Exporter); @EXPORT = qw( %SETTINGS ); # Your stuff here 1;
    Now, instead of require "include.pl";, you do use include; and it should work. (Note the untested clause in my sig.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: use strict and require
by huguei (Scribe) on Mar 12, 2003 at 22:35 UTC
    see "perldoc -f our" (our %SETTINGS;)

    huguei
Re: use strict and require
by Cabrion (Friar) on Mar 13, 2003 at 00:14 UTC
    Use the "no strict" pragma in a block;
    use strict; { no strict; require "hash.file"; }
    You may find it easier to use one of the config modules from CPAN though. Something like Config::Simple would probably work and it would make sessing configuration variable easier on those that don't understand Perl.
Re: use strict and require
by perrin (Chancellor) on Mar 12, 2003 at 22:47 UTC
    There's a discussion about this here.
Re: use strict and require
by virtualsue (Vicar) on Mar 13, 2003 at 10:59 UTC
    An easy way to do this is to rename include.pl to include.pm, making it into a simple module, and declare the variables you want to use in script.pl in a "use vars" inside include.pm. E.g.
    include.pm:
    #!/usr/local/bin/perl use warnings; use strict; use vars qw(%SETTINGS); $SETTINGS{name}="bob dobbs"; $SETTINGS{type}="surreal"; 1; # modules need to end with a true value
    script.pl:
    #!/usr/bin/perl use warnings; use strict; use include; print "$_ : '$SETTINGS{$_}'\n" for (keys %SETTINGS);
    This prints
    name : 'bob dobbs' type : 'surreal'