Besides what almut says (emphasis on "use strict;"), your code has a couple of problems. The main one is that you need use square brackets to initialize an array reference. You can't have arrays inside a hash (or hash ref), only references to arrays. So, in function module::_define, you should have:
my $object = { name => $name , port_list => [] , verilog_list => [] , signal_list => [] , instance_list => [] };
Also, the "Main program" part of your program is not actually in package 'main' as it should be. It's actually running as part of the initialization of the last package declared, in this case 'module'. That can cause problems. Unless it's declared inside a { } block, a package declaration is in effect in your source code until the next package declaration. You could define package signal like this:
{ package Signal; sub _define{ my $type = shift ; my $name = shift ; my $width = shift ; if(($type ne "reg") && ($type ne "wire")) { printf("\n***** ERROR: Wrong Signal type %s\n",$type); exit(1); } my $object = { type => $type , name => $name , width => $width }; bless($object,__PACKAGE__); return $object; } sub print_defn { my $self = shift; printf("\t%s",$self->{type}); if(($self->{width})!=1) { printf("\t[%d:0]",($self->{width})-1) } else { print(" "); } printf("\t\t\t%s;\n",$self->{name}); } }
Note the use of the enclosing curly braces. Also, I used __PACKAGE__ in the bless call. I could have also just written bless $object;. If you plan to ever use class inheritance in your program, though, it's better to use the more conventional Perl style which explicitly passes the class as the first argument. You would call Signal->_define($type,$name,$width); instead of Signal::_define($type,$name,$width); and then _define would have another parameter:
sub _define{ my $class = shift ; my $type = shift ; my $name = shift ; my $width = shift ; if(($type ne "reg") && ($type ne "wire")) { printf("\n***** ERROR: Wrong Signal type %s\n",$type); exit(1); } my $object = { type => $type , name => $name , width => $width }; bless($object, $class); return $object; }

In reply to Re: Problem in accessing array of objects. by ReturnOfThelonious
in thread Problem in accessing array of objects. by sumedhnarayan

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.