为什么 Perl 会抱怨“不推荐使用对 @_ 的隐式拆分"? [英] Why does Perl complain "Use of implicit split to @_ is deprecated"?

查看:33
本文介绍了为什么 Perl 会抱怨“不推荐使用对 @_ 的隐式拆分"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This code triggers the complaint below:

#!/usr/bin/perl 
use strict;
use warnings;

my $s = "aaa bbb";
my $num_of_item = split(/s+/, $s) ;
print $num_of_item;

When I run the code, Perl complains that "Use of implicit split to @_ is deprecated" . I really have no "context" for the problem, so I expect you help to explain what's wrong with the code.

解决方案

You are using split in scalar context, and in scalar context it splits into the @_ array. Perl is warning you that you may have just clobbered @_. (See perldoc split for more information.)

To get the number of fields, use this code:

my @items = split(/s+/, $s);
my $num_of_item = @items;

or

my $num_of_item = () = split /s+/, $s, -1;

Note: The three-argument form of split() is necessary because without specifying a limit, split would only split off one piece (one more than is needed in the assignment).

这篇关于为什么 Perl 会抱怨“不推荐使用对 @_ 的隐式拆分"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆