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.

I've gone around and around with argument checking and error handling. I'm currently using Carp and Data::Dumper. croak() is good; confess() gives you even more information. Dump() is the easiest way to see the contents of variables.

HTH,

David



2010-11-02 09:22:05 dpchr...@p43400e ~
$ cat foo.pl
#! /usr/bin/perl
use strict;
use warnings;
use Carp;
use Data::Dumper;
croak "ERROR: requires exactly two arguments: ",
    Data::Dumper->Dump([...@argv], [qw(*ARGV)])
    unless @ARGV == 2;
print $ARGV[0], $ARGV[1], "\n";
2010-11-02 09:22:08 dpchr...@p43400e ~
$ perl -c foo.pl
foo.pl syntax OK
2010-11-02 09:22:13 dpchr...@p43400e ~
$ perl foo.pl
ERROR: requires exactly two arguments: @ARGV = ();
 at foo.pl line 6
2010-11-02 09:22:19 dpchr...@p43400e ~
$ perl foo.pl bar baz 3.1415927
ERROR: requires exactly two arguments: @ARGV = (
          'bar',
          'baz',
          '3.1415927'
        );
 at foo.pl line 6
2010-11-02 09:22:22 dpchr...@p43400e ~
$ perl foo.pl bar baz
barbaz
2010-11-02 09:22:25 dpchr...@p43400e ~
$ perldoc Carp
<snip>
2010-11-02 09:22:29 dpchr...@p43400e ~
$ perldoc Data::Dumper
<snip>

--
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