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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace variable that has backslashes

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Joined
Feb 6, 2002
Messages
1,851
Location
IL
HI,
A simple one.

my $OLDPATH='P:\\prin';
my $NEWPATH='D:\\prin';

while (<FILE>) {
s/$OLDPATH/$NEWPATH/;
}

The replacement fails due to backslashes...
Appreciate any advise.


Long live king Moshiach !
 
You don't need to escape backslashes in a string enclosed by single-quotes. Use either 'P:\prin' or "D:\\prin".
 
Thanks,

I think I have mislead you here.
I'm getting the contents of the variables by reading some file,then it comes with "\\" ...
Ended up using \Q :

s/\Q$OLDPATH/$NEWPATH/;

Thanks

Long live king Moshiach !
 
If they're drive letters, perl is equally at home with "P:/prin" instead of "P:\\prin"

FWIW

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks,

Perl "is at home",however I have to update a certain file in the format it's expected to be ... :)

Long live king Moshiach !
 
wether single or double quoted strings, the meta characters will be interpreted in meta context in a regexp. So use \Q to escape them like you did. You can use \E to tell the regexp where to stop the escaping if necessary.

Code:
s/\Q$OLDPATH\E$foo/$NEWPATH/;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top