Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'strict' usage question

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
US
1. In this script, why do not $a and $b get flagged as requiring explicit package names?


#!/usr/bin/perl
use strict;
$a = 1;
$b = $a;
$c = $b;
exit;


d:\cgi-bin\test-strict.pl
Global symbol "$c" requires explicit package name at d:\cgi-bin\test-strict.pl line 5.

2. I want to use use strict but there are some variable exceptions peppered throughout the code. Can I create a block at the beginning that contains the exceptions? I'm thinking along the lines of:


no strict;
$v1;
$v2;

use strict;
$a;
$b;


but the above didn't work. Is there a way?



 
[tt]$a[/tt] and [tt]$b[/tt] have special meaning to perl: they are used in [tt]sort[/tt], and they are package globals.
The best is to never use variables named [tt]$a[/tt] or [tt]$b[/tt].

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
If you ue strict declare the variables with "my":

#!/usr/bin/perl
use strict;
my $r = 1;
my $n = $r;
my $c = $n;
print $c;
exit;

as Franco notes above, avoid using $a and $b except as arguments in the sort function.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
OK. Now I know about $a and $b. Thanks for that.

I understand about my.

2. I want to use use strict for all variables but there are some variable exceptions (those declared elsewhere) peppered throughout the code. I don't want Perl to complain about those. Can I create a block or section at the beginning that contains the exceptions? I'm thinking along the lines of:

#!/usr/bin/perl

no strict vars;
$c; # global defined somewhere else, don't want Perl to complain.

use strict vars;
my $e;
$c=1; # should not complain - no strict
$d=2; # should complain - not my'd
$e=3; # should not complain - my'd
exit;
1

It works as desired, except for $c. How can I make it not complain about $c only (in a section at the beginning -- I don't want to do a no strict everywhere in the code where they occur).
 
at the beginning of your code somewhere:

use vars ($c, $var, %this, @that, etc, etc);

or try:

our ($c, $var, %this, @that, etc, etc);

There are probably other ways:

Code:
use strict:
stuff here

{
   no strict;
   stuff here
}

more stuff here

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin's solution is the way to go here. Just declare every variable that was used under the old paradigm at the beginning of your script. Continue re-running until you solve all the use strict errors.

I would probably stick with a simple my declaration, over the use of our or use vars. This is because with the addition of use warnings, you'll get alerted of declaration of variable overrides previous declaration warnings, which could come in handy during the transition to full use strict compliance.

- Miller
 
Since you are trying to allow "global" variables under "use strict" you should probably be using "our" to declare them.
Personally I would not use "use vars".
Also, remember that there is a very big difference between "my $var;" and "our $var;" (not least in terms of scope).



Trojan.
 
Trojan: Why NOT use vars? It helps to flag accidentally created globals or mistyped local (my) variables.

A scheme that seems to be working well is to use strict; for local variables, but declare the globals as our (...); as someone suggested.

GN
 
I would advocate for the use of our over use vars, unless you're specifically trying to create cpan modules with pre perl 5.6 compatibility.

However, when converting a script to support use strict, I advise the use of my for your pseudoglobal variables while making the conversion. I say this because I believe that our should be reserved for true globals. You use my for those variables that you're waiting to fix scoping of just as a placeholder until you implement full use strict programming practices. This ends up being a clue to other future maintenance programmers who come by your code if you never fully fix them. But just to give them even more help, make sure to add a comment before the my declaration stating why those variables are defined there.

In truth, I would advise you to go ahead and scope all your variables properly, but this is generally a good first step for those are not used to programming under strict guidelines. Especially if the code has other bad programming practices in use.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top