hotshot has asked for the wisdom of the Perl Monks concerning the following question:

hello guys!

I using CGI to write to the browser, as you probably now there are several characters that need to be escaped before sending them to the browser coz' they have another meaning (e.g.: <, >, & ..).
is there a way to convert these chars automatically before sending them to browser. i'm currently do it manually for each field before I output it to the browser. the thing is I have a structure (array, hash, AoH, HoA - can be different each time) to display to the browser and in every display function I have I use:
s/&/&amp;/; s/</&lt;/; s/>/&gt;/;
before each structure entry I have, quite clumsy. Is there any function that gets a structure (can be any structure), and escapes all it's fields? Thanks in advance

Hotshot

Replies are listed 'Best First'.
Re: escaping anallowed chars for browser
by janjan (Beadle) on Nov 19, 2002 at 16:08 UTC
    Have a look at perldoc CGI; CGI.pm includes an escapeHTML() function that you might find useful:
    $escaped_string = escapeHTML("unescaped string");
    It only accepts strings, though; not sure if a function exists for any data structure.
Re: escaping anallowed chars for browser
by fruiture (Curate) on Nov 19, 2002 at 16:54 UTC
Re: escaping anallowed chars for browser
by Abigail-II (Bishop) on Nov 19, 2002 at 16:31 UTC
    I don't like to have to remember the names of the entities. So I prefer to use the numeric escapes:
    s<[<&">]>{&#${\ord$&};}g;

    Abigail

Re: escaping anallowed chars for browser
by dingus (Friar) on Nov 19, 2002 at 16:10 UTC
    From a node I can't find on perlmonks comes this handy trick:
    my %entity = ( '<' => '&lt;', '>' => '&gt;', '&' => '&amp;', '"' => '&quot;', ); $output =~ s/([<>&"])/$entity{$1}/g; print $output;
    I use it all the time for debug output

    Dingus


    Enter any 47-digit prime number to continue.