Gopal Karunakar <gk.kalipuray...@gmail.com> asked:
>      How can i declare a simple perl script file which will take
> arguments from the user? an example will be take two numbers and
> give the sum as output.

Do you want to pass the arguments on the command line or prompt for them after 
starting your script?

In the first case use the @ARGV array. Its elements are the arguments your 
program was started with, e.g.

#!/usr/bin/perl -w

use strict;

if( 2 == @ARGV ){
  my $sum = $ARGV[0] + $ARGV[1];
  print "The sum of $ARGV[0] and $ARGV[1] is $sum.\n";
} else {
  # wrong number of arguments
  print "Usage: $0 <number> <number>\nprints the the sum of two numbers passed 
as arguments\n";
}
__END__

In the second case, use the <STDIN> filehandle, e.g.

#!/usr/bin/perl -w

use strict;

print "Enter a number: ";
chomp( my $sum1 = <STDIN> );

print "Enter a second number: ";
chomp( my $sum2 = <STDIN> );

my $sum = $sum1+$sum2;
print "The sum of $sum1 and $sum2 is $sum.\n";

__END__

Input sanitation and validation in these examples is left as an exercise for 
the reader ;-)

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to