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

So I'm learning oop through this tutorial I found.
Perl Objects Tutorial

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

#!/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}; }
Here is the test.pl
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
    add:
    use strict; use warnings;
    to your code (both the module - under the package declaration - and the script), and remove the shebang line (#!/...) from the module (not needed). Then you m ight get a better idea what the error is.

      While a #! isn't needed, it's often useful to have as many utilities (e.g. file) and editors (vim, the various emacsen) will key off it to determine the file's type. Granted some of these may use the file's extension as well (the editors will) if the shebang is missing, but it really doesn't hurt anything to have it there.

Re: Need help with objects
by chromatic (Archbishop) on Nov 16, 2006 at 05:09 UTC

    I downloaded the TutorialConfig module from the article and copied your test program. It worked for me. Did you copy and paste the code? What's the exact error message you get? How do you run the program?

Re: Need help with objects
by lv211 (Beadle) on Nov 16, 2006 at 07:34 UTC
    Ok. I deleted the shebang and now it works.

    Thanks for your help guys.

      Really? won't a shebang look like a comment to perl, what with it begining with a # or should i go and read perlmodtut again?

      Update: i just figured out how to update my own nodes

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;