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";
In reply to Need help with objects by lv211
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |