By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,243 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,243 IT Pros & Developers. It's quick & easy.

function return value in a regexpr

P: n/a

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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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) }/;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2

P: n/a
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<Fe*********************@newsc.telia.net>...
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.
Jul 19 '05 #3

P: n/a
no****@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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.