CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Strict variable checking

 



RayStreet
Deleted

Jan 16, 2000, 8:38 PM

Post #1 of 5 (981 views)
Strict variable checking Can't Post

I need to put a script onto an ISP's server but they say that the script needs to compile OK when useing the strict option. So I included the strict option and got errors.

I've got a piece of code like this:

$loc = "dummy";

while ($loc = <LOCINF> ) {
chomp($loc);
print ("<option value=\"$loc\">$loc</option>");
}

and I get compile errors like this:

Global symbol "loc" requires explicit package name at /home/virtual/3webfeet/cgi-bin/dc_search_ex.cgi line 76.
Variable "$loc" is not imported at /home/virtual/3webfeet/cgi-bin/dc_search_ex.cgi line 78.

If I change the variable name from $loc to $_ then all works OK. Can anybody explain what is wrong? I've asked someone and they started talking about lexical variables and scope but I don't really know what they are talking about. Any simple explanation? Any tutorial I can go to for more help?

Thanks



Borderline
Deleted

Jan 16, 2000, 9:25 PM

Post #2 of 5 (981 views)
Re: Strict variable checking [In reply to] Can't Post

Hi,

The progma strict does several things.
1) Forces you to scope your variable or qualify them with there full package name.
2) Does not allow you to use symbolic references.
3) Does not allow barewords.
There are two methods of scoping.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# The perfered method
my $loc = 'dummy';
# or
local $loc = 'dummy';
</pre><HR></BLOCKQUOTE>
The my operator declares one or more private variables to to exist only within the inner most enclosing block or file.
The local operator declares one or more global variables to have localy scoped values within the innermost enclosing block or file.
$_, %ENV, @ARGV erc... are all global variables that do not need to be scoped unders strict. In fact you can not scope them using the my operator. This is why it would compile using $_ instead of $loc. $_ is special.
The alternative to scoping your variables is to fully qualify them with there pachage name. In this case it would be package main. So you would write all the variables you do not want to scope like this <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$main::variable</pre><HR></BLOCKQUOTE> or you could declare the variables you want to use as global variables using the vars progma like this <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use vars '$variable';</pre><HR></BLOCKQUOTE>

Hope this helped
Scott


RayStreet
Deleted

Jan 16, 2000, 10:05 PM

Post #3 of 5 (981 views)
Re: Strict variable checking [In reply to] Can't Post

Thanks for your help - it makes some sense now.

Ray


japhy
Enthusiast

Jan 17, 2000, 5:11 AM

Post #4 of 5 (981 views)
Re: Strict variable checking [In reply to] Can't Post

Errr, when using strict, you can't say

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


local $var;
</pre><HR></BLOCKQUOTE>

unless $var has previously been OK'ed by the strict pragma. That can be done in the ways you already prescribed:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use vars qw( $foo );
# or
$package::foo = "value";
</pre><HR></BLOCKQUOTE>

Just as long as that point is made clear.


Borderline
Deleted

Jan 17, 2000, 5:16 AM

Post #5 of 5 (981 views)
Re: Strict variable checking [In reply to] Can't Post

My mistake.

Scott

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives