I've been writing a program in perl, and now decided to split the functions into a separate tools.pm file (and make an attempt at OOperl!). However I seem to be getting problems when trying to access the CGI module from my .pm file: adduser.pl
#! /usr/bin/perl -wT #use strict; use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use DBI; use vars qw($DBH $CGI); use lib "./"; use tools; #require Exporter; #our @ISA = qw(Exporter); #our @EXPORT = qw(); $DBH = DBI->connect('DBI:mysql:youtrivia','xxx','xxx', {'RaiseError' = +> 1, 'AutoCommit' => 0}) or die "Couldn't connect: " . DBI->errstr; my $CGI = CGI->new(); my $action = $CGI->param("submit"); print $CGI->header; print $CGI->start_html(-title=>"Add User"); my $tools = tools->new(); $tools->display_add_user($CGI,$DBH); print $CGI->end_html; exit(0);
tools.pm
package tools; use strict; use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use DBI; use Digest::MD5; use vars qw($DBH $CGI); sub new { my ($this) = shift; my $class = ref($this) || $this; my $self = {}; bless ($self, $class); return $self; } sub display_add_user { my $cgi = shift; my $dbh = shift; print "<h1>Add user</h1>"; print $cgi->CGI::start_multipart_form(-method=>'post', -action=>'./adduser.pl', -name=>'add_user'); print "<table>"; print "<tr><td>User</td>"; print "<td>".$cgi->CGI::textfield(-name=>'username',-size=>'100')."< +/td></tr>"; print "<tr><td>Password</td>"; print "<td>".$cgi->CGI::password_field(-name=>'password', -size=>'50')."</td></tr>"; print "</table>"; print $cgi->CGI::submit(-name=>'submit', -value=>'AddUser'); print $cgi->CGI::end_form; } 1;
When I try not using the "CGI::" bit I get an error like so in my browser:
Can't locate object method "start_multipart_form" via package "tools" +at /tools.pm line 155.
The line number refers to the line with start_multipart_form, I cut out some other methods. Shouldn't this work also WITHOUT the CGI:: part? But when I do include CGI:: (i.e. in the code sample above) the text and password fields are displayed ignoring all arguments, with -name written in the text field by default and something else (starred) in the password field, and the two fields are the same size! Can anyone see what I'm doing wrong? Thanks!

In reply to Perl namespace issue driving me nuts! by dariusj

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.