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
In reply to Re: nested hashes or object oriented
by djantzen
in thread nested hashes or object oriented
by ctaustin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |