RE: sourcing one perl file from another
- From: yskchu@xxxxxxxxx (Kelvin Chu)
- Date: Mon, 22 Oct 2007 22:40:08 +1000
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.
.
- References:
- sourcing one perl file from another
- From: Kc
- Re: sourcing one perl file from another
- From: Jeff Pang
- sourcing one perl file from another
- Prev by Date: Re: Adding numbers of vector, in hash table
- Next by Date: Re: Dynamically assigned variables
- Previous by thread: Re: sourcing one perl file from another
- Next by thread: Re: sourcing one perl file from another
- Index(es):
Relevant Pages
|