I'll give you a couple answers, in order of usefulness (I hope).

If you don't plan to reuse this code, just put it in, in-line. No packaging worries, nice and simple. Once you have the data structure, the rest of the code just diddles with it.

You could make it nicer by turning it into a subroutine, like this:

sub read_it_and_construct {
   # Arguments are in the @_ array here
   my ($filename) = @_;

   # Create variables for this function only using "my"
   my $DATA;

   # Open the file and read it
   open (INPUT, $filename) or die "Can't open $filename: $!\n";

   # ... your code here, which builds the structure ...
   # (don't forget to use 'my' on any other variables you
   # use in here! Otherwise, they're global and can clobber
   # like-named variables in the main program!)

   # Pass the structure back to the caller
   return $data;
}
The main program would simply say:
my $structure = read_it_and_construct($from_this_filename);
The subroutine gets the file to read as its argument, and returns the structure as its value.

If you want to make it really black-box, you can go object-oriented; I can talk to you about that if you like. Essentially, you write some more subroutines that know how to access the data structure, and return (to the caller) a special kind of reference that knows about these functions, so you can do stuff like

my $filetree = ReadAndConstruct->new($file);
my $value = $filetree->lookup($key1,$key2,$key3);
and so on. Let me know if this sounds useful for you. I'd recommend it only if you plan to use the tree builder in a number of programs.

In reply to RE: the Magic Box system? (use require) by pemungkah
in thread the Magic Box system? (use require) by Buckaroo Buddha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.