+ -
当前位置:首页 → 问答吧 → 不清楚这个shift or return是什么意思

不清楚这个shift or return是什么意思

时间:2010-10-05

来源:互联网

RT!
有点习惯这种写法但是还不知道怎么回事。求指点!!!!


#!/usr/bin/perl

use strict;
use warnings;

main(@ARGV);

sub main
{
    open FH,"linesfile.txt";
    my $linenum=1;

    while(my $line=<FH>){
        print "line $linenum is ".$line;
        $linenum++;
    }
    close FH;
    message("this");
}

sub message
{
    my $m=shift or return;
    print ("$m\n");
}

作者: laohuanggua   发布时间: 2010-10-05

本帖最后由 coldneverend 于 2010-10-05 11:22 编辑

perldoc

shift   Shifts the first value of the array off and returns it,
        shortening 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 outside a
        subroutine and also within the lexical scopes established by the
        "eval STRING", "BEGIN {}", "INIT {}", "CHECK {}", "UNITCHECK {}"
        and "END {}" constructs.

        See also "unshift", "push", and "pop". "shift" and "unshift" do
        the same thing to the left end of an array that "pop" and "push"
        do to the right end.

另外,or的用法在perlop/Logical or, Defined or, and Exclusive Or里

作者: coldneverend   发布时间: 2010-10-05

  1. shift || return
复制代码
一样的,熟悉VBscript, Delphi的人会比较习惯or ,  用C的人习惯我这种。

作者: wind_ch   发布时间: 2010-10-05

本帖最后由 珞水的大叔 于 2010-10-05 13:18 编辑
  1. my $m=shift or return;
复制代码
相当于
  1. my $m;
  2. if($m=shift){}else{return};
复制代码
这句话是用来检查是否能获得函数的第一个输入参数,
如果能,就存放在$m中
如果不能,就return(即结束函数,并返回空)

作者: 珞水的大叔   发布时间: 2010-10-05