Click here to get back home

Perl bug for use strict - be forewarned!!

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl bug for use strict - be forewarned!! Tintin 01-29-2008
Get Chitika Premium
Posted by Tintin on January 29, 2008, 1:18 am
Please log in for more thread options
Hi Folks,

I am hoping I am wrong here but is this a bug with reference to the
code segment below?

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

$a=10;


print $a++ . "\n";
print $a . "\n";

================================================
After compilation and execution ==>
10
11
================================================
Perl version information is as below:
perl -v

This is perl, v5.8.8 built for darwin-thread-multi-2level
(with 10 registered patches, see perl -V for more detail)

Copyright 1987-2007, Larry Wall

Binary build 822 [280952] provided by ActiveState http://www.ActiveState.com
Built Jul 31 2007 19:44:51

Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source
kit.

Complete documentation for Perl, including FAQ lists, should be found
on
this system using "man perl" or "perldoc perl". If you have access to
the
Internet, point your browser at http://www.perl.org/, the Perl Home
Page.

$

================================================
The same code is now modified as below:
#!/usr/local/bin/perl
use strict;
use warnings;

$some_var=10;


print $some_var++ . "\n";
print $some_var . "\n";
================================================
After compilation and execution ==>
10
11
================================================
Global symbol "$some_var" requires explicit package name
Global symbol "$some_var" requires explicit package name
Global symbol "$some_var" requires explicit package name
Execution aborted due to compilation errors.
================================================

I don't see why this code snipped should've executed in the first
instance. I have observed this behavior to be exhibited for the
following single character identifiers ($a, $b) that act as scalar
variable names.

Has anybody has had this experience before? Any thoughts?

Regards,
- Tintin

Posted by Gunnar Hjalmarsson on January 29, 2008, 1:33 am
Please log in for more thread options
Tintin wrote:
> I am hoping I am wrong here but is this a bug with reference to the
> code segment below?
>
> ================================================
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> $a=10;
>
>
> print $a++ . "\n";
> print $a . "\n";
>
> ================================================
> After compilation and execution ==>
> 10
> 11

<snip>

$a and $b don't need to be declared under strictures because they are
special variables.

http://perldoc.perl.org/perlvar.html#%24a

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Tintin on January 29, 2008, 1:39 am
Please log in for more thread options
> Tintin wrote:
> > I am hoping I am wrong here but is this a bug with reference to the
> > code segment below?
>
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > #!/usr/local/bin/perl
> > use strict;
> > use warnings;
>
> > $a=3D10;
>
> > print $a++ . "\n";
> > print $a . "\n";
>
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > After compilation and execution =3D=3D>
> > 10
> > 11
>
> <snip>
>
> $a and $b don't need to be declared under strictures because they are
> special variables.
>
> http://perldoc.perl.org/perlvar.html#%24a
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl

Thank You Sir!! I was reading documentation from the camel and I must
have missed the point. Apologies for the trouble & many thanks for
your time.

Posted by Peter Makholm on January 29, 2008, 1:33 am
Please log in for more thread options

> I don't see why this code snipped should've executed in the first
> instance. I have observed this behavior to be exhibited for the
> following single character identifiers ($a, $b) that act as scalar
> variable names.

This is not a bug but a feature and well documented in the
documentation for the strict pragma 'perldoc strict':

Because of their special use by sort(), the variables $a and $b
are exempted from this check.

//Makholm

Posted by Ron Bergin on January 29, 2008, 1:38 am
Please log in for more thread options
> Hi Folks,

Hi TinTin,

>
> I am hoping I am wrong here but is this a bug with reference to the
> code segment below?
>
> ================================================
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> $a=10;
>
> print $a++ . "\n";
> print $a . "\n";
>
> ================================================
> After compilation and execution ==>
> 10
> 11
> ================================================

quoted from perldoc perlvar
$a
$b Special package variables when using sort(), see "sort" in
perlfunc. Because of this specialness $a and $b don't need
to be
declared (using use vars, or our()) even when using the
"strict
'vars'" pragma. Don't lexicalize them with "my $a" or "my
$b" if
you want to be able to use them in the sort() comparison
block
or function.

> Perl version information is as below:
> perl -v
>
> This is perl, v5.8.8 built for darwin-thread-multi-2level
> (with 10 registered patches, see perl -V for more detail)
>
> Copyright 1987-2007, Larry Wall
>
> Binary build 822 [280952] provided by ActiveStatehttp://www.ActiveState.com
> Built Jul 31 2007 19:44:51
>
> Perl may be copied only under the terms of either the Artistic License
> or the
> GNU General Public License, which may be found in the Perl 5 source
> kit.
>
> Complete documentation for Perl, including FAQ lists, should be found
> on
> this system using "man perl" or "perldoc perl". If you have access to
> the
> Internet, point your browser athttp://www.perl.org/, the Perl Home
> Page.
>
> $
>
> ================================================
> The same code is now modified as below:
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> $some_var=10;
>
> print $some_var++ . "\n";
> print $some_var . "\n";
> ================================================
> After compilation and execution ==>
> 10
> 11
> ================================================
That's odd. Here's the results I get.

C:\test>type Tintin2.pl
#!/usr/local/bin/perl
use strict;
use warnings;

$some_var=10;

print $some_var++ . "\n";
print $some_var . "\n";

C:\test>Tintin2.pl
Global symbol "$some_var" requires explicit package name at C:\test
\Tintin2.pl line 5.
Global symbol "$some_var" requires explicit package name at C:\test
\Tintin2.pl line 7.
Global symbol "$some_var" requires explicit package name at C:\test
\Tintin2.pl line 8.
Execution of C:\test\Tintin2.pl aborted due to compilation errors.

> Global symbol "$some_var" requires explicit package name
> Global symbol "$some_var" requires explicit package name
> Global symbol "$some_var" requires explicit package name
> Execution aborted due to compilation errors.
> ================================================
>
> I don't see why this code snipped should've executed in the first
> instance. I have observed this behavior to be exhibited for the
> following single character identifiers ($a, $b) that act as scalar
> variable names.
>
> Has anybody has had this experience before? Any thoughts?
>
> Regards,
> - Tintin

C:\test>perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 33 registered patches, see perl -V for more detail)


Ron
aka FishMonger

Similar ThreadsPosted
Using strict and warnings in embedded perl July 14, 2004, 4:11 am
specifying use strict January 5, 2005, 12:23 pm
use strict and s///ee November 11, 2005, 2:24 pm
using strict November 19, 2005, 4:01 am
Can't get past 'use strict' :( December 15, 2004, 5:37 pm
use strict; and O_WRONLY February 17, 2005, 7:08 pm
why I gave up -w and "use strict" August 25, 2005, 11:25 pm
why is important to use : use strict? September 19, 2005, 2:31 pm
strict ref problem October 10, 2005, 11:29 pm
using no strict "refs" January 10, 2007, 3:59 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap