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

[MacPerl] More Perl Starmapping



I'm working on a console interface (just using STDIN at the moment), and 
have turned out the following subroutines  Sorry this isn't the complete 
program (it's getting rather big), but to the best of my knowledge, there 
are no obvious bugs herein.  What I'm looking for is a second set of 
eyes, or three, to tell me what I might do better.  Below are three 
routines:  the first loops until(defined($done)), which happens when the 
user types a "0".  The second is called by the first repeatedly as long 
as $done is undefined. The third supplies the initial conditions, so that 
the lazy user can just type "0", and be assured of reasonable output.  Of 
course my code calls init_settings(); before calling usr_console();

At the moment the only user-supplied constraint that actually DOES 
anything is "$settings{output_filename}", which is passed along to 
"$out_flie". Because this is cross-platform code, $settings{interactive} 
is supplied on the off chance that I might want to implement command-line 
switches in Unix. ;-) Well, without further ado:

sub usr_console {
my ($input, $done);

until (defined($done)) {

        usage();

        $input = <STDIN>;
        chomp($input);
        if ($input =~ /^1$/)  {
            print "Enter reference point X coordinate: ";
            $settings{ref_x} = <STDIN>;
            chomp($settings{ref_x});
                if ($settings{ref_x} =~ /[A-Za-z]+|\s+/) {
                print "X coordinate must be numeric.\n";
                $settings{ref_x} = 0;
                }
            print "Enter reference point Y coordinate: ";
            $settings{ref_y} = <STDIN>;
            chomp($settings{ref_y});
                if ($settings{ref_y} =~ /[A-Za-z]+|\s+/) {
                print "Y coordinate must be numeric.\n";
                $settings{ref_y} = 0;
                }
            print "Enter reference point Z coordinate: ";
            $settings{ref_z} = <STDIN>;
            chomp($settings{ref_z});
            if ($settings{ref_z} =~ /[A-Za-z]+|\s+/) {
                print "Z coordinate must be numeric.\n";
                $settings{ref_z} = 0;
            }
        }
        elsif ($input =~ /^2$/) {
            print "Minimum distance: ";
            $settings{mindist} = <STDIN>;
            chomp($settings{mindist});
            if ($settings{mindist} =~ /[A-Za-z]+|\s+/) {
                print "Distance needs to be numeric.\n";
                $settings{mindist} = 0;
            }
        }
        elsif ($input =~ /^3$/) {
            print "Maximum distance: ";
            $settings{maxdist} = <STDIN>;
            chomp($settings{maxdist});
            if ($settings{maxdist} =~ /[A-Za-z]+|\s+/) {
                print "Distance must be numeric.\n";
                $settings{maxdist} = 5;
            }
        }
        elsif ($input =~ /^4$/) {
            print "Minimum Luminosity: ";
            $settings{minlum} = <STDIN>;
            chomp($settings{minlum});
            if ($settings{minlum} =~ /[A-Za-z]+|\s+/) {
                print "Luminosity must be numeric.\n";
                $settings{minlum} = 0;
            }
        }
        elsif ($input =~ /^5$/) {
            print "Maximum Luminosity: ";
            $settings{maxlum} = <STDIN>;
            chomp($settings{maxlum});
            if ($settings{maxlum} =~ /[A-Za-z]+|\s+/) {
                print "Luminosity must be numeric.\n";
                $settings{maxlum} = 200.0;
            }
        }
        elsif ($input =~ /^6$/) {
            if ($settings{allow_binaries} eq "YES") {
                $settings{allow_binaries} = "NO";
            } else { $settings{allow_binaries} = "YES" }
        }
        elsif ($input =~ /^7$/) {
            if ($settings{just_habitables} eq "NO") {
                $settings{just_habitables} = "YES";
            } else { $settings{just_habitables} = "NO" }
        }
        elsif ($input =~ /^8$/) {
            if ($settings{output_style} eq "VERBOSE") {
                $settings{output_style} = "TERSE";
            } else { $settings{output_style} = "VERBOSE" }
        }
        elsif ($input =~ /^9$/) {
            print "Enter new filename: ";
            $settings{output_filename} = <STDIN>;
            chomp($settings{output_filename});
        }
        elsif ($input =~ /^0$/) {
            $done = "1";
        }
        else
        {
        print "I haven't the foggiest notion what you mean by $input\n";
        }
    }
}

sub usage {
    print "Enter an item number as indicated below: \n\n";
    printf STDOUT (<<'    USAGE'
    Current output settings are
    ---------------------------
    1). Reference Point      : %2.3f  %2.3f  %2.3f
    2). Minimum Distance     : %2.3f
    3). Maximum Distance     : %2.3f
    4). Minimum Luminosity   : %3.5f
    5). Maximum Luminosity   : %3.1f
    6). Binaries Allowed?    : %s
    7). Only Habitable Stars?: %s
    8). Output Style         : %s
    9). Output File Name     : %s
    ----------------------------
    Enter a number to modify the output settings,
    or type "0" to run.
    USAGE
    ,
    $settings{ref_x}, $settings{ref_y}, $settings{ref_z},
    $settings{mindist},
    $settings{maxdist},
    $settings{minlum},
    $settings{maxlum},
    $settings{allow_binaries},
    $settings{just_habitables},
    $settings{output_style},
    $settings{output_filename}
    );
}

sub init_settings {
    %settings = (
    'ref_x'           => 0.0,
    'ref_y'           => 0.0,
    'ref_z'           => 0.0,
    'mindist'         => 0.0,
    'maxdist'         => 5.0,
    'minlum'          => 0.0,
    'maxlum'          => 200.0,
    'allow_binaries'   => 'YES',
    'output_style'     => 'VERBOSE',
    'output_filename'  => "gliese3.log",
    'interactive'     => 'ON',
    'just_habitables' => 'NO'
    );
}

__END__

Now that I think of it, in the routines where it's possible for the user 
to make entry errors (those that aren't simple switches), when an error 
occurs, I should perhaps exit(); after reseting the variable to it's 
default value.

--B


# Fungal Parataxonomy                   Mycology Information (Mycoinfo)
# Webmaster, Staff Writer      **The World's First Mycology E-Journal**   
# <mailto:webmaster@mycoinfo.com>            <http://www.mycoinfo.com/> 
#
# First they ignore you. Then they laugh at you. Then they fight you.
# Then you win.                                     --Mohandas Gandhi


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org