本文介绍了为什么 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 withthe 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 inthe assignment).

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

08-24 01:27