How to test if global has been assigned to?

Discussion in 'Perl Misc' started by kj, Mar 11, 2009.

  1. kj

    kj Guest

    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
    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
     
    kj, Mar 11, 2009
    #1
    1. Advertising

  2. kj

    Uri Guttman Guest

    >>>>> "k" == kj <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Mar 11, 2009
    #2
    1. Advertising

  3. kj

    Tim McDaniel Guest

    In article <gp9187$hlg$>,
    kj <> wrote:
    >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.

    --
    Tim McDaniel,
     
    Tim McDaniel, Mar 11, 2009
    #3
  4. On 2009-03-11, kj <> wrote:
    *SKIP*
    > 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).

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, Mar 12, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cGFhcGEyMQ==?=

    Variable is used before it has been assigned

    =?Utf-8?B?cGFhcGEyMQ==?=, Jan 8, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,892
    Karl Seguin [MVP]
    Jan 8, 2006
  2. Anon
    Replies:
    39
    Views:
    2,221
    Joona I Palaste
    May 2, 2004
  3. Ken Adams
    Replies:
    3
    Views:
    14,933
    Beauregard T. Shagnasty
    Mar 15, 2005
  4. subbu
    Replies:
    1
    Views:
    1,418
    Andrew Thompson
    Jul 27, 2006
  5. bill.wu
    Replies:
    0
    Views:
    318
    bill.wu
    Jan 17, 2008
Loading...

Share This Page