Home  |  Linux  | Mysql  | PHP  | XML
From:John W. Krahn Date:Wed Jul  1 05:47:09 2009
Subject:Re: Substitution question
sanket vaidya wrote:
> Hi all,

Hello,

> As a part of one program I need to get the current working directory. So I
> get this using cwd(). I want to replace forward slash (/) by backslash (\)
> in the path which I get because I work on windows.

You shouldn't need to do that.

> Kindly look at the code below:
>
> use warnings;
> use strict;
> use Cwd;
>
> my $current_path = cwd();
> print "$current_path";
>
> $current_path =~ s|/|\|;

That should be:

$current_path =~ s|/|\\|g;

Or:

$current_path =~ tr|/|\\|;

> print "$current_path";

perldoc -q quoting
Found in /usr/share/perl/5.8/pod/perlfaq4.pod

The problem is that those double-quotes force stringification--
t
want them to be strings. Think of it this way: double-quote
expansion is used to produce new strings. If you already have a
string, why do you need more?

If you get used to writing odd things like these:

print "$var"; # BAD

> When I run this I get the below error:
> Substitution replacement not terminated at line 8.

That is because a single backslash escapes the terminating delimiter.

> However if I replace 's|/|\|' with 's////\/' it works.

$ perl -e's////\/'
Search pattern not terminated at -e line 1.

It doesn't appear to work.

> Can anyone tell me why this happens & how to overcome it?


John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov

Navigate in group perl.beginners at sever nntp.perl.org
Previous Next


Your recent visits
Re: variable scope
mailto:beginners-uc.1265554412.pbjbnmdgcoehhijoegcf-anilputhiyath
Re: More compact way to write this
Re: sorting anonymous arrays inside arrays
Re: Substitution question



  
© No Copyright
You are free to use Anything, but please consult your advocate before doing so as this website
also list content from other sources which may be copyrighted.
Site Maintained by Zareef Ahmed
Powered By PHP Consultants