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

Learning Perl: 4.10. Non-Scalar Return Values

发布时间:2020-12-15 20:56:30 所属栏目:大数据 来源:网络整理
导读:? 4.10. Non-Scalar Return Values A scalar isn't the only kind of return value a subroutine may have. If you call your subroutine in a list context, [ ] it can return a list of values. [ ] You can detect if a subroutine is being evaluated i

Previous Page

Next Page

?

4.10. Non-Scalar Return Values

A scalar isn't the only kind of return value a subroutine may have. If you call your subroutine in a list context,[

] it can return a list of values.

[

] You can detect if a subroutine is being evaluated in a scalar or list context using the wantarray function,which lets you easily write subroutines with specific list or scalar context values.

Suppose you wanted to get a range of numbers (as from the range operator,..) except that you want to be able to count down as well as up. The range operator only counts upward,but that's easily fixed:

    sub list_from_fred_to_barney {
      if ($fred < $barney) {

        # Count upwards from $fred to $barney
        $fred..$barney;
      } else {
        # Count downwards from $fred to $barney
        reverse $barney..$fred;
      }
    }
    $fred = 11;
    $barney = 6;
    @c = &list_from_fred_to_barney; # @c gets (11,10,9,8,7,6)

In this case,the range operator gives us the list from 6 to 11,and reverse reverses the list,so it goes from $fred (11) to $barney (6) just as we wanted.

The least you can return is nothing at all. A return with no arguments will return undef in a scalar context or an empty list in a list context. This can be useful for an error return from a subroutine,signalling to the caller that a more meaningful return value is unavailable.

Previous Page

Next Page

(编辑:李大同)

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

    推荐文章
      热点阅读