加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Class::Struct

发布时间:2020-12-15 20:50:59 所属栏目:大数据 来源:网络整理
导读:如果用perl来实现C的数据结构struct,请使用模块Class::Struct. 以下为一简单示例: -------------------------------------------------------------------- use Data::Dumper; use Class::Struct; use IO::File; struct Test ={ ??? s = '$',???????? #定

如果用perl来实现C的数据结构struct,请使用模块Class::Struct. 以下为一简单示例: -------------------------------------------------------------------- use Data::Dumper; use Class::Struct; use IO::File; struct Test =>{ ??? s => '$',???????? #定义使用scalar类型 ??? a => '@',???????? #定义使用array类型 ??? h => '%',???????? #定义使用hash类型 ??? t => 'IO::File'?? #定义使用其它类 }; #struct 也可以用[]来定义,效果一样 $f = IO::File->new("<d:/tmp/test.pl"); $t = Test->new( ??? s=>'this is just a testing',??? a=>[1,2,3],??? h=>{one=>'two',three=>4},??? t=>$f ??? #这里也可以这样初始化 t=>(IO::File->new("<d:/tmp/test.pl")) ); print $t->t->getline; $x = $t->a; push @$x,'testing'; print? Dumper $t 输出结果: -------------------------------------------------------------------- $VAR1 = bless( { ???????????????? 'Test::t' => bless( /*Symbol::GEN1,'IO::File' ),???????????????? 'Test::s' => 'this is just a testing',???????????????? 'Test::a' => [ ??????????????????????????????? 1,??????????????????????????????? 2,??????????????????????????????? 3,??????????????????????????????? 'testing' ????????????????????????????? ],???????????????? 'Test::h' => { ??????????????????????????????? 'three' => 4,??????????????????????????????? 'one' => 'two' ????????????????????????????? } ?????????????? },'Test' ); 上例中如果不对每个域赋值,则结果会为: $t = Test->new; print Dumper $t 输出: $VAR1 = bless( { ???????????????? 'Test::s' => undef,???????????????? 'Test::a' => [],???????????????? 'Test::h' => {} ?????????????? },'Test' ); struct无法定义默认初始值,可以尝试如下定义: struct Test =>{ ??? s? => 'testing',??? a => [1,??? h => '%',??? t => 'IO::File' }; 运行出错,可以自己尝试一下。 运用struct可以实现C中的链表,树结构,自己可以尝试一下。更详细的介绍可以参考perl自带的文档。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读