How to test if global has been assigned to?

K

kj

With hashes one can use the exists predicate to distinguish between
values that have not been assigned to from those that have been
assigned undef:

my %foo = ( bar => undef );

my $have_bar = exists $foo{ bar }; # true
my $have_baz = exists $foo{ baz }; # false

(Actually, autovivification introduces a wrinkle here, but I don't
want to get into that.)

Is there a way to make the analogous distinction between the case
where a global variable has not been initialized from the one where
it has been explicitly initialized with the value "undef"?

Thanks in advance!

kynn
 
U

Uri Guttman

k> With hashes one can use the exists predicate to distinguish between
k> values that have not been assigned to from those that have been
k> assigned undef:

k> my %foo = ( bar => undef );

k> my $have_bar = exists $foo{ bar }; # true
k> my $have_baz = exists $foo{ baz }; # false

k> (Actually, autovivification introduces a wrinkle here, but I don't
k> want to get into that.)

not in those examples. autoviv only matters in deeper accesses or when
used as an lvalue where there is an undef value where a ref should be.

k> Is there a way to make the analogous distinction between the case
k> where a global variable has not been initialized from the one where
k> it has been explicitly initialized with the value "undef"?

why do you care? if you have to, use another hash which has this
variable instead of a global. globals are BAAAAD! (unless you know why
you need one).

uri
 
T

Tim McDaniel

Is there a way to make the analogous distinction between the case
where a global variable has not been initialized from the one where
it has been explicitly initialized with the value "undef"?

My first thought: if it's a global variable in the sense of a package
variable (as opposed to "my" lexical variables), then it is or is not
in the package's symbol table, and that's a hash %package_name:: .

However,

#! /usr/bin/perl -w

print ('never_used: ', (exists $main::{never_used} ? 1 : 0), "\n");

print ('narf: ', (exists $main::{narf} ? 1 : 0), "\n");
$narf = 3;
print ('narf: ', (exists $main::{narf} ? 1 : 0), "\n");

print ('barf: ', (exists $main::{barf} ? 1 : 0), "\n");
eval { $barf = 3; };
print ('barf: ', (exists $main::{barf} ? 1 : 0), "\n");

print ('zarf: ', (exists $main::{zarf} ? 1 : 0), "\n");
eval '$zarf = 3;';
print ('zarf: ', (exists $main::{zarf} ? 1 : 0), "\n");

print ('arf: ', (exists $main::{arf} ? 1 : 0), "\n");
eval '$arf = 3;';
print ('arf value: ', (defined $arf ? $arf : 'huh'), "\n");
print ('arf: ', (exists $main::{arf} ? 1 : 0), "\n");

exit 0;

in Perl 5.10 on Windows, prints

Name "main::narf" used only once: possible typo at
local/test/055.pl line 6.
Name "main::barf" used only once: possible typo at
local/test/055.pl line 10.
never_used: 0
narf: 1
narf: 1
barf: 1
barf: 1
zarf: 0
zarf: 1
arf: 1
arf value: 3
arf: 1

So I guess the compiler has to be parsing everything it can, noticing
the assignments OR uses of $narf and $barf and $arf at compile time,
and putting them into the symbol table from the start.

So this is a dead end that doesn't help you excep tin the most limited
of cases. Sorry.
 
E

Eric Pozharski

On 2009-03-11 said:
Is there a way to make the analogous distinction between the case
where a global variable has not been initialized from the one where
it has been explicitly initialized with the value "undef"?

I've got your question this way: How I can know if global variable was
declared (C<use strict> provided) and set to C<undef> explicitly rather
than put in symbol table by blind referencing it on right side of
assignment or in I<@_> of some function or subroutine?

The answer to *this* question is simple -- look in symbol table. That's
possible there's module what can do it for you (B<Devel::>? since I'm
not interested, I'm not going to search one for you). Anyway you can
see debugger's code -- it somehow can output variables in any requested
namespace (even lexicals).
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top