--- David Simcik <[EMAIL PROTECTED]> wrote:
> Newbie question here...
>
> I've created a CGI.pm object and want to pass its reference to a function.
> It keeps on puking though, returning this message:
David,
First bit of advice: don't use prototypes.
http://www.perl.com/pub/a/language/misc/fmproto.html
is an excellent explanation of how prototypes work (or don't) in Perl.
Second: here's a code snippet that does what I *think* you want: i.e., test whether
or not
certain params are present (my apologies if I misguessed your intention:
use strict;
use CGI;
my $req = CGI->new;
my ( $user, $pass, $agreed ) = qw/ user pass agreed /;
if ( !isPresent( $req, $user ) ) {
print "User not present\n";
}
if ( !isPresent( $req, $pass) ) {
print "Password not present\n";
}
if ( !isPresent( $req, $agreed) ) {
print "Agreement not present\n";
}
sub isPresent {
my $query = shift;
my $paramname = shift;
if ( ! defined $query->param($paramname) ) {
return 0;
} else {
return 1;
}
}
Note that I am not not passing a reference to the CGI object. I am passing the scalar
directly
because that, in itself, is already a reference.
Third, do you really need to use 'our'? In your code, you had:
our $req = new CGI();
Many people misunderstand how 'our' is used. There are basically two ways of
declaring variables
in Perl (well, sort of): package and lexical.
A package variable has a package name prepending to it:
$main::foo
$CGI::POST_MAX
@foo::bar
A package variable is global. All packages can access the variable $foo in the main
symbol table
(%main:: or the shorthand %::) by using $main::foo. Global variables a generally a
bad idea, but
do crop up in a lot of code.
A lexically scoped variable is declared with 'my':
my $foo;
my $POST_MAX;
my @bar;
Though they look similar to the package variables above, they are *not* the same and
cannot be
accessed outside of the scope in which they were declared.
If you use strict and you try to use a variable that's not previously declared, you'll
get an
error similar to the following:
Global symbol "$foo" requires explicit package name at C:\test.pl line 2.
Basically, Perl expects that you are trying to use a package variable, but left off
the package
name. The problem with using package names ($main::foo) is that strict ignores these
variables
and if you type $main::field in one place and $main::feild in another place, strict
won't warn you
of the typo.
'our' tries to help out by letting you use package variables without adding the
package name. The
following two variables are the same:
package foo;
use strict;
our $bar;
$foo::bar;
That eliminates the $main::field/$main::feild problem by allowing you to do this:
our $field;
>From there, you just drop package names and the script will run and later will warn
>you if you use
$feild. Unfortunately, there are a lot of problems with this.
The main problem is that you are now using global variables which is generally a Bad
Thing. Any
code, anywhere, can change your global. Large systems with lots of globals typically
suffer this.
This also leads to very subtle coding errors. Try the following code:
use strict;
for ( 1..3) { &doit }
sub doit {
our $foo;
print ++$foo . "\n";
}
That will print 1, 2, and 3 on successive lines. Change that 'our' to a 'my' and it
prints 1, 1,
and 1. Because the my variable goes out of scope at the end of the sub and the our
variable
doesn't (since it's a global variables), you get wildly different results.
Sorry to be long-winded. Just thought you might want to know.
Cheers,
Curtis Poe
=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]