http://qs1969.pair.com?node_id=159363

George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:

I have a subroutine to help me work with PDF::Create. It accepts a hashref as argument, which must contain page, font and text, and can contain some other optional parameters. In return it lays out the text in lines of appropriate width and spacing - that is, turns a string of text into a column of text in PDF file.

The usage of the module, and the module itself, are below.

My question is about the way I call the module. As you see, having created a new page object using PDF::Create, I then pass this object to my subroutine, which modifies it and returns it. What I'd like to know how to do is write it so I could call it thus: $page->PDFColumn(....). This seems neater than the three-card monty I have to do at present with objects going back and forth. (Although on mature consideration I think I may wish to pass back the modified $height parameter as well)

One thing I know I'd have to do is alter the start of my subroutine to get the page directly rather than as an element in a hashref. But that's clearly not all there is to it. I know little of OO, less of Perl OO. I read through bless, but this doesn't seem to be what I want. I'd be very grateful if any kind monk cd steer me in the direction of an explanation.

#!/usr/bin/perl -w use strict; use PDF::Create; use Font::AFM; my $page = $pdf->new_page('MediaBox' => [ 0, 0, 650, 920 ]); my $fn = $pdf->font( 'Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica' ); $page = PDFColumn( { page => $page, font => $fn, text => $text, right => 250, height => 850, fontsize => 15 } ); $pdf->close; sub PDFColumn { # get the hashref of parameters: my $params = shift; # get the critical parameters and return if they are not present: my $page = $params->{page}; return unless $page; my $font = $params->{font}; return $page unless $font; # get the non-critical parameters or their default values: my $text = $params->{text} || ""; my $fontsize = $params->{fontsize} || 10; my $lineheight = $params->{lineheight} || $fontsize * 1.4; my $left = $params->{left} || 50; my $right = $params->{right} || 300; my $height = $params->{height} || 750; # get the afm info: my $afm_file = $page->{'pdf'}{'fonts'}{$font}{'BaseFont'}[1]; my $afm = new Font::AFM $afm_file; # get the text and the width of the column: my @text = split /\s+/, $text; my $line = shift @text; my $width = $right - $left; # loop through the text sending each new line to the PDF page: while (@text) { my $word = shift @text; if ($afm->stringwidth($line . " " . $word, $fontsize) > $width +) { $page->string($font, $fontsize, $left, $height, $line ); $line = $word; $height -= $lineheight; } else { $line .= " " . $word; } $page->string($font, $fontsize, $left, $height, $line ) if @te +xt == 0; } # return the modified page: return $page; }


§ George Sherston