Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Passing regexp as argument

by loris (Hermit)
on Dec 20, 2004 at 09:47 UTC ( #416142=perlquestion: print w/replies, xml ) Need Help??
loris has asked for the wisdom of the Perl Monks concerning the following question:

Hello wiser ones,

How would I pass a regexp as a parameter to a subroutine? I would like to do something like:

changeName($topNodeOfXmlTree,'s/WrongNode/RightNode/');

and have the function recurse through the XML tree and change, in my case, the node names:

sub changeNodeNames { my ($self,$node,$regEx) = @_; my $nodeName = $node->nodeName; $nodeName =~ $regEx; ...

The above, however, does not work.

Thanks for your help,

loris

Replies are listed 'Best First'.
Re: Passing regexp as argument
by Zaxo (Archbishop) on Dec 20, 2004 at 10:10 UTC

    You are trying to pass a whole substitution expression, not just a regex. You need eval STRING to do what you want, eval "\$nodeName =~ $regEx";
    Note the escaped sigil in $nodeName. That is to prevent the variable from having its value interpolated before evaluation.

    I'd be inclined to pass the regex and substitution as two arguments and interpolate them

    sub changeNodeNames { my ($self,$node,$regEx,$subst) = @_; my $nodeName = $node->nodeName; $nodeName =~ s/$regEx/$subst/; # . . . } $foo->changeNodeNames($anode, qr/foo/, 'bar');

    After Compline,
    Zaxo

Re: Passing regexp as argument
by edan (Curate) on Dec 20, 2004 at 09:56 UTC

    Pass string and the replacement as 2 arguments:

    $obj->changeName($topNodeOfXmlTree, 'WrongNode', 'RightNode'); sub changeNodeNames { my ($self,$node,$from,$to) = @_; my $nodeName = $node->nodeName; $nodeName =~ s/$from/$to/; ... }
    --
    edan

Re: Passing regexp as argument
by BUU (Prior) on Dec 20, 2004 at 16:34 UTC
    The others before me have answered the exact question you asked, namely how you pass a regex, but if you want a slightly more flexible solution (and better than a string eval), you should consider passing a subreference, which you then call to modify node name. For your code above would look like:
    sub change_node_names { my ($self, $node, $code) = @_; $nodeName = $code->( $nodeName ) } $foo->change_node_names( $node, sub { my $name = shift; $name =~ s/wrongnode/rightnode/; return $name } );
    While this requires slightly more effort to call the subroutine, it is much more flexible as you can put literally *any* perl code inside of the annonymous sub, which is what the funnny  sub { } creates.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://416142]
Approved by bart
help
Chatterbox?
[tobyink]: 1nickt: your code?
[LanX]: pryrt: yeah, that's why I didn't consider, but the last >10 anonymous posts are from the same troll-person
[jdporter]: is there a module for expanding tabs in text?
[jdporter]: A: yes.
[Lady_Aleena]: I don't know how hard it will be to get out of that mess.
[LanX]: M-x untabify
[choroba]: I don't think they're multiple people. I was told "he or she" sounds old-fashioned and "they" is the way to say it
[pryrt]: Those others were definitely offensive or unequivocably rude, I agree.
[choroba]: (well, it comes from the 14th century, so labelling it as "modern" doesn't seem appropriate)
[Your Mother]: "They" is becoming accepted but it irritates me sometimes. I tend to just pick she or he randomly or try to use "one."

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (17)
As of 2017-05-24 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?