[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Omaha.pm] bad perl - need help - dispatch table



Terry,

As Mike & Jay said in their posts, the get options packages 
are great.  Although this is a bit beyond what you're asking for, 
I have a standard routine to allow the short options (single
character options) and object files to be intermingled:

	###############################################
	#allow options to be intermixed with filenames
	###############################################
	   while(@ARGV) {
	      #if it doesn't start with a '-', assume it's not an cmd line option
	      while($ARGV[0] =~ /^[^\-]/) { 
	         push @hold, shift @ARGV;
	      }  
	      getopts('<place-expected-option-characters-here>');
	   }     
	   @ARGV=@hold; 

And I've got a largish program that calls a routine whose name
we have in a variable, I have this little routine to help do some 
error checking:

	#######################################################################
	# Call the appropriate handling routine base on the box type given.
	#
	# Ok, I was (and still am) lazy and used dashes in "box-type" strings,
	# which are (or should be) specified within the ip-2-name.tbl file.
	# These files were all setup before I thought "Why don't I just name
	# the subroutines the same thing, and call those routines indirectly?"
	#
	# Dashes are not legal characters for any variable/subroutine name
	# because the parser would get confused because it thinks dashes are 
	# instructions to subtract. 
	#
	# I also need to check for the case where we don't know what kind
	# of box a particular message is from. And the case where the routine 
	# isn't defined. In these cases we call a default routine.
	#  
	#######################################################################
	sub call_sub {
	   my $sub = @_[0];
	      
	   # translate upper case to lower, and more importantly, dashes to
	   # underscores
	   $sub =~ tr/A-Z\-/a-z_/;
	      
	   if ($sub eq "") {
	      &unknown_box;
	   } else {
	      if( defined &$sub ) {
	         &$sub;
	      } else {
	         print "no <$sub> subroutine defined - using unknown_box routine\n";
	         &unknown_box;
	      }
	   }
	}

	#greatly simplified program starts here
	while(<>) {
		$type = &get_box_type($_);
		&call_sub( $type );  #call the appropriate box-type handling subroutine
	}

-Scott