Re: Global symbol requires explicit package name
by graff (Chancellor) on Aug 15, 2003 at 02:10 UTC
|
antirice and aquarium have answered the immediate question, as to why the code at line 23 generated that particular error. If you follow their advice and simply put my %in; above that offending line, I can anticipate what your next posted question might be...
"Why do I get these warnings about 'use of undefined value at line 24'?" The uncooperative answer will be: "You added that extra line to declare %in, so the for-loop line is now line 24, and you are using the '-w' flag on the initial line of the script, which causes these warnings to be printed to stderr, and you have not assigned any values to the hash array %in, which is what those warnings are complaining about."
So, the next issue for you to address is: what is the hash array %in supposed to contain? Where are these contents supposed to come from? It seems that you expect this hash to have elements keyed by the various strings in the @param array, and since you are using the CGI module, you probably want %in to hold the names and values of parameters that come in from a web form, but you haven't got the code that is needed to put those values into %in.
The man page for the CGI module is very informative -- it's long, but it's worth the time you take to read it. That should make things clearer for you. Look especially at the part titled "FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER".
In your case, it might be best to ignore what the earlier replies suggested, and base your code on a better knowledge of the CGI module. In essence, you don't need a hash array:
$body .= param($_)."\n" for @param;
This uses the "function-oriented" style of CGI usage, calling the "param()" method to return the value of each parameter name, as supplied by the @param array.
update: personally, if I were expecting email from this sort of process, I would prefer that the email message include the parameter name on each line along with the parameter value:
$body .= join("", "$_ : ", param($_), "\n") for @param;
and of course, if any parameter name could be submitted with more than one value, I'd want the email to list all of the values, which could be done like this:
for my $p (@param) {
my @v = param( $p );
$body .= join( "", map { "$p : $_\n" } @v );
}
| [reply] [d/l] [select] |
|
|
You are a perl god. Of all the people you have been most helpful by far. Have got this error now but I don't think it would be too hard to solve. It is: Can't call method "close" without a package or object reference at enquiry02.pl line 36.
It is referring to the line: $mailer->close;
Any ideas?? What could be wrong with the line??
| [reply] |
|
|
use Mail::Mailer (qw/close/);
update: having just looked at the docs on CPAN for Mail::Mailer, it may be that the correct means is:
use Mail::Mailer qw(mail);
Though why this should make a difference is beyond me. (This module is not exemplary in its documentation.) Anyway, you could also try just removing the "close" statement -- when the script exits, the mail object is bound to close. | [reply] [d/l] [select] |
|
|
|
|
|
|
|
Re: Global symbol requires explicit package name
by jeffa (Bishop) on Aug 15, 2003 at 04:31 UTC
|
Ouch. This would all be my fault, as that is
code i posted. I left out the part where you
call CGI::ReadParse() which allows you to retrieve
submitted query variables via this magical global variable named %in - the only catch is that you have to
declare it before you can use with strict turned on, and
you can't use my (but you can use our).
So, the quick fix with the code posted is to remove use strict; (and you might as well take out the -w while yer at it ...) but strict is good, so instead change
the top to this:
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Mail::Mailer;
our %in;
CGI::ReadParse();
# rest of script as posted
However ... i personally don't use (or recommend using)
CGI::ReadParse() - please use CGI::param()
as graff instructed. And if you haven't gone through Ovid's free online
Web Programming with Perl yet, then do so now. Good stuff.
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)
| [reply] [d/l] |
Re: Global symbol requires explicit package name
by antirice (Priest) on Aug 15, 2003 at 01:18 UTC
|
...this is a result of using strict. Your body would contain nothing if strict would allow it to execute. Look at the line:
$body .= "$in{$_}\n" for @param;
Before that line, where are you getting a %in? Set it and your code should work.
Hope this helps.
antirice The first rule of Perl club is - use Perl The ith rule of Perl club is - follow rule i - 1 for i > 1 | [reply] [d/l] |
|
|
Thanks for taking a look. I'm only a newbie to perl so what exactly do you mean by that previous question? How would I go about setting what %in is? Should i just get rid of strict all together??
| [reply] |
|
|
I understand you are just starting out. I have been following your posts as of recent days. Somewhere prior to the use of $in{$_}, you must declare %in with my %in.
I must ask that you read some material prior to your continuation in perl. Check out perldata for information regarding variables and symbols in perl. Check out perlfunc and perlop for information on the various methods and operators available within the language. Be certain to read perlref, perllol and perldsc for more information regarding complex data structures and how they work.
Hope this helps.
antirice The first rule of Perl club is - use Perl The ith rule of Perl club is - follow rule i - 1 for i > 1
| [reply] [d/l] [select] |
|
|
insert a line just above the "for" loop: my %in; ...this declares a hash variable named "in". lookup perlvar section of perl documentation to learn about the basic variable types in perl. keep using strict, especially that you're newbie. it will make you always declare variables. so if you mis-spell a variable name when accessing it, perl won't create it for you automatically as it does without "use strict".
| [reply] |
|
|
|
|
Re: Global symbol requires explicit package name
by dragonchild (Archbishop) on Aug 15, 2003 at 13:33 UTC
|
Your issues have been fixed, but I would like to offer a bit of advice for future issues.
- Go buy a copy of Learning Perl. You can find it at nearly every bookseller. If you can't, Amazon is your friend.
- Read it, cover to cover. And, I really do mean every single word.
- Do every exercise.
- When you have questions, bring them here.
- When you're done, go buy a copy of Programming Perl. That book, as with me, will be your Bible. (I actually do view it as my programming Bible.)
Follow those steps and you will be helped immensely.
------ We are the carpenters and bricklayers of the Information Age. The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6 Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |
|
|
Thanks very much for your help and advice which I will definately take into account. Script is working OK now just waitning on Web host to install an extra module that is required called close.pm. Once this is installed I hope that the script will finally work. Thanks to everyone for all your time and help.
| [reply] |