"Escape" in perl

B

Bill H

I am using the following code to unescape html text that is coming
from flash:

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

for example it will take this text:

%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22#000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%20Title%3C%2FP%3E%3C
%2FFONT%3E

and it will convert it to this:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
ALIGN="CENTER">Chapter Title</P></FONT>

What I am trying to figure out is how to go the other way in perl,
convert the html to an escaped format. Any hints, clues, pointers
would be appreciated

Bill H
 
T

Tim Greer

Bill said:
I am using the following code to unescape html text that is coming
from flash:

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

for example it will take this text:

%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22#000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%20Title%3C%2FP%3E%3C
%2FFONT%3E

and it will convert it to this:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
ALIGN="CENTER">Chapter Title</P></FONT>

What I am trying to figure out is how to go the other way in perl,
convert the html to an escaped format. Any hints, clues, pointers
would be appreciated

Bill H

Just don't run the input through the unescape sub routine (or have one
that does and another that doesn't (and do whatever you want with the
other))? Or am I misunderstanding your problem/question?
 
B

Bill H

Just don't run the input through the unescape sub routine (or have one
that does and another that doesn't (and do whatever you want with the
other))?  Or am I misunderstanding your problem/question?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!- Hide quoted text -

- Show quoted text -

Tim

Actually the problem is that I want to use perl to make a lot of
default "escaped" html for flash. Up till now I have been just using
flash to do it, but now I have about 60 templates that I need to
create and know there has to be a better way then code actionscript,
run flash, cut / paste escaped stuff into perl code, run perl to
create template, repeat.

I was thinking of using HTML::Entities, but I can't install it on the
server I am using.

Bill H
 
B

Bill H

Tim

Actually the problem is that I want to use perl to make a lot of
default "escaped" html for flash. Up till now I have been just using
flash to do it, but now I have about 60 templates that I need to
create and know there has to be a better way then code actionscript,
run flash, cut / paste escaped stuff into perl code, run perl to
create template, repeat.

I was thinking of using HTML::Entities, but I can't install it on the
server I am using.

Bill H- Hide quoted text -

- Show quoted text -

The other thought I had, the brute force approach, is to just escape
every character in the string (ie convert each character to a %??). I
suppose I could get a little creative and not touch any numbers or
letters, just everything below ascii 48, above ascii 57 and below
ascii 65. Above ascii 90 and below ascii 97, and above ascii 122.
Wonder if there is a regex that could do this for me?

Bill H
 
T

Tad J McClellan

Bill H said:
I was thinking of using HTML::Entities, but I can't install it on the
server I am using.


Why not?

That is, what problem did you experience when you tried it?
 
P

Petr Vileta \(fidokomik\)

Bill said:
I was thinking of using HTML::Entities, but I can't install it on the
server I am using.

HTML::Entities is the right way. When you have not a rights to install it on
server then you can try 2 ways.
1) Ask server admin or tech. support for installing this module
2) download HTML::Entities and HTML::parser modules, place these modules to some
directory on server and write some like this to your code:

require "/full_path_to_module/HTML/Entities.pm";
require "/full_path_to_module/HTML/Parser.pm";

I think these modules have not other dependencies then Entities to Parser and
Parser to Entities.
 
S

sln

I am using the following code to unescape html text that is coming
from flash:

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

for example it will take this text:

%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22#000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%20Title%3C%2FP%3E%3C
%2FFONT%3E

and it will convert it to this:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
ALIGN="CENTER">Chapter Title</P></FONT>

What I am trying to figure out is how to go the other way in perl,
convert the html to an escaped format. Any hints, clues, pointers
would be appreciated

Bill H

This might be one way:

sub escape
{
my $text = shift;
$text =~ s/([<>= "#])/'%'.uc sprintf("%x", ord($1))/ge;
return($text);
}
 
T

Tim Greer

Bill said:
....


Tim

Actually the problem is that I want to use perl to make a lot of
default "escaped" html for flash. Up till now I have been just using
flash to do it, but now I have about 60 templates that I need to
create and know there has to be a better way then code actionscript,
run flash, cut / paste escaped stuff into perl code, run perl to
create template, repeat.

I was thinking of using HTML::Entities, but I can't install it on the
server I am using.

Bill H

That should work. Did you try installing the module's files in your own
account's "lib" directory (create one and call the module there if you
need to)?
 
S

sln

I am using the following code to unescape html text that is coming
from flash:

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

for example it will take this text:

%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22#000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%20Title%3C%2FP%3E%3C
%2FFONT%3E

and it will convert it to this:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
ALIGN="CENTER">Chapter Title</P></FONT>

What I am trying to figure out is how to go the other way in perl,
convert the html to an escaped format. Any hints, clues, pointers
would be appreciated

Bill H

This might be one way:

sub escape
{
my $text = shift;
$text =~ s/([<>= "#])/'%'.uc sprintf("%x", ord($1))/ge;
return($text);
}

Yeah, this seems to work. Try all except alpha-numeric and newline.

sln


####################
# Esc_some_html.pl
####################
use strict;
use warnings;

# Try all except alpha-numeric and newline
# --------------------------------------------
my $htmlchrs = '[^\w\n]'; # or '[<>= "#]'

my $str_escaped = '
%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22#000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%20Title%3C%2FP%3E%3C
%2FFONT%3E';


my $str_normal = unescape( $str_escaped);

print "$str_normal\n\n";

print escape( $str_normal)."\n";

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

sub escape
{
my $text = shift;
$text =~ s/($htmlchrs)/'%'.uc sprintf("%02x", ord($1))/ge;
return($text);
}

__END__

output:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE=
"20"><P ALIGN="CENTER">Chapter Title</P><
/FONT>


%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22%23000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%20Title%3C%2FP%3E%3C
%2FFONT%3E
 
X

xhoster

Bill H said:
....

I was thinking of using HTML::Entities,

What you want is not HTML entity escaping (&lt;), but URI escaping
(%3C). See URI::Escape.
but I can't install it on the
server I am using.

URI::Escape looks like it is pure Perl. If you can run Perl modules of
your own creation, then you can install it, as it is just anther Perl
module.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
B

Bill H

I am using the following code to unescape html text that is coming
from flash:
sub unescape
{
   my $text = shift;
   $text =~ s/%(..)/pack("c",hex($1))/ge;
   return($text);
}
for example it will take this text:

and it will convert it to this:
<FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
ALIGN="CENTER">Chapter Title</P></FONT>
What I am trying to figure out is how to go the other way in perl,
convert the html to an escaped format. Any hints, clues, pointers
would be appreciated

This might be one way:

sub escape
{
    my $text = shift;
    $text =~ s/([<>= "#])/'%'.uc sprintf("%x", ord($1))/ge;
    return($text);



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks! This works great

Bill H
 
P

Peter J. Holzer

The other thought I had, the brute force approach, is to just escape
every character in the string (ie convert each character to a %??). I
suppose I could get a little creative and not touch any numbers or
letters, just everything below ascii 48, above ascii 57 and below
ascii 65. Above ascii 90 and below ascii 97, and above ascii 122.
Wonder if there is a regex that could do this for me?

Yes. Search for "character class" in perldoc perlre. Then just translate
what you wrote from English to Regexp.

hp
 
B

Bill H

Yes. Search for "character class" in perldoc perlre. Then just translate
what you wrote from English to Regexp.

        hp

Thanks Peter.

I have been meaning to post a question / request about perldocs on
here but keep forgeting. It would be immensely helpful to me, and no
doubt others on here, to have a good description of how to get
information out of perldocs.

I have fumbled with the perldoc -f and mostly found what I was looking
for, and when I don't I post a question on here and 9 times out of 10
someone will show me how I should have used perldoc (though as an
aside, I was kinda suprised when I did a perldoc -f for and it came up
with nothing on the for loop). Most of the time I usually end up
looking through a printed copy of all the docs (500+ double sided, 2
up pages) that I had printed about 8 years ago.

In this group there is usually 3 or more auto posts a day from the FAQ
and they are on the majority very helpful, I have seen many
duplicates, though, but I have never seen a simple "How do I
find ?????? in perldoc?". If this exists, it would be a great FAQ to
have in the rotation, and possibly weighted so it shows up often. If
it doesn't exist, maybe it should be written and placed in there.

Bill H
 
T

Tad J McClellan

I have been meaning to post a question / request about perldocs on
here but keep forgeting. It would be immensely helpful to me, and no
doubt others on here, to have a good description of how to get
information out of perldocs.


I'll offer what help I can below.

I have fumbled with the perldoc -f


That looks in only one of the hundred-plus POD files (perlfunc.pod).

Searching less than 1% of the available information means you will
often miss what you are looking for.

I was kinda suprised when I did a perldoc -f for and it came up
with nothing on the for loop).


That is because "control structures" are not "functions".

Perl's control structures are documented in perlsyn.pod.

grep For all.heads

shows these 2 lines

perlsyn.pod:=head2 For Loops
perlsyn.pod:=head2 Foreach Loops

among its 50-some lines of output.

Bill Hl > Bile oMost of the time I usually end up
looking through a printed copy of all the docs (500+ double sided, 2
up pages) that I had printed about 8 years ago.


Computers are much better at searching than using paper...

I have never seen a simple "How do I
find ?????? in perldoc?".


I never actually use perldoc for searching the standard docs. I use grep(1).

I use perldoc for reading a module's docs fairly regularly though. I most
often use it only for getting the module's docs into a file though (so
that I can then grep it). eg:

perldoc -u DBI >DBI.pod

If this exists, it would be a great FAQ to
have in the rotation, and possibly weighted so it shows up often. If
it doesn't exist, maybe it should be written and placed in there.


Looks like it has been a couple of years since I last posted
my "Perl problem resolution checklist", so here it is again:




----------------------------------
Perl problem resolution checklist:
----------------------------------

1) check the Perl FAQs

(word search with "perldoc -q". Or better, find where the
*.pod's are on your system, and word search (grep) the
entire contents of the files)

2) expand the above to _all_ of the standard *.pod files.

3) check a Usenet archive such as:

http://groups.google.com/advanced_group_search

4) check books and websites (this step is optional)

5) write a Usenet article, but don't post it yet!

5a) make a small and complete (including data) program that
people can execute that shows your problem.

5b) state how the program's output is not what you want. Describe
what you want.

5c) repeat steps 1-4 using search terms taken from your description
of the problem or your Subject header (try some synonyms for
the terms also)

6) Give up on a quick answer. Post to Usenet for a slow answer.

7) wait hours/days/forever for followups with answers rather than
than the 5 or 10 minutes it would have taken if steps 1-3
had worked.

8) Wonder at the quality of the answers given, rather than know
it is a peer-reviewed, validated answer if steps 1-2 had worked.

9) Repeat steps 1-3 many times for many problems. You will seldom
get past step 3, and even less often get past step 5a.

10) Now that you know so much, go *answer* some questions on Usenet :)


-----

To help with 1 and 2 above, I make "headlines" files to grep in,
because sometimes there is Too Much Information when grepping
the entire bodies:

cd /an/INC/dir/pod/

grep '^=' perlfaq[1-9].pod >faq.heads

grep '^=' *.pod >all.heads
 
J

Jürgen Exner

Bill H said:
I have been meaning to post a question / request about perldocs on
here but keep forgeting. It would be immensely helpful to me, and no
doubt others on here, to have a good description of how to get
information out of perldocs.

Well, as always there is a perldoc entry specifically for that, see
'perldoc perldoc' and maybe even more important 'perldoc perl'. :))

But seriously, you have a very valid point. If you know what you are
looking for or if you have a strong background in computer science then
perldoc is ok to great. If you are just a casual user without formal
computer science education then the structure often seem arbitrary and
information scattered in odd places (heck, that even applies if you do
have a CS degree).
Now, while recognizing this as a problem, I don't have a real solution,
either. The only suggestion I have is to familiarize yourself with them.
That does _NOT_ mean to memorize all of it, but rather to learn about
the general structure, develop an understanding of where you are likely
to find some pointers, and what information is available about which
topics: oh, there is a perlretut (Perl regular expression tutorial) and
a perlreftut (reference tutorial) and a even a perlipc. Most likely you
are not interested in IPC when reading the perldoc index. However this
tidbit of information hopefully gets stuck in your brain, so that when
you do need information about IPC you remember that there was a perldoc
module about IPC and you can go back and actually read it this time.
I have fumbled with the perldoc -f and mostly found what I was looking
for, and when I don't I post a question on here and 9 times out of 10
someone will show me how I should have used perldoc (though as an
aside, I was kinda suprised when I did a perldoc -f for and it came up
with nothing on the for loop).

That's what I meant with Computer Science background: 'for' is not a
function, it is core language syntax. You will find it in 'perldoc
perlsyn'.
Most of the time I usually end up
looking through a printed copy of all the docs (500+ double sided, 2
up pages) that I had printed about 8 years ago.

That IMO is a rather poor idea. You got everything online sitting right
there on your own computer. Use the power of the force, aehhmm, the
computer to search and hopefully find the information you need. It would
also be the accurate information for the version of Perl you are using
isntead of some outdated version for some outdated Perl without all the
bug fixes that went into it since the new millenium.

Also one way that sometimes works is to grep for a key word in the
perldoc source files. That may give you a pointer to the file/perldoc
manual that contains more information about a topic.
In this group there is usually 3 or more auto posts a day from the FAQ
and they are on the majority very helpful, I have seen many
duplicates, though, but I have never seen a simple "How do I
find ?????? in perldoc?". If this exists, it would be a great FAQ to
have in the rotation, and possibly weighted so it shows up often. If
it doesn't exist, maybe it should be written and placed in there.

Now, that's a great idea. I just don't know the answer to that FAQ,
otherwise I would write it. :)

jue
 
P

Peter J. Holzer

I have fumbled with the perldoc -f and mostly found what I was looking
for, and when I don't I post a question on here and 9 times out of 10
someone will show me how I should have used perldoc (though as an
aside, I was kinda suprised when I did a perldoc -f for and it came up
with nothing on the for loop). Most of the time I usually end up
looking through a printed copy of all the docs (500+ double sided, 2
up pages) that I had printed about 8 years ago.

When I started to program Perl in earnest (in early 1995, so that must
have been 5.000), I started with "perldoc perl" ("man perl" actually, I
only found out about perldoc later) and read through all the documents
referenced from there. At the time that was a few hundred pages, so that
took only a few days. Today that would be quite a bit longer. I didn't
learn it by heart - just read through it to get a feeling for what the
language could do and how the documentation was laid out, so that later,
for any specific question I had a rough idea where to look (for example,
all the control structures are in "perldoc perlsyn", so that's where I
would look for information about the for loop).

The most important perldocs are imho:

* perldoc Some::Module for information about Some::Module. Perl is
CPAN, the rest is just syntactic sugar ;-).
* perldoc perlre for information about regular expressions. There are
always some details about regexps which I don't quite remember, so
I use that frequently.
* perldoc perlfunc, or rather perldoc -f for info about builtin
functions.
* perldoc perlsyn, perlop and perldata for the language proper.
(actually, these I days I mostly need them for usenet discussions :))
* perldoc perlrun for command line parameters
* perldoc -q if I don't know where to look
* perldoc perl if I still don't know where to look.

Usually I search first CPAN, then Google somewhere in between.
And of course there is http://perldoc.perl.org/ with a searchable
online version of all the perldocs for 5.10.0 and 5.8.8. I use that only
rarely, but I guess I should use it more often (if only because the
pages are better formatted than in a terminal window).

hp
 
T

Tad J McClellan

Peter J. Holzer said:
The most important perldocs are imho:

* perldoc Some::Module for information about Some::Module. Perl is
CPAN, the rest is just syntactic sugar ;-).
* perldoc perlre for information about regular expressions. There are
always some details about regexps which I don't quite remember, so
I use that frequently.
* perldoc perlfunc, or rather perldoc -f for info about builtin
functions.
* perldoc perlsyn, perlop and perldata for the language proper.
(actually, these I days I mostly need them for usenet discussions :))
* perldoc perlrun for command line parameters
* perldoc -q if I don't know where to look
* perldoc perl if I still don't know where to look.


Surely perlop.pod belongs in that list.
 
E

Eric Pozharski

*SKIP*
In this group there is usually 3 or more auto posts a day from the FAQ
and they are on the majority very helpful, I have seen many
duplicates, though, but I have never seen a simple "How do I find
?????? in perldoc?". If this exists, it would be a great FAQ to have
in the rotation, and possibly weighted so it shows up often. If it
doesn't exist, maybe it should be written and placed in there.

B<Perldoc::Search>?

It has a script inside, that provides quiet simple (down to dumb)
interface on all the *text* files in I<@INC> (I believe, didn't looked
inside). It's too verbose for daily usage, but it does what its name
say.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top