I believe this is your problem:

$command_action= $cl[0]{$cl[1]};

In strict, you can't use $cl[0] (contains 'log') as a hash ref (pointing to the %log hash).

As was mentioned before, you should consider using subroutine references instead of eval'ing. As for the problem with two-word commands, I'd suggest this:


%command_list = (
       quit => \&end_program,
       greeting => \&greeting,
       time => \&systime,
       help => \&help,                        
       log => \&which_log   # this one is new
);

sub which_log {
  my $arg = shift;
  return start_logs() if ($arg eq "start");
  return stop_logs() if ($arg eq "stop");
  return log_status();  # default behavior
}

$command = <STDIN>; # assume $command="log start"
@cl = split / /,$command;
my $func = shift @cl;   # first CLI argument is hash key
if (defined($command_list{$func})) {
  $command_list{$func}->(@cl);   # passes rest of CLI args to handler
} else {
  print "invalid command\n";
}


This is untested code, but hopefully it does what I think. Whatever the command line entered, it'll take the first arg as the hashkey in %command_list and pass any extra arguments. So just make a simple wrapper for all your *log() subroutines that uses the argument to determine which to call. Hopefully this makes sense ;)

In reply to Re: Trying to simulate a CLI using hashes... by blokhead
in thread Trying to simulate a CLI using hashes... by thoth

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.