in reply to Re: Problem with Tie::RegexpHash
in thread Problem with Tie::RegexpHash
decided to add this as a seperate node
This is the starting point I've got so far:
#!/usr/bin/perl -w use strict; use diagnostics; my %cmds; my $rgx; my $sub; $cmds{qr/^\s*quit/} = \&quit; $cmds{qr/^\s*cmd .*/} = \&cmd; my $txt = "cmd 12 arg2"; foreach $rgx (keys(%cmds)) { if ( $txt =~ /$rgx/i ) { $sub = $cmds{$rgx}; &$sub($txt); } } exit; sub quit { print "called quit\n"; } sub cmd { my ($cmd, @argx) = split /\s+/, $_[0]; print "called ".$cmd." with ".$argx[0]." ".$argx[1]."\n"; }
which when called as above returns:
called cmd with 12 arg2
and if the line:
is changed tomy $txt = "cmd 12 arg2";
returns:my $txt = "quit";
called quit
|
---|
In Section
Seekers of Perl Wisdom