Help with strict

Help with strict

am 23.08.2008 02:00:37 von Barry Brevik

I have seen it said many times, on this list, that we should always
have:

use strict;

I have tried to use this many times, but all it does is give me hundreds
of errors like this:

Global symbol "$version" requires explicit package name at
domainctrl.pl line 10.
Global symbol "%cmptrlist" requires explicit package name at
domainctrl.pl line 18.

All of these are variables created in the main body of the program... I
suppose that they are technically part of the "main" package, but if
strict is trying to tell me that I have to refer to these as
main::$version (for example), I don't think that I am willing to go
there.

Can anyone tell me what is going on here?

TIA,

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with strict

am 23.08.2008 02:13:29 von Bill Luebkert

Barry Brevik wrote:
> I have seen it said many times, on this list, that we should always
> have:
>
> use strict;
>
> I have tried to use this many times, but all it does is give me hundreds
> of errors like this:
>
> Global symbol "$version" requires explicit package name at
> domainctrl.pl line 10.
> Global symbol "%cmptrlist" requires explicit package name at
> domainctrl.pl line 18.
>
> All of these are variables created in the main body of the program... I
> suppose that they are technically part of the "main" package, but if
> strict is trying to tell me that I have to refer to these as
> main::$version (for example), I don't think that I am willing to go
> there.
>
> Can anyone tell me what is going on here?

All you have to do is define them one first use or somewhere prior to
first use for globals.

Rather than saying :

$status = get_status ();

try using 'my' (define and allocate memory) or 'our' (just define) to
predefine the variable prior to use :

our $status; # or my $status = ;

... # some intervening code

$status = get_status ();

or at the time of first use (what I do in most cases unless it needs to
be a global used in other subs in which case you would pre-define) :

my $status = get_status ();

Once you've given your intention to use a variable, you won't get an error
when you actually do and if you do get the error it will help you find
logic erros in your script.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with strict

am 23.08.2008 03:45:23 von lyle

Barry Brevik wrote:
> I have seen it said many times, on this list, that we should always
> have:
>
> use strict;
>
> I have tried to use this many times, but all it does is give me hundreds
> of errors like this:
>
> Global symbol "$version" requires explicit package name at
> domainctrl.pl line 10.
> Global symbol "%cmptrlist" requires explicit package name at
> domainctrl.pl line 18.
>
> All of these are variables created in the main body of the program... I
> suppose that they are technically part of the "main" package, but if
> strict is trying to tell me that I have to refer to these as
> main::$version (for example), I don't think that I am willing to go
> there.
>
> Can anyone tell me what is going on here?
>

It's saying that these variables haven't been defined. E.g. they haven't
been declared local variables with 'my' such as
my $version;
Or you haven't declared them as Global with 'our' such as
our $version;
Note that 'our' is only in Perl 5.8 and up. Previously Globals had to be
declared with use vars, such as
use vars qw( $version );
You can still 'use vars' post Perl 5.8, some people prefer it to 'our'
and it's good for backwards compatibility (and keeping all your globals
listed in one place)


Lyle
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with strict

am 23.08.2008 12:30:15 von Serguei Trouchelle

Lyle wrote:

> Note that 'our' is only in Perl 5.8 and up.

Actually, it is in 5.6 and up.

--
S.T.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Help with strict

am 27.08.2008 12:05:36 von Brian Raven

Barry Brevik <> wrote:
> I have seen it said many times, on this list, that we should always
> have:
>
> use strict;
>
> I have tried to use this many times, but all it does is give me
> hundreds of errors like this:
>
> Global symbol "$version" requires explicit package name at
> domainctrl.pl line 10. Global symbol "%cmptrlist" requires explicit
> package name at domainctrl.pl line 18.
>
> All of these are variables created in the main body of the program...
> I suppose that they are technically part of the "main" package, but
> if strict is trying to tell me that I have to refer to these as
> main::$version (for example), I don't think that I am willing to go
> there.
>
> Can anyone tell me what is going on here?

You have had some good answers, but the following article might help you
understand what is going on.

http://perl.plover.com/FAQs/Namespaces.html

I always get plenty of those errors in new code. They tell me where I
have forgotten a declaration, or more often, identify a typo. In short,
"use strict" helps me avoid some classes of bug.

HTH

--
Brian Raven

------------------------------------------------------------ -----------------------------------------------
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.



____________________________________________________________ __________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
____________________________________________________________ __________
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs