Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: perl module...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 7 Topic 11015 of 11509
Post > Topic >>

Re: perl module?

by peng.kyo@[EMAIL PROTECTED] (J. Peng) May 2, 2008 at 04:32 PM

On Fri, May 2, 2008 at 2:54 PM, Levente Kovacs <leventelist@[EMAIL PROTECTED]
>
wrote:
> Ok, I've reached the level, when I want to do perl modules. I've seen
>  tutorials with the `h2xs' approach, but the problem is that I don't
know what
>  it EXACTLY does.

Try this simple one:
http://search.cpan.org/~rjbs/Module-Starter-1.470/lib/Module/Starter.pm

>
>  I'd like to write a code shared among several simple scripts, as a
NON-OO
>  module, used in single program packege. I'd be happy with some #include
style
>  in C, but I don't know how to do that really.
>

I have replied this similiar question on this list, just copy it here.

You can 'source' or 'include' another perl file from the current one.
The ways are like:


#########################
# 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 im****ted into main package's space.

$ cat mydata.pl
use strict;
our (@[EMAIL PROTECTED]
);

$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 ex****t
the needed varibles.When main script use this package,it im****t those
variables automatically.

$ cat mydata2.pm
package mydata2;
use strict;
require Ex****ter;
our @[EMAIL PROTECTED]
 = qw(Ex****ter);
our (@[EMAIL PROTECTED]
);
our @[EMAIL PROTECTED]
 = qw(@[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
);


$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 im****ted 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 the
object if this object was not changed later,since object is only
located in its own namespace.

$ cat mydata3.pm
package mydata3;
use strict;

sub new {
   my $class = ****ft;
   my (@[EMAIL PROTECTED]
);


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


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

-- 
J. Peng - Peng.Kyo@[EMAIL PROTECTED]
 Chinese Squid sup****ts
http://SquidCN.spaces.live.com/
 




 7 Posts in Topic:
perl module?
leventelist@[EMAIL PROTEC  2008-05-02 08:54:47 
Re: perl module?
noreply@[EMAIL PROTECTED]  2008-05-02 10:22:54 
Re: perl module?
peng.kyo@[EMAIL PROTECTED  2008-05-02 16:32:32 
Re: perl module?
leventelist@[EMAIL PROTEC  2008-05-02 16:51:14 
Re: perl module?
noreply@[EMAIL PROTECTED]  2008-05-02 18:33:07 
Re: perl module?
leventelist@[EMAIL PROTEC  2008-05-02 19:26:49 
Re: perl module?
peng.kyo@[EMAIL PROTECTED  2008-05-03 01:33:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 23 21:04:38 CDT 2008.