免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4352 | 回复: 19
打印 上一主题 下一主题

请教一个bless对象的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-23 09:54 |只看该作者 |倒序浏览
sub new {  
  my ($class, $genes) = @_;

  my $self;
  if (ref $class) {
    $self = bless {} => ref $class;
    $self->{$_} = $class->{$_} for keys %$class;
    $self->{GENES}  = $genes;
    $self->{CALCED} = 0;

  } else {          # new mode. Genome is given
    goto &newSpecific;
  }

  return $self;
}


还有一个
sub newRandom {
  my ($class, $length) = @_;

  my $self = bless {
                    GENES   => [],
                    SCORE   => 0,
                    FITFUNC => sub {},
                    CALCED  => 0,
                   } => $class;

  push @{$self->{GENES}} => rand > 0.5 ? 1 : 0
    for 1 .. $length;

  return $self;
}


$self = bless 中的=> 号不理解,
push @{$self->{GENES}} => rand > 0.5 ? 1 : 0 这里面的=> 也不理解,有没有高手解答一下了 先谢谢了!

[ 本帖最后由 killer5757 于 2008-1-23 10:47 编辑 ]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2008-01-23 12:58 |只看该作者
=>  equal to ,
please reference to
perldoc perl
       The "=>" operator is a synonym for the comma, but forces any word (con-
       sisting entirely of word characters) to its left to be interpreted as a
       string (as of 5.001).  This includes words that might otherwise be con-
       sidered a constant or function call.

           use constant FOO => "something";

           my %h = ( FOO => 23 );

       is equivalent to:

           my %h = ("FOO", 23);

       It is NOT:

           my %h = ("something", 23);

       If the argument on the left is not a word, it is first interpreted as
       an expression, and then the string value of that is used.

       The "=>" operator is helpful in documenting the correspondence between
       keys and values in hashes, and other paired elements in lists.

               %hash = ( $key => $value );
               login( $username => $password );

[ 本帖最后由 apile 于 2008-1-23 13:00 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-01-23 19:49 |只看该作者
$self = bless 中的=> 号不理解,
push @{$self->{GENES}} => rand > 0.5 ? 1 : 0 这里面的=> 也不理解,有没有高手解答一下了 先谢谢了!


1. =>在perl里就是逗号',', 第一句就等同于bless {},$class,返回一个bless的目标给$self. ref $class表示如果该类已实例化的话,那么就取其类自身.
2. rand是个内置函数吧,如果call该函数的结果大于0.5,就push 1到$self->{GENES}指向的数组里去.否则push 0到这个数组里去.
3. 这个代码写的有点晦涩.另外,现代好的programming style建议不用goto语句.


btw: 好久没用中文,','是句号还是逗号刚才想了半天,

论坛徽章:
0
4 [报告]
发表于 2008-01-23 21:02 |只看该作者
楼上重现江湖.

论坛徽章:
0
5 [报告]
发表于 2008-01-23 21:36 |只看该作者
仙子下凡了!!!!!!
回娘家感觉可好?

论坛徽章:
0
6 [报告]
发表于 2008-01-23 21:39 |只看该作者
原帖由 Nosferatu 于 2008-1-23 21:36 发表
仙子下凡了!!!!!!
回娘家感觉可好?


没回呢..我这边登录CU超级慢,所以平时上得很少.
快过年了,有点想家..借此帖给本版版友和flw拜个早年.
祝2008大家和Perl一同进步!

论坛徽章:
0
7 [报告]
发表于 2008-01-24 00:04 |只看该作者
跟在后面顶~异国他乡,不容易.快过春节了,也不打算回乡?呵呵

论坛徽章:
0
8 [报告]
发表于 2008-01-24 00:08 |只看该作者
原帖由 mouse.rice 于 2008-1-24 00:04 发表
跟在后面顶~异国他乡,不容易.快过春节了,也不打算回乡?呵呵


不着急,刚来不久呵呵.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2008-01-24 00:11 |只看该作者
妹妹在线麽?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
10 [报告]
发表于 2008-01-24 08:35 |只看该作者
仙子可以找找看有没有比较快的Proxy...
一般国际Bandwidth都会有限制...
透过Proxy..会快一点...

题外话:那个"大连理论"..看起来是来捣乱的..

[ 本帖最后由 apile 于 2008-1-24 08:36 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP