amw1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm building a data structure from a config file that looks roughly like this (doing a single pass populating hash entries as they are encountered, not looking ahead):
# Define a page page <page key> { toolbar standard; }; # Define a standard toolbar toolbar standard { item { <item config> }; };

When I encounter toolbar standard; in the page config I want to create a blank hash key that will get filled later with the actual definition of toolbar standard. I've tried the following:

# Encounter the toolbar standard; line in the page config # Doesn't actually do anything $page->{<page_key>}{'toolbar'} = $toolbar->{'standard'}; # complains about standard getting re-defined $toolbar->{'standard'} ||= {}; # also tried undef and dummy value ins +tead of {} with no luck $page->{<page_key>}{'toolbar'} = $toolbar->{'standard'}; # $toolbar->{'standard'} is actually populated sometime after I proces +s $page->{<page_key>}

Is there any way to get the toolbar def in the page config point to a bucket that will be filled when the toolbar section is processed?

Replies are listed 'Best First'.
Re: Defining a hash key for later use
by eieio (Pilgrim) on Feb 01, 2005 at 16:52 UTC
    The following seems to work for me. I'm storing a reference in the page hash rather than a value. When $toolbar->{'standard'} is later populated, the value will change and the reference that is stored in the page hash will point to the new value.
    $page->{'page_key'}{'toolbar'} = \$toolbarRef->{'standard'}; $toolbar->{'standard'} = 7; print ${$page->{'page_key'}{'toolbar'}}; #prints 7
      That appears to work. I had a hole elsewhere. *sigh* I should have done a small test case rather than working in my main program. Thanks.
Re: Defining a hash key for later use
by Animator (Hermit) on Feb 01, 2005 at 16:45 UTC

    So what you want to do is just create an element? and set it's value to undef?

    Then you could simply do $page->{....}{'toolbar'}; then the key will be created...

      I've also just tried to set:
      $page->{...}{'toolbar'}{'standard'} = $toolbar->{'standard'};
      before $toolbar->{'standard'} gets defined but it doesn't appear to work. I was expecting it to work as well.

      I may have misstated a detail on the problem. The structure lays out a little more like this:

      # $struct->{'toolbar'}{'standard'} does not exist at # this point. $struct->{'toolbar'} may or may not exist $struct->{'page'}{...}{'toolbar'}{'standard'} = $struct->{'toolbar'}{' +standard'};

      This is more complete.