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


In reply to Understanding and building modules by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.