[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-beginners
Subject:    Re: Pragmas use strict and use warnings
From:       "Jenda Krynicky" <Jenda () Krynicky ! cz>
Date:       2007-10-31 23:57:21
Message-ID: 47292471.2300.1076D445 () Jenda ! Krynicky ! cz
[Download RAW message or body]

On 31 Oct 2007 at 4:17, Paul Lalli wrote:
> On Oct 30, 5:24 pm, Je...@Krynicky.cz (Jenda Krynicky) wrote:
> > On 30 Oct 2007 at 10:22, Paul Lalli wrote:
> > > On Oct 30, 11:15 am, chas.ow...@gmail.com (Chas. Owens) wrote:
> > > > On 10/30/07, Kaushal Shriyan <kaushalshri...@gmail.com> wrote:
> > > > snip> Whats the exact purpose of use strict
> >
> > > > snip
> >
> > > > The strict pragma has three effects (unless modified by
> > > > arguments): 1. most variables must be declared (there are some
> > > > exceptions)
> >
> > > This is a common misperception.  use strict 'vars' in fact does
> > > not require anything to be declared.  All it requires is that
> > > global variables must be fully qualified.  You cannot use the
> > > short-name of globals that belong to the current package.
> >
> > > <snipped>
> >
> > > With strict enabled, the only way to use a short-name of a
> > > variable is to declare a lexical of that name (the right choice)
> > > using 'my', or to disable strict 'vars' on a variable-by-variable
> > > case using 'our' (the wrong choice).  This is what leads people to
> > > assert "use strict forces you to declare your variables".
> >
> > Technicaly you are right, but generally it's better to present use
> > strict 'vars' as a way to force oneself to declare variables.
> 
> I would agree, if the resulting error message was something along the
> lines of "Undeclared variable $foo while using strict", but it's not.
> The error message is "global variable $foo must be fully qualified
> while using strict vars".  When a new Perl programmer sees that
> message, if all they know about using strict is that it "forces you to
> declare your variables", this error message is meaningless, and
> frequently leads to the wrong solution.  They either fully qualify it,
> or they stick an 'our' in front of it.
> 
> Tell newbies what 'strict' actually does, and the error message makes
> sense, and they know what they did wrong and how to fix it.

I think it makes sense even without it. All you have to do is to tell 
them that as they did not declare any variable with that name, Perl 
thinks that they might have wanted to access a global variable and 
tells them that if indeed they do want to access a global variable 
they have to do so explicitely. If I intend the $foo to be a local 
variable and I get a message that it's an ill-acessed global I know I 
did something wrong and that I have to change something to make it 
local. (That's local in the normal meaning, not using the local() 
keyword).

I think that our(), use vars and local() should be banned to newbies. 
Untill you can explain crrectly what exactly do they do, you will be 
slapped whenever I find them in your code. :-)

Jenda



===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic