Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

strict problem

by december (Pilgrim)
on Mar 20, 2002 at 01:21 UTC ( [id://152898]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
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)

      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.

        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.

        Go re-read the docs (or i should say: go re-read the examples & source since cgi-lib.pl is so horribly documented)...

        # 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.

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.

      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.

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
      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!
        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.

        But if I call an external procedure, won't this procedure inherit the environment of it's caller - and therefor all of it's variables be within the scope of that very block?

      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...

(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?)

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      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

      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!
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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://152898]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found