http://qs321.pair.com?node_id=575091

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($,,$;,$*,$/)