I have a class called book.pm with an attribute called 'html'. This attribute is a file which I want to write to. Book.pm uses a package called template.pm to help it write the html. I cannot seem to pass the book->html attribute to template. I get no complaints and the code runs, but I get files written in the scripts directory. Maybe a bit o' code will help clarify.

Here's an excerpt from book.pm:
sub new { my ($class) = @_; #call the constructor of the parent class, Person. my $self = $class->SUPER::new(); $self->{_name} = undef; $self->{_template} = undef; $self->{_html} = undef; bless $self, $class; return $self; } sub html{ my ( $self, $html ) = @_; $self->{_html} = $html if defined($html); return ( $self->{_html} ); }

Later I try to toss the file to Template to build the actual page like so:

my $template = eval { new Template(); } or die ($@); $template->header($html, $header, $time);

Here's the corresponding Template.pm bit:

sub header { my $html = shift; my $header = shift; my $time = shift; open (OUT,">$html") or die ("No html file: $!\n");

It's writing to text files and html files, but not the one that's passed. A sample html file is entitled: Template=HASH(0x81eb334)
Also, if I define it directly in Template then how do I access it with the other variables I pass to it. Where is it in the stack, so to speak?

I'm a bit green in the OOP department. If you've got some advice I'd be much obliged.

thanks~

Bub

In reply to Passing variable to method in perl by bubnoff

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.