(Reading time: 1 minute)

Perl's empty parentheses () idiom, is a list assignment in scalar context which returns the number of elements on the right-hand side of the list assignment.

() is very useful to count the number of matches in a regular expression

 

For example:

 
my $str = 'aa bb cccc';
 
 
my $val = () = $str =~ /\w+/g;
 
 
print $val;
 
 
 

The above snippet will print number of matches in the regexp.

 

Oxygen
Author: Oxygen
Other articles by this author


Chillzee Tag Cloud

Let's Socialize

About Chillzee

Chillzee.com is an entrepreneurship portal.

The site provides informative topics on Organizational and Strategic needs.