Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > String escaping

Reply
Thread Tools

String escaping

 
 
RedGalaxy UK
Guest
Posts: n/a
 
      11-26-2005
Help!!

I'm rather new to Perl; trying it out as it has advantages over PHP in
the things I want to do.

In PHP there is a simple function, addslashes(), which changes ' for
\', " for "\, \ for \\ and so on (basically adding a backslash to
escape it).

Trying to do the same in Perl and can't find any way to do the \ = \\
change? Found this little sub:

sub addslashes {
my $string = shift;
$string=~s/'/\\'/g;
$string=~s/"/\\"/g;

return $string;
}

If I pass it something like:

This is just a 'dummy test' and it works.

It will give me:

This is just a \'dummy test\' and it works.

However, parts of my data will include backslashes in the data itself,
and if I try passing something like:

C:\Windows\System\

It will print:

C: indows ystem

Anybody know how I could do this, or even if there is a function within
Perl I just don't know about? I'm using ActiveState's ActivePerl 5.8.7
for Windows.

Thanks.

Paul

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-26-2005
"RedGalaxy UK" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> Help!!
>
> I'm rather new to Perl; trying it out as it has advantages over PHP in
> the things I want to do.
>
> In PHP there is a simple function, addslashes(), which changes ' for
> \', " for "\, \ for \\ and so on (basically adding a backslash to


"\ ?

> escape it).


What determines which characters are escaped?

I sense an X-Y problem here: You want to achieve X, you think Y is the
way to do it, and therefore you are asking about Y.

What is X in your case?

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-26-2005
RedGalaxy UK wrote:
>
> I'm rather new to Perl; trying it out as it has advantages over PHP in
> the things I want to do.
>
> In PHP there is a simple function, addslashes(), which changes ' for
> \', " for "\, \ for \\ and so on (basically adding a backslash to
> escape it).
>
> Trying to do the same in Perl and can't find any way to do the \ = \\
> change?


my $newstring = quotemeta $oldstring;

my $newstring = "\Q$oldstring\E";


perldoc -f quotemeta
perldoc perlop


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-26-2005
RedGalaxy UK <(E-Mail Removed)> wrote:

> In PHP there is a simple function, addslashes(), which changes ' for
> \', " for "\, \ for \\ and so on (basically adding a backslash to
> escape it).



> C:\Windows\System\



Just in case you didn't know this, you can probably use

C:/Windows/System/

with no ill effects.

As long as the filename is not fed to the command interpreter,
(like from within a Perl program) you can use forward slashes
instead of backwards slashes.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
RedGalaxy UK
Guest
Posts: n/a
 
      11-26-2005
The only problem with this is that the text passed to the database
might not necessarily be a file path. People are using a website where
they post entries, comments, messages, etc, and my perl script is
basically processing this information and inserting data into a
database on my company data server, so it could be a string of random
text where they insert a backslash into it.

Paul

 
Reply With Quote
 
RedGalaxy UK
Guest
Posts: n/a
 
      11-26-2005
Hi John,

I just tried those; it seems to be escaping anything which isn't an
alphanumeric character, but failed to do anything about the backslashes
in the original string.

Paul

 
Reply With Quote
 
RedGalaxy UK
Guest
Posts: n/a
 
      11-26-2005
A. Sinan Unur,

I'm determining which characters are escaped with the separate lines in
the sub:

sub addslashes {
my $string = shift;
$string=~s/'/\\'/g;
$string=~s/"/\\"/g;

return $string;
}

That's currently looking for instances of ' and " and escaping those,
and my thought, or "Y" as you put it, was that if I added

$string=~s/\/\\\/g;

it would escape any backslashes already in the string. However I get an
error, trailing backslash in regex. "X" in this case is trying to get
the script to escape backslashes in a string, so for example if I had
"Tomatoes\Apples\Pears\Fruits" it'll change it to
"Tomatoes\\Apples\\Pears\\Fruits"

It would be so much easier if this was data I could control but it's
from a large member-controlled site.

Does this help you to understand my situation better?

Thanks.

Paul

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-27-2005
"RedGalaxy UK" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> A. Sinan Unur,
>
> I'm determining which characters are escaped with the separate lines
> in the sub:


It would be much easier to follow the discussion if you quoted an
appropriate amount of context.

....

> That's currently looking for instances of ' and " and escaping those,
> and my thought, or "Y" as you put it, was that if I added


That still does not explain what the "Y" is: I have a feeling you think
all this slash stuff is necessary for dealing with paths, but it is not.

See File::Spec.

> $string=~s/\/\\\/g;
>
> it would escape any backslashes already in the string. However I
> get an error, trailing backslash in regex.


Here is a naive implementation:

#!/usr/bin/perl

use strict;
use warnings;

my $s = q{'"Sin} . q{\\} . q{an"'};

print "$s\n";
print my_escape($s);

sub my_escape {
my ($s) = @_;
$s =~ s{(['"\\])}{\\$1}g;
return $s;
}

__END__

By the way, I would be using quotemeta for this particular purpose:

perldoc -f quotemeta

On the other hand, if you are jumping through these hoops to deal with
Win32 paths, it is not necessary.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-27-2005
RedGalaxy UK <(E-Mail Removed)> wrote:

> $string=~s/\/\\\/g;
>
> it would escape any backslashes already in the string.


> so for example if I had
> "Tomatoes\Apples\Pears\Fruits" it'll change it to
> "Tomatoes\\Apples\\Pears\\Fruits"



Both the pattern and the replacement string part of s///
are "double quotish", they act like double quoted strings.

You need to backslash each backslash in your code in order
to backslash each backslash in your data.

$string =~ s/\\/\\\\/g;


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
RedGalaxy UK
Guest
Posts: n/a
 
      11-27-2005
Tad,

Just tried this; for some reason it doesn't seem to work If I
change the string to Apples\Bananas\Pears, it comes up with
ApplesBananasPears and for some reason completely removes the
backslashes before the string is passed to anything else.

Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: String substitution VS proper mysql escaping Cameron Simpson Python 10 09-04-2010 01:10 PM
String substitution VS proper mysql escaping Python 2 08-18-2010 09:07 AM
FW: Escaping a triple quoted string' newbie question Jules Stevenson Python 1 03-02-2008 10:57 AM
Safe string escaping? Grant Olson Python 2 03-08-2005 02:53 AM
escaping % in a string??? Amy G Python 3 02-27-2004 12:40 PM



Advertisments