in reply to Passing variable to method in perl
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".
After that, you can access your object through $self, and everything will be in the right variables.sub header { my $self = shift; # add this line here. my $html = shift; my $header = shift; my $time = shift;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing variable to method in perl
by bubnoff (Novice) on Aug 21, 2009 at 02:23 UTC |