Re: [ILUG] Perl OO and formats

From: Peter Heslin (Peter.Heslin at domain ucd.ie)
Date: Thu 10 Feb 2000 - 00:46:37 GMT


On Wed, Feb 09, 2000 at 11:07:26AM +0000, Donncha O Caoimh wrote:
> Just got my head around Perl OO yesterday (after a lengthy time away
> from trying to figure it out, those O'Reilly books are great!) and I'm

I've been reading /Object Oriented Perl/ by Damien Conway, published
by Manning -- it's very good indeed.

> trying to print to a FORMAT from a derived function.
> I tried the obvious,
> $self->{CUSTOMER_REPORT} = CUSTOMER_REPORT;
> in the base constructor which didn't work (didn't think it would
> though.) but I'd like to know if anyone knows a better way of using
> formats other than including the file in each function, or making them
> global variables. (Is that possible with formats even?)

I do not understand exactly what you are trying to do here, but if you
just want to treat a format as a global variable, that should be easy --
qualify it with its full package name:

use FileHandle;
my $fh = new FileHandle "> foo.txt";
$fh->format_name("My_Class::My_Subclass::My_format");
write ($fh) if defined $fh;

I would imagine that you can also pass a format to a subroutine
(or method) by passing a typeglob, just as one would do for a
filehandle or any other symbol table entry:

$self->{CUSTOMER_REPORT} = \*CUSTOMER_REPORT;
---------------------------^^

But I have never tried this myself, and now that I write this, I am
having second thoughts -- there is some special magic attached to
filehandle refs that implicitly and automatically dereferences them
upon use, and this magic may not apply likewise to formats. It might be
worth a quick try.

Could it be that your problem is not only that you need access to the
format itself, but also that lexically scoped variables used in the
format's value list have gone out of scope (or have not even been
declared yet, since you say this is a constructor method)?

Peter



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:05:23 GMT