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

Probably a simple one but I can't find reference to it here or in my Perl book!

I'm playing around with packages and modules for the first time and I'm trying to 'import' a path (as a variable) into a package but just don't seem to be able to get the hang of it.

Imagine if you will the following...

script.cgi <---This ties everything together.
variables.pl <---This defines the variables.
package.pl <---The package I'm using.
data.dat <---A data file that I want to read from package.pl

Now, script.cgi accesses variables.pl and gets the path to data.dat "/path/to/datafiles". Script then tells package.pl to access data.dat but package.pl is 'forgetting' what /path/to/datafiles is!

I'm assuming it is possible to make package.pl 'remember' what variables.pl is telling it, but I just can't seem to find the right combination of code to achieve this.

Hopefully someone out there knows?

Thanks
Floyd

Replies are listed 'Best First'.
Re: Using variables within packages
by nite_man (Deacon) on May 07, 2003 at 16:47 UTC
      Aha!
      I finally worked it out!
      I was using the ordinary name for the variable (ie. $path) when I should have used $main::path! I also understand that I could use $::path, but haven't tried it!
      Thanks everyone!
      Floyd
Re: Using variables within packages
by Maddingue (Sexton) on May 07, 2003 at 16:47 UTC

    It depends on how you are calling package.pl from script.cgi. And if you have defined a namespace inside package.pl, it may not directly access the other variables unless you use their full name ($main::var).

    In fact you are using variables.pl as a configuration file, right? Maybe it would be more clear and more safe to use a plain text configuration file and read it with Config::Simple.

Re: Using variables within packages
by tcf22 (Priest) on May 07, 2003 at 16:55 UTC
    From what I can gather script.cgi requires variables.pl and package.pl. If this is the case I think you may want to use 'our'.

    In variables.pl you would use
    our $Path = '/path/to/datafiles';
    In package.pl would just have
    our $Path;
    package.pl should then see the variable $Path that was defined in variables.pl.
      I was under the impression that our is from v5.6.0? What would happen if someone was running an older version?

      Floyd

        Have you tried
        use vars qw( $Path );
        Putting this in variables.pl and package.pl should allow you to use the variable $Path in both scripts with older versions of perl.
Re: Using variables within packages
by Coplan (Pilgrim) on May 07, 2003 at 16:31 UTC
    I don't want to sound like a grumpy monk, but I'm not exactly sure what you're doing wrong. Do you have any code to provide us? We can start by looking at that.(and if it's a lot of code, don't forget the readmore tag)

    --Coplan

      OK

      Firstly, I have used the AUTOLOAD code form Perl Black Book

      package Autoload; BEGIN { use Exporter(); @ISA = qw(Exporter); @EXPORT = qw(AUTOLOAD); } sub AUTOLOAD() { my $subroutine = $AUTOLOAD; $subroutine =~ s/.*:://; $callme = 'Genesis::'.$subroutine; &{$callme}; } END {} return 1;

      Then I'm using a package that I have called Genesis to read a datafile...

      package Genesis; BEGIN { use Exporter(); @ISA = qw(Exporter); @EXPORT = qw(&readdatafile); } ################## sub readdatafile { ################## open(FILE, "$datadir/data.dat" ); @thedata = <FILE>; close(FILE); } END {} return 1;

      variables.pl actually reads a data file for the different values, but for example, assume this simply reads...

      $datadir = "path/to/datafiles"; return 1;

      Finally, script.cgi 'requires' variables.pl, 'uses' Autoload and then executes 'readdatafile();'

      That is roughly that, so hopefully it will give you the idea of what I'm trying to do?

      Floyd