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

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!

Replies are listed 'Best First'.
Re: Perl namespace issue driving me nuts!
by shmem (Chancellor) on Jul 16, 2007 at 12:19 UTC
    See Re: Odd number of elements error. Calling $tools->display_add_user your object $tools gets passed into that sub as the first argument.

    You want:

    sub display_add_user { my $self = shift; my $cgi = shift; my $dbh = shift; ...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks! That solved it - guess I'm still a true newbie with OOperl.
Re: Perl namespace issue driving me nuts!
by moritz (Cardinal) on Jul 16, 2007 at 12:20 UTC
    Is there a particular reason for calling $cgi->CGI::method instead of $cgi->method?
      Not any more - I thought I was having namespace problems, while I wasn't shifting the object itself...