#!/usr/bin/perl -w $|++; use strict; use Benchmark; # # not so elegant but still the chanp. sub getpropstr { local $_ = shift; my $level = shift || 0; if ( $level == -1 ) { /([^()]+)\)+$/o; return $1 } while ( $level-- > 0) { chop; s/^[^(]+\(//o; } return $_; } my @data = ; chomp @data; my $count = 7000; timethese ( $count, { 'my best: getpropstr' => sub { for (@data) { getpropstr( split ); } }, 'OP best: get_proparg_new' => sub { for (@data) { get_proparg_new( split ); } }, 'orig-arg2' => sub { for (@data) { arg2( split ); } }, 'yours' => sub { for (@data) { arg3( split ); } }, } ); sub arg2 { my($str,$lvl)= @_; my $str1 = $str; return $str if !$lvl; if ( $lvl == -1 ) { $str =~ /\(([^\(\)]+)\)+/; return $1; } $str =~ /([^(]*\(){$lvl}(.+)(\)){$lvl}/; return $2; } sub arg3 { my($str,$lvl)= @_; my $str1 = $str; return $str if !$lvl; if ( $lvl == -1 ) { $str =~ /\(([^\(\)]+)\)+/; return $1; } $str =~ /\A (?> (?> [^(]* \( ) {$lvl} ) ( .+ ) \) {$lvl} \z/x; return $1; } # the original poster's improved version sub get_proparg_new { my $propstr = shift; # get the property string my $level = shift || return $propstr; # get the level we want to extract my $cnt; # initialize counter if($level == -1) { # special case, get the innermost argument $propstr =~ /\(([^\(\)]+)\)+/o; return $1; } else { # get whatever argument $level indicates for($cnt = 0;$cnt<$level; $cnt++) { $propstr =~ /\((.+)\)/o; $propstr = $1; } return $propstr; }