Hi Sri, a few comments on your code. On Tuesday 02 November 2010 07:08:41 Sri wrote: > Hi - I have just started with Perl and would need your help on this. > > I am trying to write a program which expects two strings(arguments) > from the end user (no less, no more) and I would like to check if the > end-user did pass only two arguments. I am using the code below for > this task. Pl advice. > > #!/usr/local/bin/perl >
Please go over http://perl-begin.org/tutorials/bad-elements/ . Your code contains many bad elements - where did you learn to write Perl this way from? Add strict and warnings here. > MAIN: { > sub chk_args($); Don't use prototypes for subroutines. And why do you need the "MAIN: { ... }" wrapper? See: https://www.socialtext.net/perl5/index.cgi?prototype > my $name1 = <>; > my $name2 = <>; > ($name1, $name2) = @ARGV; So you're assigning $name1 and $name2 from the default input filehandle ("*ARGV" - not to be confused with the "@ARGV" array.), and then over-writing them with the first two values of @ARGV. Why are you doing that? > my $num = $#ARGV + 1; This is better written as: {{{ my $num = @ARGV; }}} > > chk_args($num); > } > > sub chk_args($) { > my $n = @_; You are passing a single scalar to "chk_args()" but then base your calculations on the length of its parameter list (which is always one.). You should do: my $n = shift; As Shawn and Uri noted, your code is better written as: [code] if (@ARGV != 2) { die "Expects exactly two arguments."; } my ($name1, $name2) = @ARGV; [/code] Or maybe: [code] my $name1 = shift(@ARGV); my $name2 = shift(@ARGV); if (@ARGV) { die "Expects exactly two arguments."; } [/code] > if ($n != 2) { > > print " $n arguments - 2 required \n"; > exit 1; > > } > else { > > return 0; Why are you returning 0? What does it indicate? Either do "return 1;" if it's a boolean or "return;" to indicate you wish to return a false value (or that you shouldn't count on the return value). > } > } Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Interview with Ben Collins-Sussman - http://shlom.in/sussman <rindolf> She's a hot chick. But she smokes. <go|dfish> She can smoke as long as she's smokin'. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/