function return value in a regexpr

J

Jonas

Dear members,

How can I use a function return value in a regexpr?

$tmp=~s/mysearch/myfunction($1)/;
sub myfunction {
return $v;
}


Thanks in advance,

Jona


-
Jona
 
G

Gunnar Hjalmarsson

Jonas said:
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) }/;
 
N

nobull

Gunnar Hjalmarsson said:
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.
 
G

Gunnar Hjalmarsson

Gunnar said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top