I found this page really useful for escaping stuff to pass to javascript functions in PHP, and I needed a perl version.
So here it is:
sub js_enc {
my $str = shift || '';
my @chars = split(//,$str);
foreach my $c (@chars) {
$c = '\x' . sprintf("%x", ord($c));
}
return join('',@chars);
}
Some weirdness in the post:
The sourcecode in the above example gets all whacked with the smart quotes and curly quotes and all that – if you just copy/paste the source, you’ll get all sorts of errors from Perl.
Secondly, spaces and tabs seem to act funky when I tried this – I’m trying to escape some HTML in Perl to pass to FCKEditor which itself is being called by HTML::Template – anyways, here’s what I did – after escaping the HTML, I think unescaped spaces and tabs – I bet it would be easier to do this using HTML::Entities but, meh:
sub js_enc {
my $str = shift || ”; my @chars = split(//,$str); foreach my $c (@chars) { $c = ‘\x’ . sprintf(“%x”, ord($c)); } my $e = join(”,@chars);
$e =~ s/\xa/ /g; $e =~ s/\x9/\t/g; return $e;
}
top work, we are implementing the FCKeditor into our CMS and needed this exact code. Just Wanted to Say THANKS! Richard SiteWizard Developer