defined $var

G

George Mpouras

why this give errors ?

use strict;
use warnings;
#my $var=1;
unless (defined $var) {die "variable \$var is undefined\n"}
print $var;
 
P

Peter Makholm

George Mpouras said:
why this give errors ?

use strict;
use warnings;
#my $var=1;
unless (defined $var) {die "variable \$var is undefined\n"}
print $var;

Because 'use strict' forces you to declare variables. I thinks you are
confused about the difference between not declaring a variable and a
variable having the undefined value. It is two different things.

//Makholm
 
J

Jürgen Exner

George Mpouras said:
why this give errors ?

use strict;
use warnings;
#my $var=1;
unless (defined $var) {die "variable \$var is undefined\n"}
print $var;

Because you didn't declare $var.

"defined()" is about the value of a variable, it has nothing to do with
the existence i.e. declaration of a variable.

jue
 
G

George Mpouras

Jurgen Exner said:
Because you didn't declare $var.

"defined()" is about the value of a variable, it has nothing to do with
the existence i.e. declaration of a variable.

jue

Ok , then what about the existence ?!
 
R

Rainer Weikusat

George Mpouras said:
Ok , then what about the existence ?!

'Existence' doesn't figure here. The variable will spring into
existence upon first use. But when strict 'vars' is being used
(IMO, generally a good idea) then a declaration (our $var) is required
in order to inform the compiler that this variable is actually
supposed to exist.

I suggest to discuss the theory that what hasn't been declared cannot
possibly exist with a customs officer in suitable situation in order
to gain some inside into the meaning of the term 'declaration'.
 
T

Ted Zlatanov

GM> Ok , then what about the existence ?!

You can use Devel::Symdump to look at the full list of declared scalars,
arrays, and hashes, for any scope, including $main.

Ted
 
J

Jürgen Exner

George Mpouras said:
Ok , then what about the existence ?!

What about it?
There are programming languages where a variable is created
automatically on first use. Perl is such a language.
However this methodology has been frowned upon for a long time since it
leads to many hard to find errors where a user simply misspelled a
variable. Therefore 'use strict' alters this behaviour and enforces a
declaration before you can use the variable(*).

jue

*: somewhat simplified, there are a few exceptions
 
R

Rainer Weikusat

Jürgen Exner said:
What about it?
There are programming languages where a variable is created
automatically on first use. Perl is such a language.
However this methodology has been frowned upon for a long time since it
leads to many hard to find errors where a user simply misspelled a
variable. Therefore 'use strict' alters this behaviour and enforces
a declaration before you can use the variable(*).

[rw@sapphire]~ $perl -we '$a = 1'
Name "main::a" used only once: possible typo at -e line 1.

But that's - at best - as unreliable as relying on strict to catch
accidental misspellings. That's something the computer cannot ever do
because doing so requires both understanding the meaning of the
executed code and knowing what effect the program was supposed to
produce.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top