Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > function return value in a regexpr

Reply
Thread Tools

function return value in a regexpr

 
 
Jonas
Guest
Posts: n/a
 
      08-11-2004

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
-----------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2004
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
 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      08-11-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in message news:<FelSc.100519$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: unexpected result with string.split( regexpr ) francisrammeloo@hotmail.com Ruby 1 07-28-2005 12:30 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Regexpr FTP MC Perl 4 02-09-2005 03:23 AM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
regexpr question is w2 taken Richard Bell Perl Misc 3 04-16-2004 11:48 AM



Advertisments