how to get user input and use that value in the sc

2019-09-03 05:34发布

问题:

I need to do the following:

Print "User please enter the age, sex, blah_blah" > $age>$sex>$blah_blah;


print "the current user stats are- age = $age, sex = $sex";

my $rand;

if ($blah_blah > $rand)

      {

      do something

      }

else 

      {
      something else
      }

Can someone help me with to take inputs from the user and then be able to use that value in the script?

I also want to be able to run this script as:

perl script1 -age -sex -blah_blah

By that I mean instead of asking the user to provide these values can I simply type these in the command line and can my script still use these as above?

Is this possible? If so, can I also add -help i.e. perl script1 -help and if this could print out some comments from the script?

回答1:

Try this for command line options and help ..run as..in place of my params put age,sex,address whatever you want.

perl test.pl -pass 'abcd' -cmd 'abcd' -path 'paths' -value 'testde'

#!/usr/bin/perl


use Getopt::Long;
GetOptions ("value=s" => \$value,    # string
              "cmd=s"   => \$cmd,      # string
              "pass=s"  => \$pass,# string
              "help"=> \$help ) 
or die("Error in command line arguments\n");


if ( $help ){
        print " this is a test module for help please bare with it ";
        exit;
}
 print "$cmd,$pass\n";


回答2:

The Perl module IO::Prompt::Tiny is useful for prompting with a prompt message, accepting input portably, or accepting default values if it detects a non-interactive runtime.

Combine this with Getopt::Long to accept command line inputs.

Your program might check for the definedness of the Getopt::Long options, and for each one that hasn't been supplied on the command line, invoke prompt().

Usage info and documentation are made trivial by the Perl module Pod::Usage.

Here's an example:

use strict;
use warnings;
use IO::Prompt::Tiny 'prompt';
use Pod::Usage;
use Getopt::Long;


my( $age, $sex, $blah, $help, $man );
GetOptions(
  'age=s'  => \$age,
  'sex=s'  => \$sex,
  'blah=s' => \$blah,
  'help|?' => \$help,
  'man'    => \$man,
) or pod2usage(2);

pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;


$age  //= prompt( 'Enter age: ', '0'          );
$sex  //= prompt( 'Enter sex: ', 'undeclared' );
$blah //= prompt( 'Blab now:  ', ''           );

print "Your age was entered as <$age>, sex declared as <$sex>, and you ",
      "chose to blab about <$blah>.\n";

__END__

=head1 User Input Test

sample.pl - Using Getopt::Long, Pod::Usage, and IO::Prompt::Tiny

=head1 SYNOPSIS

    sample.pl [options]

        Options:
          -help        Brief help message.
          -age n       Specify age.
          -sex s       Specify sex.
          -blah blah   Blab away.

=head1 INTERACTIVE MODE

If C<-age>, C<-sex>, and C<-blah> are not supplied on the command line, the
user will be prompted interactively for missing parameters.

=cut

Getopt::Long and Pod::Usage are core Perl modules; they ship with every Perl distribution, so you should already have them. IO::Prompt::Tiny is on CPAN, and although it has a few non-core build dependencies, its runtime dependencies are core-only, and pretty light-weight.