Re: function return value in a regexpr

From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 08/11/04


Date: Wed, 11 Aug 2004 21:12:26 GMT

nobull@mail.com wrote:
> Gunnar Hjalmarsson wrote:
>> Jonas wrote:
>>> How can I use a function return value in a regexpr?
>>>
>>> $tmp=~s/mysearch/myfunction($1)/;
>>> sub myfunction {
>>> return $v;
>>> }
>>
>> You can either use the /e modifier, or you can do
>>
>> $tmp =~ s/mysearch/${\ myfunction($1) }/;
>
> The "${\foo()}" interpolation construct is best avoided. It looks
> like foo() should be called in a scalar context but in fact it's
> called in a list context and all but the last value is discarded.
> This is confusing better IMNSO to use "@{[foo()]}" which looks like
> it's a list context.

Well, the OP's example function does not return more than one element,
which made me post that example. But, sure, "@{[foo()]}" works
irrespective of how many elements that are returned.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Relevant Pages