Go to content Go to navigation Go to search

HTML Formatting Your Perl · Aug 21, 02:07 AM by Dylan Doxey

Ever wonder how to make your Perl code presentable on a website? Use perltidy.

perltidy -html rand_sort.pl

Then cut & paste the resulting HTML into your blog and delight your readers.


#!/usr/bin/perl 

use strict;
use warnings;

my @numbers = sort { -1 + int rand 3 } 1 .. 1000;

for my $x (@numbers) {

    print "$x\n";
}

However, in my opinion the best option for a blog snippet is vim. While you've got your Perl (or whatever) file open in vim use the :TOhtml command. That will open another buffer with HTML in it. The resulting HTML page is:

 1 #!/usr/bin/perl 
 2 
 3 use strict;
 3 use warnings;
 4  
 5 my @numbers = sort { -1 + int rand 3 } 1 .. 1000; 
 6  
 7 for my $x (@numbers) { 
 8  
 9     print "$x\n"; 
10 } 
11  

For more information about using perltidy see http://perltidy.sourceforge.net/perltidy.html#html_options. For the record, my example of perltidy HTML output is pretty lame. As linked from the perltidy home page (http://perltidy.sourceforge.net/) check out this example of perltidy HTML output: http://perltidy.sourceforge.net/Conf.pm.html.


For more details about using :TOhtml user command in vim see http://vimdoc.sourceforge.net/htmldoc/syntax.html#:TOhtml


Now I won't forget how to do that again!

Commenting is closed for this article.