function return value in a regexpr

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonas

    function return value in a regexpr


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

  • Gunnar Hjalmarsson

    #2
    Re: function return value in a regexpr

    Jonas wrote:[color=blue]
    > How can I use a function return value in a regexpr?
    >
    > $tmp=~s/mysearch/myfunction($1)/;
    > sub myfunction {
    > return $v;
    > }[/color]

    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

    Comment

    • nobull@mail.com

      #3
      Re: function return value in a regexpr

      Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<FelSc.100 519$dP1.348987@ newsc.telia.net >...[color=blue]
      > Jonas wrote:[color=green]
      > > How can I use a function return value in a regexpr?
      > >
      > > $tmp=~s/mysearch/myfunction($1)/;
      > > sub myfunction {
      > > return $v;
      > > }[/color]
      >
      > You can either use the /e modifier, or you can do
      >
      > $tmp =~ s/mysearch/${\ myfunction($1) }/;[/color]

      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.

      Comment

      • Gunnar Hjalmarsson

        #4
        Re: function return value in a regexpr

        nobull@mail.com wrote:[color=blue]
        > Gunnar Hjalmarsson wrote:[color=green]
        >> Jonas wrote:[color=darkred]
        >>> How can I use a function return value in a regexpr?
        >>>
        >>> $tmp=~s/mysearch/myfunction($1)/;
        >>> sub myfunction {
        >>> return $v;
        >>> }[/color]
        >>
        >> You can either use the /e modifier, or you can do
        >>
        >> $tmp =~ s/mysearch/${\ myfunction($1) }/;[/color]
        >
        > 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.[/color]

        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

        Comment

        Working...