For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > strict behavior with .pm









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author strict behavior with .pm
Jeremy Kister

2007-09-28, 7:01 pm

Given the below code, is there something that will warn/prevent me from
declaring $variable when i really meant @variable ?

I usually use perl -wTc scriptname to check for silliness, but i've
realized code in the below fashion won't be reported. This got me very
under mod_perl, because @variable contained things seemingly
unrelated to what i expected.

__BEGIN__
#!/usr/local/bin/perl
use strict;
use My::Example;
my $example = My::Example->new();
my $ref = $example->go();
__END__


__BEGIN__
package My::Example;
sub new {
return bless({}, shift);
}
sub go {
my $variable;
push @variable, 1;
return(\@int);
}
1;
__END__

--

Jeremy Kister
http://jeremy.kister.net./
Chas. Owens

2007-09-28, 7:01 pm

On 9/28/07, Jeremy Kister <perl-04@jeremykister.com> wrote:
> Given the below code, is there something that will warn/prevent me from
> declaring $variable when i really meant @variable ?

snip
> package My::Example;
> sub new {
> return bless({}, shift);
> }
> sub go {
> my $variable;
> push @variable, 1;
> return(\@int);
> }
> 1;

snip

The strict pragma is lexically scoped, so you need to use it again
after the package statement. It is probably a good idea to turn on
warnings as well.
Tom Phoenix

2007-09-29, 4:00 am

On 9/28/07, Chas. Owens <chas.owens@gmail.com> wrote:

> The strict pragma is lexically scoped, so you need to use it again
> after the package statement. It is probably a good idea to turn on
> warnings as well.


To be sure, I don't think that's an accurate description of the
effects of lexical scoping.

Both 'strict' and 'warnings' are lexically scoped directives. That
generally means that the declaration takes effect at the end of the
declaring statement, and it continues until the end of the smallest
enclosing block or file. A package directive has no effect upon
lexical scope of other directives.

On the other hand, most ordinary modules are package scoped. You have
to "use Carp" separately in each package in which its imported names
are needed, for example; so it may need to appear more than once in a
single file. But 'strict' and 'warnings', when not enclosed in the
curly braces that make a smaller scope, take effect for the rest of
the file.

Using lexically scoped directives within a file (as opposed to at the
top of the file) is generally needed only when requesting something
extra. For example, requesting more warnings than the default ones.

So, as a general style guideline, each file needs to start with a
block of boilerplate, including both "use strict" and "use warnings"
directives.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Paul Lalli

2007-09-29, 7:59 am

On Sep 28, 5:18 pm, perl...@jeremykister.com (Jeremy Kister) wrote:
> Given the below code, is there something that will warn/prevent me from
> declaring $variable when i really meant @variable ?
>
> I usually use perl -wTc scriptname to check for silliness, but i've
> realized code in the below fashion won't be reported. This got me very
> under mod_perl, because @variable contained things seemingly
> unrelated to what i expected.
>
> __BEGIN__
> #!/usr/local/bin/perl
> use strict;
> use My::Example;
> my $example = My::Example->new();
> my $ref = $example->go();
> __END__


Presumably, this __END__ and __BEGIN__ notation means that these are
two different files?

If so, you're only using strict in the first file. strict is a
lexically scoped pragma. It only affects the file in which it is
placed. (It actually only affects the block in which it's placed, but
you didn't place it in any block, so the scope is the file itself)

> __BEGIN__
> package My::Example;


Put:
use strict;
here again, and strictures will be enabled in My/Example.pm as well.

> sub new {
> return bless({}, shift);}
>
> sub go {
> my $variable;
> push @variable, 1;
> return(\@int);}
>
> 1;
> __END__



Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com