Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Passing results of a substitution to a function

by Anonymous Monk
on Jan 20, 2003 at 02:01 UTC ( #228269=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm trying to get a regular expression to pass the results of a regular expression substitution into a method argument. Rather than just make a temporary variable and do the substitution on that, and then pass that to the function, is there any way I can do the substitution as part of the argument list, eg:
$word = 'BLAHBLAH'; print blah(s/BLAH/COW/); print "$word\n"; sub blah { my $stuff = shift; print "$stuff\n"; }
I want to keep the blah method as general as possible, and don't want to add the regular expression there. This is purely a style issue (as I think something like this would look much nicer than having to use one-off variables)

Replies are listed 'Best First'.
Re: Passing results of a substitution to a function
by Pardus (Pilgrim) on Jan 20, 2003 at 03:38 UTC
    This can't be done directly since the search and replace functions return the number of matches, not the string.

    But you could do this:
    $word = 'blah'; print my_sub(map {s/blah/cow/; $_} ($word));
    although it _is_ a one-liner it won't get a beauty price either.
    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
Re: Passing results of a substitution to a function
by boo_radley (Parson) on Jan 20, 2003 at 03:43 UTC
    Yes, there is a way you can pass a regex -- as a scalar -- around. See qr// in perlop.
Re: Passing results of a substitution to a function
by Coruscate (Sexton) on Jan 20, 2003 at 03:44 UTC

    You could always use grep. Note that I changed the print() in blah() to a return, otherwise you'll end up printing an extra value out.:

    my $word = 'BLAHBLAH'; print blah(grep{s/BLAH/COW/}$word); sub blah { my $stuff = shift; return "$stuff\n"; }
      This seems to work quite nicely - but needs a small amount of tweaking to make sure grep doesn't return stuff in scalar context.
      Ok - I now have something which works a lot more niftily:
      my $word = 'BLAHBLAH' blah(grep {s/BLAH/COW/ || 1 } $word);
      The { s/BLAH/COW/ || 1 } bit is required as I want to also pass in arguments which do not contain 'BLAH' (which grep would usually.. er.. grep away ;) ). I was worried about list context before because I was trying to get around this by using
      blah(grep(s/BLAH/COW/,$word)||$word)
Re: Passing results of a substitution to a function
by BrowserUk (Patriarch) on Jan 20, 2003 at 12:05 UTC

    Your sample code doesn't make much sense in that you are not applying the s/// to anything.

    You say you want to pass the results of the s/// to the sub, without recourse to a temporary var. If your intent is to apply the s/// to $word, then this requires no additional vars.

    $word = 'BLAHBLAH'; $word =~ s/BLAH/COW/; print blah( $word ); print "$word\n";

    It's not clear to me if this is what you want, but if it's not, maybe you could clarify for me?


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      I want to apply the s/// to $word, but I want to keep the contents of $word as is. It's a one-off non-permanent substitution I'm making (for the purpose of feeding arguments into the method).
Re: Passing results of a substitution to a function
by BrowserUk (Patriarch) on Jan 20, 2003 at 23:37 UTC

    You could do this ... whether you should is for your own concience:)

    sub blah{print +shift;} my $word = 'BLAHBLAH'; blah( do{ (local $_= $word) =~ s/BLAH/COW/g; $_} ); # prints COWCOW print $word; # prints BLAHBLAH

    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Passing results of a substitution to a function
by jepri (Parson) on Jan 20, 2003 at 05:19 UTC
    Intruiging - it works for matches, but not for substitutes.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://228269]
Approved by data64
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-05-31 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?