december has asked for the wisdom of the Perl Monks concerning the following question:
'lo,
I have a bit of a stupid question - I'm trying to use 'strict', and it's giving me headaches...
I have a variable that is returned by a function in another file (@in, in function ReadParse from the well-known cgi-lib.pl). Without strict, I can just execute &ReadParse, and use $in{'...'}; but with strict, it expects me to declare this variable. If I declare it locally in my perlscript (my (@in);) then it uses a different variable (which is logical, it declares a new one in the scope of the current function which 'overwrites' the other var)... So how do I use a variable from a included library-file if strict is on?
I thought something like 'my (@in) = &ReadParse;' would work, but it doesn't... :(
All explanation welcome, I want to understand this good for once and for all (that's why I want strict).
/ december
Title edit by tye as single-word titles complicate future simple searches
Re: strict
by hossman (Prior) on Mar 20, 2002 at 01:45 UTC
|
First comment, take a look at CGI,
It comes standard with any recent version of perl.
(The cgi-lib site may say it "has become the de facto
standard library" for CGIs, but you'll aso notice that
page was last updated in 1998.)
Second comment: whenever post to perlmonks, you should
allways post your acctual code (or the smallest example you can write that causes hte same error),
and the exact error you get -- it helps people help you
Third comment: if you must use cgi-lib.pl, please read the documentation and examples more carefully (&ReadParse doesn't return an array)
(Edited: i just realized that
[CPAN://CGI] isn't as
usefull of a link as I thought, so i
updated it to point to the local copy of the CGI perldoc)
| [reply] [d/l] |
|
1) I will take a serious look at CGI - thanks for the pointer!
2) ok, I'll remember - I just didn't want to flood this forum for a question that seems as trivial as this one.
3) I know cgi-lib returns a hash, sorry - I was just playing around with everything, and got a bit confused because of the late hour.
| [reply] |
|
# Returns >0 if there was input, 0 if there was no input
# undef indicates some failure.
It doesn't return a hash, or an array.
| [reply] [d/l] |
|
Re: strict
by chromatic (Archbishop) on Mar 20, 2002 at 02:19 UTC
|
If you're accessing a variable named "in" with curly brackets, it's probably a hash. Also, if I recall correctly, ReadParse might be trying to set a package variable. If you're determined to do this (and CGI.pm is a much better alternative), you may have better luck with use vars qw( %in );
Don't pass up the opportunity to move to the far superior CGI.pm, though. | [reply] [d/l] |
|
It's a hash, yes - I was just getting confused (and pissed) after playing around with this - the whole script works, except for this rotten variable-declaration with strict.
And I'll check out CGI.pm - I have it installed everywhere, anyway - thanks.
| [reply] |
strict and variable scopes
by flocto (Pilgrim) on Mar 20, 2002 at 08:43 UTC
|
Regardless of other circumstances (such as using CGI.pm instead of lib-cgi.pl) here's how to define a global variable under strict:
#!/usr/bin/perl -w
use strict;
use vars qw/$my_very_own_global_variable_one $foo $three/;
# rest of the script goes here..
Please consider reading some documentation on variable scope if you have any further questions..
octo
| [reply] [d/l] |
|
An important thing to keep in mind, is that you are defining a lexical variable with my @in. This means it will only be in scope within that block, unless you use a method like flocto mentions above. Though it can be useful to subvert perl (sometimes essential?), one should always consider what is really trying to be accomplished and whether that subversion is truly necessary.
There may be a semantics problem here, too. One of the reasons that my was introduced to perl to was to give perl true local variabls, as local was a work around using a technique of substituting globals. What you would be telling perl is "please make this a local variable, but then don't be strict with me and let me use it anywhere I want without chiding me" (I hope that doesn't sound too harsh!). If you really want global variables, you probably want to use what perl terms as "package" variables:
Instead of using my, you would declare the package that you are in (probably just "main" in this case) like so:
$main::in = ...
Hope that helps. By the way, there are all kinds of nodes here discussing local, my, our etc., and how various monks deal with scoping. Best of luck!
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
|
That actually worked... Thanks!
I don't remember seeing something saying literally here's how to define a global variable under strict - which was all I needed to hear (for now).
I'll read up on variable scoping soon, I'm just learning here... (like all of us, I suppose). Anyway, it wasn't clear to me after reading the docs at hand.
See you around...
| [reply] |
(crazyinsomniac: questions) Re: strict expects me to declare this variable
by crazyinsomniac (Prior) on Mar 20, 2002 at 08:49 UTC
|
I have a bit of a stupid question - I'm trying to use 'strict', and it's giving me headaches...
Hmm, I'm sorry, but stupid questions are especially forbidden (tsk tsk).
Watch me "bold" some key words (ok)
I have a variable that is returned by a function in another file (@in, in function ReadParse from the well-known cgi-lib.pl). Without strict, I can just execute &ReadParse, and use $in{'...'}; but with strict, it expects me to declare this variable. If I declare it locally in my perlscript (my (@in);) then it uses a different variable (which is logical, it declares a new one in the scope of the current function which 'overwrites' the other var)... So how do I use a variable from a included library-file if strict is on?
You only see errors, when you use strict, so ... What is this strict?
Does it have documentation? And more importantly, does that documentation say anything useful?
The error message that "it" expects you to declare this variable, so ....
What are all the ways you can think of declaring a variable?
Where would you read about declaring variables?
What would you have done without perl monks? Hopefully start asking these questions yourself.
After which, you should've stumbled accross perldata and strict, and read like a mad man. In case you didn't stumble accross those, then you need to read How To Read The Friendly Manual which very nicely outlines numerous perl resources, as well as general strategy for finding the answers you're looking for.
Peace! Start communication ;)
update: Great. I would like to point out however, and I did not provide the exact answer as many others have (what would be the point?)
| [reply] |
|
I have read perldata; I am pretty familiar with the concept of a man page and documentation. I also have read the part involving 'strict' very attentive, but could not derive how I'm supposed to declare a variable used (and set) in another file - which I don't wish to touch.
It's nice to rtfm me, but I'm not new to opensource, the 'net, development, *nix or whatever - I just couldn't find this directly in the (extensive) documentation, and it's not important enough to spend a month of thinking on.
I _am_ quite new to perl, though. And I tried all the ways I could think of declaring a variable already.
So, thanks for your referrals, but - don't you know the answer yourself?... - your answer, as it stands, is not really of much help.
:P
| [reply] |
|
Hmm, this was not particularly helpful :)
I would like to point out that:- there are no stupid questions (answers...maybey :)
- The poster took the time to create an account and actually login
- The poster is actually trying to use strict! Which we at the monastary constantly preach
- The original post did mention he was trying to declare the array, but was not having any luck with syntax.
On the other hand, you were right, the info could be found in the man pages.
"Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
| [reply] |
Re: strict
by braughing (Sexton) on Mar 20, 2002 at 01:54 UTC
|
It sounds like you want "local", which gives a global variable a local value, rather than "my" which declares a new lexical variable. Use a block to localise the effect of local: { local @in;
ReadParse();
print $_ foreach (@in);
}
I don't know cgi-lib.pl, or much Perl at all, so maybe this is wrong or off-target. | [reply] [d/l] [select] |
|
No, I actually want the hash 'in' (sorry for that '@' - it was just a left-over from all things I tried) to be known in the entire script, globally, not in the scope of the lib, 'main' or any arbitrary block. As it was without strict. But since the variable gets initialized and set in the included file, I wonder how I can declare it in the main script globally so it gets used even within this scope.
| [reply] |
|
|