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

In reply to Making a function callable on an object by George_Sherston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.