Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

variable definition error not caught when using strict and warnings

9 views
Skip to first unread message

Nemana, Satya

unread,
Nov 9, 2012, 12:08:00 PM11/9/12
to begi...@perl.org
Hi

I am a little confused with this program

Program:
use strict;
use warnings;

if ($999 == 1056)
{
print ("\nequal");
}
else
{
print ("\nnot equal");
}

What I expect: Perl to throw me an error as $999 variable is not defined, but perl executes the code with a warning.(I don't expect the code to compile at all)

Output:
Use of uninitialized value in numeric eq (==) at wrong1.pl line 4.

not equal


However, if I replace $999 with $num (which is not defined or initialized), I get the following error

Global symbol "$num" requires explicit package name at wrong1.pl line 4.
Execution of wrong1.pl aborted due to compilation errors.

What is happening with the top program?

Thanks in advance
sn

Robert Wohlfarth

unread,
Nov 9, 2012, 12:27:21 PM11/9/12
to begi...@perl.org
On Fri, Nov 9, 2012 at 11:08 AM, Nemana, Satya <sne...@sonusnet.com> wrote:

> Program:
> use strict;
> use warnings;
>
> if ($999 == 1056)
> {
> print ("\nequal");
> }
> else
> {
> print ("\nnot equal");
> }
>
> What I expect: Perl to throw me an error as $999 variable is not defined,
> but perl executes the code with a warning.(I don't expect the code to
> compile at all)
>

Perl interprets $999 as "the nine hundred and ninety-ninth sub-group of the
last regular expression". It's a special variable. It has no value, which
is what the warning tells you.

Perl expects program variables to start with a letter or underscore. So
"$a999" or "$num" work like you expected.

You can find more about variable names here:
http://perldoc.perl.org/perldata.html.

--
Robert Wohlfarth

Andy Bach

unread,
Nov 9, 2012, 12:30:06 PM11/9/12
to Nemana, Satya, begi...@perl.org
On Fri, Nov 9, 2012 at 11:08 AM, Nemana, Satya <sne...@sonusnet.com> wrote:

>
> if ($999 == 1056)
> {
> print ("\nequal");
> }
> else
> {
> print ("\nnot equal");
> }
>
> What I expect: Perl to throw me an error as $999 variable is not defined,
> but perl executes the code with a warning.(I don't expect the code to
> compile at all)


$999 is, in a sense, one of the Perl "magic" variables. This is why the
definition of a Perl variable name doesn't allow the first character to be
a digit. The $<digit> variables are for the RE capture storage - in this
case, your asking for what was saved by 999th set of capture parens - try:
use strict;
use warnings;

"1056" =~ /(\d+)/;
if ($1 == 1056) {
print ("equal\n");
}
else {
print ("not equal\n");
}




--

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Rob Dixon

unread,
Nov 9, 2012, 12:17:15 PM11/9/12
to begi...@perl.org, Nemana, Satya
Hello Satya

The variable $999 holds the 999th capture from the most recent regular
successful expression match. Since you have no regular expressions in
the program it is left undefined. It would be very unusual to have so
many captures in a regex but Perl does support it.

Rob

Nemana, Satya

unread,
Nov 9, 2012, 4:00:40 PM11/9/12
to Rob Dixon, begi...@perl.org

________________________________________
From: Rob Dixon [rob....@gmx.com]
Sent: 09 November 2012 17:17
To: begi...@perl.org
Cc: Nemana, Satya
Subject: Re: variable definition error not caught when using strict and warnings

Hello Satya

Rob


Hi Rob

Thank you.
I will give a read of the perl regular expression manual.
Actually $999 resulted in a typo was i wanted to compare another variable with 999.
Not from a regular expression.
But it took a while for me to find the culprit as the intended code was never hit.
I was trying to understand why I never got an error during the compile itself.

Thanks for answering.

Regards,
Satya

0 new messages