blackice69 has asked for the wisdom of the Perl Monks concerning the following question:

hello wise monks.. my problem today is a simple one at that but I am unfortunately just beginning to learn the way of perl. My problem is that I have been asked to write a script that checks for command line switches such as "script1.pl -l logfile.txt -u useraccounts.txt -r report.txt" I don't care what order they are in but as long as it checks logfile and useraccounts exist and reports an error to the screen like "usage: wrong switch used! -c doesnt exist" and stuff like "logfile not found". How would I accomplish this? oh wise perl monks please hear my call! also.. I dont want to use geoopt::long module! just using good ol strict and warnings!! ya no this isn't a home work assignment. This is a task from work which has been given to me but unfortunately they will not install any modules, none what soever and will not give me access to anything except to write this script.. ultimatly what it does is parses a logfile and useraccounts file and searches for certain strings such as times and usernames. I have never done perl before but yet I am being told to do this! anything would be of help! anything..

Replies are listed 'Best First'.
Re: command line switches (array)
by Corion (Patriarch) on Nov 01, 2010 at 13:31 UTC

    Have you looked at the Getopt::Long module? It does what you want (and more, but...).

      yeah but unfortunately they say you can do it without it using just an array and etc.

        I guess you will have to program the business logic of what parameters are required in which combinations yourself.

Re: command line switches (array)
by mjscott2702 (Pilgrim) on Nov 01, 2010 at 17:01 UTC
    If you really can't use any modules, even a core one that ships with Perl itself (and you should ask why not, if that is the case), then simply scan over the @ARGV array and take appropriate actions, something like (untested):

    for (my $i=; $i < @ARGV; $i++)) { if ($ARGV[$i] eq "-l") { warn "Logfile $ARGV[$i+1] missing\n" unless -e $ARGV[$i+1]; } elsif ($ARGV[$i] eq "-u") { warn "Useraccounts $ARGV[$i+1] missing\n" unless -e $ARGV[$i+1]; } else { warn "Invalid switch $ARGV[$i]\n"; } $i++; }

      The above will result in weird behaviour if you have a user named -l or a logfile named -u :-)))

        yeah the logfile name and the useraccounts name will always be the same. Infact it should test for the existance of them.
Re: command line switches (array)
by raybies (Chaplain) on Nov 01, 2010 at 16:11 UTC

    As has been suggested, Getopt::Long is the module you want. You can find dozens of examples online on how to use this option. Google it, if you can't do the CPAI^HN thing. Create a seperate usage subroutine and redirect all bad options to that, and once you make your initial call to getopt, if you find that the argument array still has things in it, you can flag an error on that too.

    Rather than trying to solve your whole problem, I suggest you write a little test program just for getopt, and play with that. It comes in handy if you need to write any additional scripts, as getopt is generally the first thing EVERY perl script does. That's generally good advice is to test out a new feature in a tiny program before you apply it to whatever big program you're attempting to use.

    Good luck, --Ray