Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Problem to inspect scalars in STASH

by educated_foo (Vicar)
on Feb 15, 2012 at 05:08 UTC ( #953834=note: print w/ replies, xml ) Need Help??


in reply to Problem to inspect scalars in STASH

Yes, this is annoying, and even more so when defined(@array) and defined(%hash) become deprecated. The solution I came up with is to use defined($$name) for scalars, and defined(*{$name}{THING}) for the others. I think it works on all current Perls without polluting the stash, e.g.

main @> $Test::x = 1; @Test::y = qw(2 3); %Test::z = (a => 'b'); main @> keys %Test:: y x z main @> join ' ', "SCALARS:", grep defined(${"Test::$_"}), keys %Test: +: 'SCALARS: x' main @> join ' ', "ARRAYS:", grep defined(*{"Test::$_"}{ARRAY}), keys +%Test:: 'ARRAYS: y' main @> join ' ', "HASHES:", grep defined(*{"Test::$_"}{HASH}), keys % +Test:: 'HASHES: z' main @> join ' ', "SCALARS:", grep defined(${"Test::$_"}), keys %Test: +: 'SCALARS: x'


Comment on Re: Problem to inspect scalars in STASH
Select or Download Code
Re^2: Problem to inspect scalars in STASH
by LanX (Canon) on Feb 15, 2012 at 16:05 UTC
    Thanks for your help... :)

    > The solution I came up with is to use defined($$name) for scalars,

    Sorry, that's not a solution because it excludes all declared package variables which became undef in the meantime.

    Think of something like  $flag = () in the middle of the code. I'm trying to fix the tab-expansion in the perldebugger, actually $f#TAB# lists all symbols starting with f, no matter which slot is used.

    DB<223> sub flo {} => 0 DB<224> $f # type TAB $f $find $flag $flo

    As a workaround I could include all symbols where only the scalar slot is defined, but if someone decides to have equally named sub &flo AND scalar $flo where the latter is undef, I can't tell if the scalar belongs to the code or not.

    While this cases seems rare enough - most people tend to use different names for different sigils - it's not a 100% solution.

    If there is no better suggestion, I will need to parse the code to reliably find all scalars.

    >Yes, this is annoying,

    FWIW I consider this behavior a bug.

    Cheers Rolf

      You're right, of course, and I agree that the SCALAR auto-vivification thing is a bug. I remember discussing this with some P5P folks awhile back, and IIRC this auto-creation of the SCALAR slot was fairly well-baked into the core. Still, you think that wandering through the symbol table should be easy -- it's just a hash of GLOBs, right? -- and then you run into all this stuff. *sigh*

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://953834]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2015-07-04 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls