[CGI Home] [Next] [Prev] .............. Introduction to CGI

CGI Tricks - Mostly Perl

This document contains some tricks that are useful when fiddling with cgi programs in perl (or other) languages. If you have additional nice tricks, please send them to ian.graham@utoronto.ca with the subject line CGI coding tricks.


Converting Latin-1 Octets to Character References

This is something you often need to do when sending HTML documents via e-mail, since 8-bit characters get messed up by many mailers. So, assuming that the line you want to convert is in the variable $line, the relevant line of perl code is one of the following:

  $line =~ s/[&<>\200-\377]/sprintf("&#%d;", unpack("C", $&))/ge;
  $line =~ s/[&<>\200-\377]/sprintf("&#%d;", ord ($&))/ge;
This trick is courtesy of Abigail.
[CGI Home] [Next] [Prev] .............. Introduction to CGI