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

I have a number of perlscripts which have a number of variable names in common, such as a html string called footer which prints copyright stuff at the bottom of html page. Ive decided to strip out all these vars and store them in one big file called var.pl . So lets just say var.pl contains one line, which is
$footer = font({face => "arial", size => "2"},"Copyright 2002");
Now I can use this var by including the line
require "var.pl";
in the calling function, but i also have to comment out -> use strict; in order for this to work without any errors.
How can i get the script to work again with strict included.

Replies are listed 'Best First'.
Re: strict and require.
by davorg (Chancellor) on Jan 02, 2002 at 21:42 UTC

    You'll need to make $footer into a package variable. Add the line

    use vars qw($footer);

    to the top of vars.pl and remove any my declarations of $footer.

    Of course, the best solution would be to make a MyVars.pm module.

    Update: Ignore me, robin's right (as usual). I'm talking nonsense. I blame the Euro.

    The module idea is still the best way to go tho'.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I think you really need to add
      use vars qw($footer);
      to your main program. Because strictness is lexically scoped, it won't apply to vars.pl anyway, so it's not a problem there.

      Then you can use the package variable $footer from the main program. Another possibility is to put an explicit package declaration into vars.pl (but don't use package vars; because that name is used by the use vars pragma.)

      package Vars; $footer = qw(...);
      then you can require vars.pl, and refer to the footer as $Vars::footer from your main script.
      Ok, i added the line but this gives my the error
      Global symbol "$footer" requires explicit package...(for the calling function),
      Again when i comment out strict it works.
      Note: my var.pl file contains nothing but the line you suggested and the definition of $footer...and <bold>doesnt</bold> contain the line
      #!/vars/bin/perl -Tw

        Maybe you could show us the code. I find that's a little more precise than vague descriptions :)

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: strict and require.
by merlyn (Sage) on Jan 03, 2002 at 00:05 UTC
    A more sane way might be to jump directly into module-izing the variables. Put this into MyVars.pm somewhere along your @INC path:
    package MyVars; use base Exporter; @EXPORT = qw($footer $header); # etc use CGI qw(font); # etc $footer = font(...); $header = font(...);
    then in the main code:
    use MyVars qw($footer);
    The default will pull in everything from @EXPORT.

    -- Randal L. Schwartz, Perl hacker

Re: strict and require.
by flocto (Pilgrim) on Jan 03, 2002 at 02:30 UTC
    I do know that this is bad style and I wouldn't do it myself, but this should work for a quick hack like this appears to be:
    Include this in your main program:
    use vars qw/$foot $head/; require Defaults;

    and let your Defaults.pm be something like:
    package Defaults; $::head = 'foo'; $::foot = 'bar';

    If you want to have a "good"solution please use merlyn's suggestion!
    -octo-
    --
    GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+
Re: strict and require.
by uwevoelker (Pilgrim) on Jan 03, 2002 at 13:51 UTC
    This works (tested):
    #!/usr/bin/perl -w use strict; use vars qw($data); require "load_configfile_data.pl"; print $data;
    ... and in load_configfile_data.pl this:
    $data = "Test erfolgreich."; 1;
    Good bye, Uwe
      Ok Guys, Success, and i wrote my first module in the process. Altough i did have to add the line
      use lib '.';
      even though i presumed the . directory was always stored in @INC
      Anyway THanks.
        Yes it is. But as the last entry in the lib array. Maybe there was another file with the same name in your @INC path.