RE: sourcing one perl file from another



Thanks Jeff and David, that did the trick!

Cheers,
Kelvin

-----Original Message-----
From: rwwebs@xxxxxxxxx [mailto:rwwebs@xxxxxxxxx] On Behalf Of Jeff Pang
Sent: Monday, 22 October 2007 10:21 PM
To: kc
Cc: beginners@xxxxxxxx
Subject: Re: sourcing one perl file from another

There are some ways to 'source' a config file like under unix shell.
Just show 3 ways below:


#########################
# the first way
#########################
The first,you can just require a file,because this file didn't be declared
as a package,so it doesn't has its own namespace,so all variables in this
file can be imported into main package's space.

$ cat mydata.pl
use strict;
our (@key1,@key2);
$key1[64]="0xc120718a1ccce7f8";
$key2[64]="0xeadf28cb82020921";
$key1[128]="0xaf503224b6cff0639cf0dc310a4b1277";
$key2[128]="0x3e1fcbd4e91ca24bb276914de3764cdf";
1;

$ cat usedata.pl
require 'mydata.pl';
print $key1[64];


#########################
# the second way
#########################
The second way,you can declare the config file as a package,and export the
needed varibles.When main script use this package,it import those variables
automatically.

$ cat mydata2.pm
package mydata2;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our (@key1,@key2);
our @EXPORT = qw(@key1 @key2);

$key1[64]="0xc120718a1ccce7f8";
$key2[64]="0xeadf28cb82020921";
$key1[128]="0xaf503224b6cff0639cf0dc310a4b1277";
$key2[128]="0x3e1fcbd4e91ca24bb276914de3764cdf";

1;

$ cat usedata2.pl
use mydata2;
print $key1[64];


#########################
# the third way
#########################
Both the first way and the second way are not good.Because your config file
is large,the former ways have imported all those large content into your
main script.If your main script is run under cgi/modperl which is generally
multi-process,your memory could be eated quickly.So the best way is to
create an object then multi-process can share this object,since object is
only located in its own namespace.

$ cat mydata3.pm
package mydata3;
use strict;

sub new {
my $class = shift;
my (@key1,@key2);

$key1[64]="0xc120718a1ccce7f8";
$key2[64]="0xeadf28cb82020921";
$key1[128]="0xaf503224b6cff0639cf0dc310a4b1277";
$key2[128]="0x3e1fcbd4e91ca24bb276914de3764cdf";

bless {key1=>\@key1,key2=>\@key2},$class;
}

1;

$ cat usedata3.pl
use mydata3;
my $d = mydata3->new;
print $d->{key1}->[64];


Good luck with it!


On 10/22/07, kc <yskchu@xxxxxxxxx> wrote:
Hi Perl gurus,

I've a question, and I was wondering if anyone could

I've a perl program, and I'm trying to make it tidier.

I have a huge file of a array of variables, for initialization,
something like this:

$key1[64]="0xc120718a1ccce7f8";
$key2[64]="0xeadf28cb82020921";
$key1[128]="0xaf503224b6cff0639cf0dc310a4b1277";
$key2[128]="0x3e1fcbd4e91ca24bb276914de3764cdf";

etc etc

Currently, they're all in the huge perl script file as the perl code
that uses it. I was hoping to separate this out to another file.


.



Relevant Pages

  • Re: sourcing one perl file from another
    ... There are some ways to 'source' a config file like under unix shell. ... $ cat usedata.pl ... I've a perl program, and I'm trying to make it tidier. ...
    (perl.beginners)
  • Re: Dumb printing question
    ... If you want to split the output and both drive the printer and save a file copy then look at the jetdirectprint script.. ... # cat jetdirectprint ... open a socket to the printer. ... My PERL is nonexistent, so don't ask me for syntactically correct PERL. ...
    (comp.os.linux.misc)
  • Using OOP to load a config file
    ... I'm fairly new to OOP in Perl and trying my hand to load a config file ... Perl module: CfgLoader.pm ...
    (comp.lang.perl.misc)
  • Using OOP to load a config file
    ... I'm fairly new to OOP in Perl and trying my hand to load a config file ... Perl module: CfgLoader.pm ...
    (comp.lang.perl.misc)
  • Re: Regexp, Strings and spaces
    ... > to do that, without lookbehinds, that's it. ... Are you sure you are using Perl for this? ... myself (that is, putting a regular expression in a config file), but I ... Then you could just set your regular expression to be: ...
    (comp.lang.perl.misc)