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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use Strict

Status
Not open for further replies.

cginewbie

Programmer
Jan 20, 2002
24
US
HI,
If my CGI program have only 1 file, then
use Strict

would signals un-defined variables

However, if I have several code library that will be imported via the `use` statement, use Strict wont work on that sense

So How do I strict programs that have several libraries.

kevin
 
This is a simple example of doing that. Hopefully it illustrates the approach you are trying.

Code:
--------------------------------------------
FIRST FILE
--------------------------------------------

#!/usr/local/bin/perl
use strict;

require "./strict2.pl";
require "./strict3.pl";

my $name = getName();
my $age = getAge();

print "NAME: $name\tAGE: $age\n";


--------------------------------------------
SECOND FILE (./strict2.pl)
--------------------------------------------
#!/usr/local/bin/perl
use strict;

my $name = 'John';

sub getName {
return("$name");
}

1; # returns true for the 'require'


--------------------------------------------
THIRD FILE ((./strict3.pl))
--------------------------------------------
#!/usr/local/bin/perl
use strict;

my $age = '39';

sub getAge {
return("$age");
}

1;
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks
So i just simple put the
use Strict
directive on every code library that I want to "use Strict"?

I thought I only have to put it up on the executable files only.....

thanks for your tips
 
If you use 'use strict' in the main piece of code, then you must control the scope of all variables strictly. While I have 'use strict' in the 'required' second and third pieces of code, the thing that is really satisfying the 'use strict' in the first piece of code is the use of 'my' for ALL variables. 'my' controls the scope of the variable and thus makes the 'strict' pragma happy. That does not mean, however, that you are doing anything reasonable or appropriate with the scope of that variable. So, be deliberate about controlling the scope how and where you want and should.

You can see that scope of $last in the second chunk below is restricted to that second chunk. If you try to run those, you'll get an error. The 'my $last....' in the second (required) chunk will not be visible in the first chunk and you'll get a gripe in the print line of the first chunk.

Code:
--------------------------------------------
FIRST FILE
--------------------------------------------

#!/usr/local/bin/perl
use strict;

require "./strict2.pl";
my $name = getName();
print "NAME: $name $last\tAGE: $age\n";


--------------------------------------------
SECOND FILE (./strict2.pl)
--------------------------------------------
#!/usr/local/bin/perl
use strict;

my $name = 'John';
my $last = 'Smith';
sub getName {
return("$name");
}
1; # returns true for the 'require'
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top