Hi all: I'm working on my first-ever package, and I'm running into some difficulty. The package is designed to create "menu" objects for text-based terminal applications (a-la CDRX.pl). I want to be able to create a new menu object, then assign menu entries to the object in the form of a hash.

Everything seems to be working ok, with the exception of some missing attributes from the initial new() method. The title is accepted, and the menu entries are created, but no header or comment attributes are saved. I would've expected the problem to be within the set_choices() method because of the complex structure involved (HoH), but, that seems to be working ok. Can anyone pinpoint where my code is broken?

TIA,
-fp
package Menu; use strict; use Data::Dumper; sub new { my ($self, %args) = @_; bless { _title => $args{title} || die "Sorry, we need a title for this + menu!", _header => $args{header}, _comment => $args{comment} }, $self; } sub set_choices { my ($self, %choices) = @_; my $count; foreach (keys %choices) { $self->{$_} = ( _id => $_, _name => $choices{$_}{name} || die "Sorry, we need a name +for this menu entry!", _sub_or_id => $choices{$_}{sub_or_id} || 0, _return_sub => $choices{$_}{return_sub} ); } } sub print { my $self = shift; print Dumper $self; } 1;
------------------------------------------------------------
#!/usr/bin/perl # test_menu.pl use strict; use lib qw(.); use Menu; my $menu = Menu->new( title => "Testing menu", header => "Just testing...", comment => "Ok, this is where the comment section would normal +ly go." ); my %entries = ( 1 => { name => "Entry #1" }, 2 => { name => "Entry #2" }, 3 => { name => "Entry #3" }, 4 => { name => "Quit" } ); $menu->set_choices(%entries); $menu->print;
------------------------------------------------------------
[jason@lappy jason]$ perl test_menu.pl $VAR1 = bless( { '1' => 'Entry #1', '2' => 'Entry #2', '3' => 'Entry #3', '4' => 'Quit', '_title' => 'Testing menu' }, 'Menu' );

In reply to Designing a package object by fuzzyping

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.