ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am a total newbie with Perl but I am trying very hard to get things done. Anyway I have this small piece of code from a script I created but feel I am doing it the "hard way" and wondered if anyone could maybe simplify it. It would certainly help me by example.
Here is the code:
Code:
if ($#ARGV == -1) {
usage();
}else{
if ($ARGV[0] !~ /\.txt$/) {
usage();
}
}
I tried with || and && in the if statements to join them (so to speak) but I was getting all manner of compilation errors. So managed to get the above working and have just left it. But now would like to see other ways of how to code this.
It is always a bit of a risk commenting on matters of style, especially with Perl, but...
Other than your indenting style, which may have been a victim of copy/paste, my main point would be on the use of '$#ARGV'. While it is technically fine, it is somewhat less conventional than the simpler '@ARGV'. When an array is used in scalar context, it evaluates to the length of the array. Since you evidently want to test whether the user gave any commandline arguments:
It is a matter of personal preference, but I prefer not to combine various tests when parsing the commandline. In my experience, the commandline parser is one place most likely to be modified over time, and keeping all of the various tests separate seems to make that part cleaner. Other will have different opinions, no doubt.
I'm a total Perl novice (so this sort of thread is fun for me), but I will say: those tests seem excessively redundant to me, especially since you're calling usage() in both cases.
If scalar $ARGV[0] does not match the pattern you specified, then testing @ARGV for undef seems completely superfluous. (i.e. The pattern test is the one that really counts. Who cares if @ARGV is undef?)
-------
edit: I was one minute too slow, but my comments are intended to be in the same vein as markush's.
Good eye. Could be corrected by using shift to assign it to a variable (while doing the test)...
I've checked this, but one gets a warning about "use of uninitialized value..." if @ARGV is empty.
Quote:
...Does usage() also die? If so, I still don't see the point of both tests. I may be missing some important context.
this was what I thought, most programs die with a "usage" message if one executes them with wrong arguments. But it is possible to let the program run and print the usage-message anyway, which the OP wants to achieve.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.