lv211 has asked for the wisdom of the Perl Monks concerning the following question:
I've gotten about half way down with the tutorial and I keep getting the same error "Can't find object method 'new' via ..."
I get the warning TutorialConfig is successfully loaded but the new method doesn't seem to be loading.
Here is the TutorialConfig.pm
Here is the test.pl#!/usr/bin/perl package TutorialConfig; warn "TutorialConfig is successfully loaded!\n"; 1; sub new { my ($class_name) = @_; my ($self) = {}; warn "We just created our new variable...\n "; bless ($self, $class_name); warn "and now it's a $class_name object!\n"; $self->{'_created'} = 1; return $self; } sub read { my ($self, $file) = @_; my ($line, $section); open (CONFIGFILE, $file) or return 0; # We'll set a special property # that tells what filename we just read. $self->{'_filename'} = $file; while ($line = <CONFIGFILE>) { # Are we entering a new section? if ($line =~ /^\[(.*)\]/) { $section = $1; } elsif ($line =~ /^([^=]+)=(.*)/) { my ($config_name, $config_val) = ($1, $2); if ($section) { $self->{"$section.$config_name"} = $config_val; } else { $self->{$config_name} = $config_val; } } } close CONFIGFILE; return 1; } sub get { my ($self, $key) = @_; return $self->{$key}; }
use TutorialConfig; $tut = new TutorialConfig; $tut->read('tutc.txt') or die "Couldn't read config file: $!"; print "The author's first name is ", $tut->get('author.firstname'), ".\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need help with objects
by cLive ;-) (Prior) on Nov 16, 2006 at 04:56 UTC | |
by Fletch (Bishop) on Nov 16, 2006 at 14:40 UTC | |
|
Re: Need help with objects
by chromatic (Archbishop) on Nov 16, 2006 at 05:09 UTC | |
|
Re: Need help with objects
by lv211 (Beadle) on Nov 16, 2006 at 07:34 UTC | |
by f00li5h (Chaplain) on Nov 16, 2006 at 11:29 UTC |