First, use strict and use warnings. Next, your initialization of the nested hashes won't work because the buckets of a hash can contain only scalars, not hashes themselves, which means you need to use references. my (%agency,(%url,(%module,(%type,$type_count)))); does not create nested hashes, it just passes nested lists to my. Also, assuming you have a nested structure you cannot access it like $agency{$e4,{$e7,{$e6,$pass_count++}}};, but rather like $agency{e4}{e7}{e6} = $pass_count++;. Note though that $pass_count is a post-incremented number -- not a reference -- and so the number that actually gets assigned to that hash slot will be one less than the value of $pass_count in the enclosing scope after that line. Use a preincrement or a refererence there.

As for the object attempt, the problem is that you're class represents a single row in the log file, and so every time you loop over the Agency object you overwrite the values from previous iterations. That's why you see only the last entry when you display the results. Double check the counter as well because it will suffer the same flaw as I noted above (post-incremented). You either need to create a collection of objects in an array and iterate over that calling Agency::display on each later in the code, or create a new Agency and display it on every iteration inside the foreach loop.

A core idea of OO is encapsulating behavior and data behind an interface, which means that your main method ought not to be figuring out which version of Agency::init to use. Rather, pass the data to the constructor and let it do the work of figuring out what the stuff is. The constructor should then call init and hand you back a complete and initialized object. Also, it is customary to refer to the first element in @_ in a method by '$class' if it is a class method (i.e., new) and by '$this' or '$self' if an instance method (i.e., init, display).

See also: Damian Conway's ten rules for when to use OO, intro to references, References Quick Reference


"The dead do not recognize context" -- Kai, Lexx

In reply to Re: nested hashes or object oriented by djantzen
in thread nested hashes or object oriented by ctaustin

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.