For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > unescape or render literal backslash "\"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author unescape or render literal backslash "\"
Perl-Lerner

2006-10-20, 6:56 pm

I know what I'm asking is very simple but I'm banging my head against
the wall.
I have the following loop

while (<INPUT> ) {
$original_shapename = $_;
$_ =~ s/\./_/;
chomp $_;
$test = $_ . " = D:\\\jb_temp\\\NLCD_92\\\clipping\\\outp
ut_dd27\\\"
.. $original_shapename . "\n";
print $test;
}

What I want to do is for example ... suppose that $_ is equal to the
string value "myShapefile.shp". I want $test to be equal to the
following string value

"myShapefile_shp =
D:\\jb_temp\\NLCD_92\\clipping\\output_d
d27\\myShapefile.shp"

but since "\" is an escape character, I'm getting

syntax error at C:\repository\dev\Perl\writeScript.pl line 20, near ""
= D:\\\jb_temp\\\LCD_92\\\clipping\\\outpu
t_dd27\\\" .
$original_shapename . "\"
Can't find string terminator '"' anywhere before EOF at
C:\repository\dev\Perl\writeScript.pl line 20.

How do I unescape the escape characters ?

Paul Lalli

2006-10-20, 6:56 pm

Perl-Lerner wrote:
> I know what I'm asking is very simple but I'm banging my head against
> the wall.
> I have the following loop
>
> while (<INPUT> ) {
> $original_shapename = $_;
> $_ =~ s/\./_/;
> chomp $_;
> $test = $_ . " = D:\\\jb_temp\\\NLCD_92\\\clipping\\\outp
ut_dd27\\\"
> . $original_shapename . "\n";
> print $test;
> }
>
> What I want to do is for example ... suppose that $_ is equal to the
> string value "myShapefile.shp". I want $test to be equal to the
> following string value
>
> "myShapefile_shp =
> D:\\jb_temp\\NLCD_92\\clipping\\output_d
d27\\myShapefile.shp"
>
> but since "\" is an escape character, I'm getting
>
> syntax error at C:\repository\dev\Perl\writeScript.pl line 20, near ""
> = D:\\\jb_temp\\\LCD_92\\\clipping\\\outpu
t_dd27\\\" .
> $original_shapename . "\"
> Can't find string terminator '"' anywhere before EOF at
> C:\repository\dev\Perl\writeScript.pl line 20.
>
> How do I unescape the escape characters ?


It's not at all clear what you actually want. In a double quoted
string, if you want a "real" backslash, you have to type two
backslashes. So if you want two "real" backslashes next to each other,
you need to type *four* backslashes next to each other. In your code,
you have sequences of three backslashes. The first two in each
sequence become a single real backslash, and the third makes whatever
the following character is into a meta-character. In the final case,
that means that the " mark is made into part of the string rather than
the string delimiter.

Paul Lalli

DJ Stunks

2006-10-20, 6:56 pm

Perl-Lerner wrote:
> I know what I'm asking is very simple but I'm banging my head against
> the wall.
> I have the following loop
>
> while (<INPUT> ) {
> $original_shapename = $_;
> $_ =~ s/\./_/;
> chomp $_;
> $test = $_ . " = D:\\\jb_temp\\\NLCD_92\\\clipping\\\outp
ut_dd27\\\"
> . $original_shapename . "\n";
> print $test;
> }
>
> What I want to do is for example ... suppose that $_ is equal to the
> string value "myShapefile.shp". I want $test to be equal to the
> following string value
>
> "myShapefile_shp =
> D:\\jb_temp\\NLCD_92\\clipping\\output_d
d27\\myShapefile.shp"
>
> but since "\" is an escape character, I'm getting
>
> syntax error at C:\repository\dev\Perl\writeScript.pl line 20, near ""
> = D:\\\jb_temp\\\LCD_92\\\clipping\\\outpu
t_dd27\\\" .
> $original_shapename . "\"
> Can't find string terminator '"' anywhere before EOF at
> C:\repository\dev\Perl\writeScript.pl line 20.
>
> How do I unescape the escape characters ?


Note that even on Windows any file operations in Perl (like open() or
rename() for instance) you can still use forward slashes like

my $file = "C:/Documents and Settings/jpeavy1/Desktop/$yymmdd.log";
open my $fh, '<', $file or die "Could not open '$file': $!\n";

HTH,
-jp

Perl-Lerner

2006-10-20, 6:56 pm


Paul Lalli wrote:

>
> It's not at all clear what you actually want. In a double quoted
> string, if you want a "real" backslash, you have to type two
> backslashes. So if you want two "real" backslashes next to each other,
> you need to type *four* backslashes next to each other. In your code,
> you have sequences of three backslashes. The first two in each
> sequence become a single real backslash, and the third makes whatever
> the following character is into a meta-character. In the final case,
> that means that the " mark is made into part of the string rather than
> the string delimiter.
>
> Paul Lalli


Thanks ... I think thats all I needed was to add yet another backslash

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com