Dec 23 2007

perl: Word Search Generator

Published by at 11:26 am under Programming

Output

We now have our array @grid which has all the words which we managed to place on it, the rest of it is blank. How do we output it? We could print it out to the command line as ASCII text, or send it to a text file. But those aren’t too attractive or useful.

Better is to generate a nice looking PDF document of the puzzle. perl is well supported with external modules to do just about any task you could imagine. We will use the PDF::Create module, which can be found on the CPAN website. Note ActiveState has the great Perl Package Manager which will grab PDF::Create off the internet and install it on your machine.

The following code sets up a new PDF file, and writes 2 pages to it, one with the puzzle and one with the solution. The details we handle with a subroutine called create_page, which we’ll show later.

# create the output pdf file
$pdf = new PDF::Create('filename' => $pdf_filename,
	     'Version'  => 1.2,
	     'PageMode' => 'UseNone',
	     'Author'   => $author,
	     'Title'    => $title);
$root = $pdf->new_page('MediaBox'  => [ 0, 0, 612, 792 ]);
$f1 = $pdf->font('Subtype'=>'Type1', 
                 'Encoding' => 'WinAnsiEncoding', 
                 'BaseFont' => 'Helvetica-Bold');
$f2 = $pdf->font('Subtype'=>'Type1', 
                 'Encoding' => 'WinAnsiEncoding', 
                 'BaseFont' => 'Helvetica');

# output two pages, one without solution and one with
create_page('N');
create_page('Y');

$pdf->close;

exit;

Figure 8 – Outputting to PDF

Thats the end of the program. All that remains are a few subroutines, which we will look at next.

Pages: 1 2 3 4 5 6 7

3 responses so far

Please do not load this page directly. Thanks!