Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Escaping special characters for HTML

by serf (Chaplain)
on Jan 04, 2006 at 16:14 UTC ( #520948=note: print w/replies, xml ) Need Help??

in reply to Escaping special characters for HTML

Where you are needing to nest double quotes I would recommend using
. qq~key="value"~
or similar notation where the double quotes are for Perl and not part of the data - and save the normal " double quotes for where they *need* to be used in the HTML/JavaScript/CSS.

It will make it much easier to tell where the quotes need to be, and you will be able to have double quotes in your data without escaping them.

You are also using more concatination operators than you need to (which doesn't make your code any faster!)

e.g. where you have:

You could use:
. "px;width:${width}px;height:${height}px;cursor:pointer;"
i.e. protecting the scalar names with {} where they have to be followed immediately by more plain text or underscores.

This will improve readability because it will make the code look more like the output is going to look and make the Perl scalars stand out a bit within the code.

NB: you don't *need* to do this when the scalar is followed by an ampersand or other non-variable name character (beware of [ or { which would be the start of an array index or hash though!)

It could also simplify things if you did your maths before the assignment instead of embedding it into the concatination if you don't need it to change while it's in the concatination (especially as you're doing the same maths more than once):

$newdiv='<!-- ADD DIVS -->'."\n".div({-style=>"position:absolute;left: +".($startx+$arealeft)."px;top:".($starty+$areatop)."px;width:".$width +."px;height:".$height."px;cursor:pointer;",-onclick=>"Start('$cgiurl? +id=$custid');return true;",
could become:
my $tmp_left = $startx + $arealeft; my $tmp_top = $starty + $areatop; $newdiv= "<!-- ADD DIVS -->\n" . div( { -style => "position:absolute;left:${tmp_left}px;top:${tmp_top} +px;width:${width}px;height:${height}px;cursor:pointer;", -onclick => "Start('$cgiurl?id=$custid');return true;", ...
the only bit that looks particularly broken is the -onmouseover => '...' - it has two 'return's in it for starters... and I'm not sure what else wrong with it, but apart from that I get:
my $tmp_left = $startx + $arealeft; my $tmp_top = $starty + $areatop; my $newdiv= "<!-- ADD DIVS -->\n" . div( { -style => "position:absolute;left:${tmp_left}px;top:${tmp_ +top}px;width:${width}px;height:${height}px;cursor:pointer;", -onmouseover => ( fix this bit... ), -onclick => "Start('$cgiurl?id=$custid');return true;", -onmouseout => "window.status=' ';shw('$imagedetails[0]-po +p',event);return true;", }, '' ) ."\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://520948]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-06-18 00:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.