Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do i interpolate a variable in a regular expression?

by sanjay nayak (Sexton)
on Sep 27, 2006 at 06:44 UTC ( #575091=perlquestion: print w/replies, xml ) Need Help??

sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:

Suppose $method =INVITE.
my %config = ( URI => { regex => '^$method\ssip:', sub => \&request_line }, )
It doesn't interpolate the variable $method?
Suggest me some code.

Replies are listed 'Best First'.
Re: How do i interpolate a variable in a regular expression?
by davido (Cardinal) on Sep 27, 2006 at 06:52 UTC

    Single quotes don't interpolate. Double quotes do. For example, my $not_interpolated = '$this'; won't interpolate, but my $interpolated = "$this"; will.

    But you may find it helpful to use qr// (as discussed in perlop). ...used as follows:

    my %config = ( URI => { regex => qr/^$method\ssip:/, sub => \&request_line, }, );

    Dave

      Hi
      Thanks a lot.
Re: How do i interpolate a variable in a regular expression?
by reneeb (Chaplain) on Sep 27, 2006 at 06:54 UTC
    You should use the qr-operator:

    my %config = ( URI => { regex => qr/^$method\ssip:/, sub => \&request_line }, )


    see perldoc perlop
Re: How do i interpolate a variable in a regular expression?
by chargrill (Parson) on Sep 27, 2006 at 06:54 UTC

    For starters, your $scalar is in single quotes, which are notorious for not inerpolating variables.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2022-06-28 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (91 votes). Check out past polls.

    Notices?