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

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

Replies are listed 'Best First'.
Re: Passing variable to method in perl
by Tanktalus (Canon) on Aug 21, 2009 at 02:06 UTC

    A few points here. First off, most of us are case sensitive. In fact, Perl usually is case sensitive. As in, book.pm is not the same thing as Book.pm. Normally, this only matters in non-OO modules where you're importing stuff (e.g., "use Foo qw(bar baz)"), but, still, it's confusing. Since all-lowercase modules are, by convention, reserved for pragmas, I'd suggest sticking to Book instead of book.

    Second, generally it's preferred if you post a minimal "working" example. By "working" I do not mean "code works flawlessly" but "code shows the entire logic that exhibits the problem."

    Because of this, I'm having a hard time groking your "try to toss the file" snippet, what context, etc., where $html, $header, and $time are coming from, that sort of thing. Your code very well could work perfectly, but it's hard to tell without more context.

    Ok, after reading closer, I see the issue. When you call an object method, the first value passed in is always the object itself. What you need to do is extract it. Most people call this "$self" though those from a C++ background call it "$this".

    sub header { my $self = shift; # add this line here. my $html = shift; my $header = shift; my $time = shift;
    After that, you can access your object through $self, and everything will be in the right variables.

      I love you man!
      That was it ....That was it! I should've caught that ...but, live and learn.
      Thanks a million.

      Bub