Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: substitute characters in the RHS of a search & replace

by AnomalousMonk (Archbishop)
on Mar 09, 2013 at 19:35 UTC ( [id://1022616]=note: print w/replies, xml ) Need Help??


in reply to Re^3: substitute characters in the RHS of a search & replace
in thread substitute characters in the RHS of a search & replace

... a generic function to simulate /r ...

Defining functions that take 'naked' subroutine blocks is, IMHO, one of the few valid reasons for using prototypes. However, there's a subtle pitfall here that belies the word 'generic' in the description of the function. The use of the  $ prototype causes behavior that will almost certainly cause puzzlement in a wider, i.e., more generic, context by imposing scalar context on its argument. Better, I think, to use  @ instead.

>perl -wMstrict -le "sub rx (&$) { my $c_regex = shift; local $_ = shift; $c_regex->(); return $_; } ;; my $s = 'a b c'; print rx { s/ /_/g } $s; ;; my @ra = 'p q r'; print rx { s/ /_/g } @ra; " a_b_c 1 >perl -wMstrict -le "sub rx (&@) { my $c_regex = shift; local $_ = shift; $c_regex->(); return $_; } ;; my $s = 'a b c'; print rx { s/ /_/g } $s; ;; my @ra = 'p q r'; print rx { s/ /_/g } @ra; " a_b_c p_q_r

Replies are listed 'Best First'.
Re^5: substitute characters in the RHS of a search & replace
by LanX (Saint) on Mar 09, 2013 at 20:07 UTC
    I think a call on @something should rather generate a warning instead of trying DWIM.

    But no (good) idea how to implement that.

    Maybe better iterate over all arguments and returning a list? something called rxmap maybe?

    Cheers Rolf

      Hmmm...     This is the best I could do, but it's a bit icky for Anonymonk's OPed application:

      >perl -wMstrict -le "sub rx (&@) { return map { local $_ = $_; ()= $_[0]->(); $_; } @_[1..$#_]; } ;; my $s = 'a b c d'; print rx { s/ /_/g } $s; ;; my @ra = ('p q r', 'x y z z y'); print rx { s/ /_/g } @ra; ;; my $x = q{Here are [[a [[ variable]] number [[of ]words]] in brackets}; print qq{'$x'}; ;; $x =~ s( \[\[ (.*?) \]\] )( (rx {s/ /_/g} $1)[0] )xmsge; print qq{'$x'}; " a_b_c_d p_q_rx_y_z_z_y 'Here are [[a [[ variable]] number [[of ]words]] in brackets' 'Here are a_[[_variable number of_]words in brackets'

      Bottom line:  s///r is a lot nicer. Thanks again to whoever thought of that one.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-03-19 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found