function return value in a regexpr

Discussion in 'Perl' started by Jonas, Aug 11, 2004.

  1. Jonas

    Jonas Guest

    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
    -----------------------------------------------------------------------
     
    Jonas, Aug 11, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Aug 11, 2004
    #2
    1. Advertising

  3. Jonas

    Guest

    Gunnar Hjalmarsson <> wrote in message news:<FelSc.100519$>...
    > 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.
     
    , Aug 11, 2004
    #3
  4. 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
     
    Gunnar Hjalmarsson, Aug 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MC

    Regexpr FTP

    MC, Feb 4, 2005, in forum: Perl
    Replies:
    4
    Views:
    569
    Martin Gregory
    Feb 9, 2005
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    523
    Richard Bos
    Jun 18, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    885
    Keith Thompson
    Mar 6, 2005
  4. Replies:
    1
    Views:
    140
    Austin Ziegler
    Jul 28, 2005
  5. Richard Bell

    regexpr question is w2 taken

    Richard Bell, Apr 15, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    130
    Anno Siegel
    Apr 16, 2004
Loading...

Share This Page