2009年1月15日星期四

[PerlChina] Re: perl标量与列表和散列的一点疑问

我对Perl了解不多,没能领会用子程序封装一下变量与直接定义的变量可以带来节省内
存的好处,希望告知哪里有更多参考资料,有空我也多学习学习,谢谢.
这样做我觉得可读性较好,可以将多组数据封装到一个文件,根据不同的sub去取数
据,
个人觉得处理较复杂的数据及运算不如C/Java之类来的易于理解和传递,也便于维护.


下面代码为Jeff Pang提供
====================================================================
How to share a variable between two Perl scripts?
(or: How to read the variables in a config file into Perl script?)
====================================================================


There are some ways to do it. I may show 3 ways below:


#########################
# the first way
#########################
The first way,you can just require a file,because this file hasn't be
declared as a package,it doesn't have its own namespace.So all global
variables in this file can be imported into main script's namespace.

$ 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 distinctly.When main script uses this package,it
imports 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 enough.For example,
under mod_perl,there may are more than one script in a process.Since
each script imports the same (maybe huge) content from a file,that
wastes the memory.
So the better way is to create an object then all scripts in the same
process can share the object.This can save memory because all variables
tied to the object are located only in their 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];

> -----Original Message-----
> From: perlchina@googlegroups.com
> [mailto:perlchina@googlegroups.com] On Behalf Of yonghua
> Sent: Friday, January 16, 2009 11:04 AM
> To: perlchina@googlegroups.com
> Subject: [PerlChina] Re: perl标量与列表和散列的一点疑问
>
>
>
> --- On Fri, 1/16/09, Ben_Chan@WISTRON.COM
> <Ben_Chan@WISTRON.COM> wrote:
>
> > From: Ben_Chan@WISTRON.COM <Ben_Chan@WISTRON.COM>
> > Subject: [PerlChina] Re: perl标量与列表和散列的一点疑问
> > To: perlchina@googlegroups.com
> > Date: Friday, January 16, 2009, 10:50 AM "There's more than
> one way to
> > do it"
> > 简单易用就好吧,面向对象之类的
> > 都是一个想法而已,C也可以写成面向对象的风格啊
>
>
> 不是想法不想法的问题,而是某些情形下,OO可以节省内存、提高性能。
> 跟老外合作过不少MP项目,这方面深有体会。
> 写过一篇FAQ release在mailing lists上,看看或许有所参考:
>
> http://home.arcor.de/pangj/share_variables_between_perl_scripts.txt
>
>
> Jeff.
>
> --~--~---------~--~----~------------~-------~--~----~
> 您收到此信息是由于您订阅了 Google 论坛"PerlChina 论坛"论坛。
> 要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
> 要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
> 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该
论坛
> -~----------~----~----~----~------~----~------~--~---
>
>

没有评论: