免费注册 查看新帖 |

Chinaunix

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

the difference between @_ and shift [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-08 15:23 |只看该作者 |倒序浏览
有的时候用shift,有的时候用@_,搞不明白???
例如:
sub ff
{
        my ($reff1, $reff2)=@_; #the difference between @_ and shift
  for ($index=0;$index<=$#$reff1; $index++) {
          $result[$index]=$reff1->[$index]+$reff2->[$index];
  }
为啥改为shift之后就不好用了呢,他俩的本质区别在哪呢??当然还有$_,差点忘了。
谢谢了

[ 本帖最后由 joson12345 于 2006-5-8 15:55 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-05-08 19:14 |只看该作者
sub ff
{
        my ($reff1, $reff2)=@_; #the difference between @_ and shift
  for ($index=0;$index<=$#$reff1; $index++) {
          $result[$index]=$reff1->[$index]+$reff2->[$index];
  }


这里也可以写成:
my ($reff1, $reff2)=(shift,shift);

或下面的循环直接写成:
$result[$index]= shift->[$index]+ shift->[$index];

不会有问题。

论坛徽章:
0
3 [报告]
发表于 2006-05-08 19:18 |只看该作者
也就是说,在子程序获得数据方面,@和shift没什么区别呀?

论坛徽章:
0
4 [报告]
发表于 2006-05-08 19:59 |只看该作者
原帖由 joson12345 于 2006-5-8 19:18 发表
也就是说,在子程序获得数据方面,@和shift没什么区别呀?

shift也就是shift @_;它从@_这个数组里弹出数据。

论坛徽章:
0
5 [报告]
发表于 2006-05-08 21:17 |只看该作者
这两个(shift  and @_)只要使用其中的一个就可以了,或者两者都用效果相同?
例如:
sub sss
{
   print shift @_;  #  or shift;   or @_;
}

结果应该是一样的,@_and shift 之间是不是可以相互替换?

论坛徽章:
0
6 [报告]
发表于 2006-05-08 21:47 |只看该作者
from 'perldoc perlsub' and search for '@_' :

       Any arguments passed in show up in the array @_.  Therefore, if you
       called a function with two arguments, those would be stored in $_[0]
       and $_[1].  The array @_ is a local array, but its elements are aliases
       for the actual scalar parameters.  In particular, if an element $_[0]
       is updated, the corresponding argument is updated (or an error occurs
       if it is not updatable).  If an argument is an array or hash element
       which did not exist when the function was called, that element is cre-
       ated only when (and if) it is modified or a reference to it is taken.
       (Some earlier versions of Perl created the element whether or not the
       element was assigned to.)  Assigning to the whole array @_ removes that
       aliasing, and does not update any arguments.

from 'perodoc -f shift' :

       shift ARRAY
       shift   Shifts the first value of the array off and returns it, short-
               ening the array by 1 and moving everything down.  If there are
               no elements in the array, returns the undefined value.  If
               ARRAY is omitted, shifts the @_ array within the lexical scope
               of subroutines and formats, and the @ARGV array at file scopes
               or within the lexical scopes established by the "eval ''",
               "BEGIN {}", "INIT {}", "CHECK {}", and "END {}" constructs.


I don't like to discourage the newbe as you,but I really would not rather see someone ask a basal problem stiffly here before he could read some possible documents at first.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP