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

Replies are listed 'Best First'.
Re: Making a function callable on an object
by Ovid (Cardinal) on Apr 15, 2002 at 23:36 UTC

    If I understand you correctly, just write your own class and have it inherit from PDF::Create. The following should get you started.

    package PDF::Create::Column; use strict; use base ('PDF::Create'); use Font::AFM; sub PDFColumn { # get the hashref of parameters: my ( $self, $params ) = @_; # rest of code here return $page; }

    Then, in your regular code:

    use PDF::Create::Column; my $pdf = new PDF::Create::Column( filename => 'test.pdf', Version => 1.2, Author => 'George Sherston', Title => 'test', ); my $page = $pdf->new_page('MediaBox' => [ 0, 0, 650, 920 ]); my $fn = $pdf->font( 'Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica' ); ### ### # here we go!!! # ### ### $page = $pdf->PDFColumn( { page => $page, font => $fn, text => $text, right => 250, height => 850, fontsize => 15 } ); $pdf->close;

    That's untested, but should be what you're looking for.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Making a function callable on an object
by Kanji (Parson) on Apr 15, 2002 at 23:38 UTC

    All you need do is make sure PDFColumn and $page share the same namespace, either by declaring your sub into PDF::Create's ...

    sub PDF::Create::PDFColumn { my $page = shift; my $params = shift; # ... }

    ... or (more elegantly) by sub-classing ...

    package PDF::Create::Column; # Try looking for any methods not defined here # in PDF::Create instead. use base qw/ PDF::Create /; sub PDFColumn { my $page = shift; my $params = shift; # ... } 1;

        --k.


Re: Making a function callable on an object
by Chmrr (Vicar) on Apr 15, 2002 at 23:35 UTC

    You need to sneak your PDFColumn method into the PDF::Create::Page package. The cheap and easy way to do this is to say:

    sub PDF::Create::Page::PDFColumn { my $self = shift; # get the hashref of parameters: my $params = shift; # ... }

    Thus, when you say $page->PDFColumn, perl looks up what class $page is (I'm guessing it's a PDF::Create::Page) and attempts to call PDF::Create::Page::PDFColumn with $page as the first argument. Luckily, you've given perl such a method, so it's all happy.

    Update: Note that this is slightly sleazy -- if this is permanent code, you're better off using Ovid's method (no pun intended)

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Thanks All
by George_Sherston (Vicar) on Apr 16, 2002 at 09:30 UTC
    Just to say thanks a million for these replies. I find it quite hard to get to grip with a new area of programming from absolute first principles, but if I get a bit of an idea how it's meant to work I learn quite quickly. Like the grit in the oyster. This has really got me started, and I feel certain that it's going to be a lot easier from here - extremely useful. Thanks again.

    § George Sherston
UPDATE
by George_Sherston (Vicar) on Apr 15, 2002 at 23:26 UTC
    Chmrr kindly points out that I left out a bit - the following should come in before creation of $page:
    my $pdf = new PDF::Create( filename => 'test.pdf', Version => 1.2, Author => 'George Sherston', Title => 'test', );


    § George Sherston