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

I'm trying to build a module but, I don't have clear understanding of how to go about this. Module (../site-perl/5.8.2/Mapps/Auth.pm):
package Mapps::Auth; use Digest::SHA1; use DBI; use warnings; use strict; sub new { my $self = {}; bless $self; return $self; } sub auth { my ($uname, $passwd, $dbsecret, $salt); $uname = shift; $passwd = shift; my $dbh = DBI->connect('dbi:mysql:mapps', 'mapps', 'somepasswd'); # get secret from db my $statement="SELECT auname, passwd, salt FROM admin_users, secrets WHERE users.auid=secrets.auid AND auname='$uname';"; my $sth = $dbh->prepare($statement) or die "Couldn't prepare state +ment: $dbh->errstr"; $sth->execute or die "Couldn't execute statement: $dbh->errstr"; while (my $ref = $sth->fetchrow_hashref){ $dbsecret = $ref->{'passwd'}; $salt = $ref->{'salt'}; } # encrypts password using # SHA-1 algorithm my $sha1 = Digest::SHA1->new; # reset algorithm $sha1->hexdigest; # encrypt my $secret = Digest::SHA1::sha1_hex($passwd . $salt); # does generated secret match database secret? if ($secret eq $dbsecret){ my $auth = 1; return $auth; # no match }else{ my $auth = 0; return $auth; } } 1;
Calling the module:
use Mapps::Auth; $auth = new Mapps::Auth->auth($uname, $passwd); if ($auth == 1){ my $s = Mapps::Session->new(); my $id = Apache::Session::Generate::MD5::generate(); $s->new_session($id); my $sid = $s->sid(); # show session from Jeremy's test script printf("SID %s\n", $s->sid()); printf("UID %s\n", $s->uid()); $s->param('foo','123 456'); $s->param(bar =>' baz'); $s->update_session($s->sid()); undef($s); my $x = Mapps::Session->new(); $x->load_session($sid); my @keys = $x->param(); foreach my $key (@keys) { printf("%s => %s\n", $key, $x->param($key)); } # else no auth }else{ print h2('No authentication'); }

I'm getting the error: Can't locate object method "new" via package "Mapps::Auth"

Can some kind monk please explain the nature of creating modules and using them?

Neil Watson
watson-wilson.ca

edited: Sat Apr 3 21:53:56 2004 by jeffa - corrected title misspelling, added readmore

Replies are listed 'Best First'.
Re: Understanding and building modules
by jeffa (Bishop) on Apr 03, 2004 at 20:22 UTC
Re: Understanding and building modules
by BUU (Prior) on Apr 03, 2004 at 22:04 UTC
    $auth = new Mapps::Auth->auth($uname, $passwd); Just guessing, but this code looks really, really wrong. I'm not sure exactly what it'll do (my best guess is it calls the method "new" on the return of auth->auth, but indirect function calls are funky).

    Try changing it to two lines and see if it fixes your problems:
    $auth = Mapps::Auth->new(); $auth->auth($uname, $passwd);
    Or alternatively Mapps::Auth->new()->auth($uname,$passwd);
      And while we are at it, change:
      sub auth { my ($uname, $passwd, $dbsecret, $salt); $uname = shift; $passwd = shift; ... }
      to
      sub auth { my ($self, $uname, $passwd) = @_; my ($dbsecret, $salt); ... }
      or the method call will not work. Here is a little test script i came up with. Looks like the problem isn't in the constructor as i originally suspected, but instead in that wacky method call/instantiation.
      ref new Foo or warn "example 1 is not an object\n"; ref new Foo->foo or warn "example 2 is not an object\n"; package Foo; sub new { bless {} } sub foo { "foo" }

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Undertanding and building modules
by gmpassos (Priest) on Apr 03, 2004 at 20:29 UTC
    To use a module, first you need to load it. Soo, your code should be:
    use Mapps::Auth ; $auth = new Mapps::Auth->auth($uname, $passwd); ...
    Note that your module need to be in some @INC path. To see what directories your Perl interpreter is lookin for modules, get the output of this code:
    foreach my $INC_i ( @INC ) { print "$INC_i\n" ; }
    Here the output is:
    C:/Perl/lib C:/Perl/site/lib .

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      Yes, I do have use Mapps::Auth; and it is loaded in a place where perl can find it.

      Neil Watson
      watson-wilson.ca

        Do you in fact have a directory "Mapps" containing a module "Auth.pm", because as far as i now this is where Perl will look for it (e.g. current directory).
        To me it really looks like Perl can not find the module at all.

        You also could include a use diagnostics; in your code or take everything away but the new method and try to load the module and just call new.

        I also always bless the self hash inside the new method, because otherwise you later may get an error like can't call method "auth" on unblessed reference

Re: Understanding and building modules
by tachyon (Chancellor) on Apr 04, 2004 at 06:34 UTC
Re: Understanding and building modules
by matija (Priest) on Apr 03, 2004 at 20:28 UTC
    Your example doesn't say: Did you do a
    use Mapps::Auth;
    in your code? Because that's the kind of message I get when I forget to do that.
Re: Understanding and building modules
by TStanley (Canon) on Apr 04, 2004 at 05:22 UTC