Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interpolation in regex

Status
Not open for further replies.

moroshko

Programmer
May 24, 2008
14
IL
I have the following program:

#!/usr/bin/perl
use warnings;
use strict;
my $_ = 'Hello$xWorld';
my $x = 'abc';
print /$x/;

The output is nothing :(

How can I ask Perl not to interpolate $x ?

Thanks in advance !
 
It should print:

Can't use global $_ in "my" at script line 4, near "my $_ " Execution of script aborted due to compilation errors.

Your code has other problems too, like this will be interpreted as a regexp:

print /$x/;

Are you trying to learn how to expand variables in single-quoted constructs/program input? Normally perl does not interpolate variables inside of single-quoted constructs, you should use double-quotes. But you can use a regexp or eval to interpolate variables where perl would not normally do that.

Are you a student?


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
OK, I see from another forum that you wanted to literally find the literal expression $x inside of $_. You discovered how to do that. So I just wasted my time helping you above with the same answers you got on another forum that were not even the point of your question. :(

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, Thank you very much anyway !
I appreciate your wish to help !!

By the way, I'm not a student.
I have a degree in Math+Computer Science, but I never learned Perl.
Now, I'm learning Perl from internet.
I must admit that I like it very much !!!
Tell me please, do you know another Perl forums in which I can get answers fast ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top