#!/c/opt/perl/bin/perl BEGIN {(*STDERR = *STDOUT) || die;} use warnings; use strict; use diagnostics; use Carp; $| = 1; use Data::Dumper; # Use the generic module ip_parser.pm use ip_parser; # version number of this application specific file my $version = 0.1; # # Application specific routine that returns reference to array # of default argv. The hash ip_val is the counterpart of the # application's data structure ip_str; Convert::Binary::C # forms the bridge between this hash and the ip_str. # sub make_default_args { my $ip_val = shift; my ($d, $e, $f) = (); foreach my $foo ( @{${$ip_val}{d}} ) { ( $foo and $d .= chr( $foo ) ) or last ; } foreach my $foo ( @{${$ip_val}{e}} ) { ( $foo and $e .= chr( $foo ) ) or last ; } foreach my $foo ( @{${$ip_val}{f}} ) { ( $foo and $f .= chr( $foo ) ) or last ; } # no need to escape quotes # $d =~ s/"/\\"/g; # $e =~ s/"/\\"/g; return [ "a = ${$ip_val}{a}", "b = ${$ip_val}{b}", # "c = ${$ip_val}{c}", no default value "d = $d ", # no need to use quotes "e = $e ", "f = $f ", "g = ${$ip_val}{g}", ]; } # # Application specific specification of arguments Getopt::Declare # requires tabs in the specification; We write the specification # using _tb_ instead of tabs, then, before use, we globally # substitue _tb_ by \t # my $app_args_format =< _tb_A counter b = _tb_The downward floater c = _tb_A teenager d = _tb_The long message e = _tb_A shorter message f = _tb_The identifier g = _tb_The floater going up SPEC_FOR_THE_APPS_ARGS # Application specific routine that takes the parsed information # from ip_parser.pm and puts it into the counterpart of the # application's structure ip_str; ip_parser.pm will pass this hash # to the application using Convert::Binary::C # sub fill_args { my $ip_val = shift; my $app_args = shift; my $d = ${$app_args}->{ 'd' }{ '' }; my $e = ${$app_args}->{ 'e' }{ '' }; my $f = ${$app_args}->{ 'f' }{ '' }; my @ad = map ord, split '', $d; my @ae = map ord, split '', $e; my @af = map ord, split '', $f; ${$ip_val}{a} = ${$app_args}->{ 'a' }{ '' }; ${$ip_val}{b} = ${$app_args}->{ 'b' }{ '' }; ${$ip_val}{c} = ${$app_args}->{ 'c' }{ '' }; ${$ip_val}{d} = \@ad; ${$ip_val}{e} = \@ae; ${$ip_val}{f} = \@af; ${$ip_val}{g} = ${$app_args}->{ 'g' }{ '' }; } my $h = ip_parser::parser_init( $version, $app_args_format ); $h or exit( -1 ); # The default arguments are made using the default values # written out by the application before calling this perl # script ${$h}{ default_args } = make_default_args( ${$h}{ ip_val } ); # The arguments are processing in this order: default # values, values in any file, values specified in the # command line. ip_parser::get_arguments() or exit( 0 ); # Load the parsed arguments into the hash that will # soon be transfered to the calling applicaiton fill_args( ${$h}{ ip_val }, ${$h}{ app_args } ); # Transfer the hash to the calling application ip_parser::write_io_file( ); # Successful exit; I could not make successful parsing # return 0 since Getopt::Declare returns 0 on encountering # the user's request for usage information! So I made # successful parsing for non-trivial (meaning not requests # for help or version) parsing 1 exit( 1 ); __END__