LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2011, 12:16 PM   #1
fusion1275
Member
 
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 310

Rep: Reputation: 36
Perl - Doing things the hard way?


Hi all,

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.

Your help is greatly appreciated.

Rgds
 
Old 07-03-2011, 12:31 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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:
Code:
    if( ! @ARGV ){
        usage();
    }
    elsif( $ARGV[0] !~ m/\.txt$/ ){
        usage();
    }
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.

--- rod.
 
Old 07-03-2011, 12:34 PM   #3
fusion1275
Member
 
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 310

Original Poster
Rep: Reputation: 36
Rod,

Thank you very much for your help and comments. It has helped me a lot.

It's nice to know that I wasn't too far away from it being correct. Progress at last

Again thanks.
 
Old 07-03-2011, 12:39 PM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello,

I would use
Code:
if (shift !~ m/\.txt$/) {
       usage();
}
which works but with a warning

The reason is that if no arguments are there, they cannot match *.txt and therefore I do the test in one step.

Markus
 
Old 07-03-2011, 12:40 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.

Last edited by anomie; 07-03-2011 at 12:41 PM.
 
Old 07-03-2011, 12:43 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Code:
if (shift !~ m/\.txt$/) {
       usage();
}
Yabbut...
If there was an argument passed on the commandline, and you want to make use of it, you've just blown it away.
--- rod.

Last edited by theNbomr; 07-03-2011 at 12:45 PM.
 
1 members found this post helpful.
Old 07-03-2011, 12:56 PM   #7
fusion1275
Member
 
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 310

Original Poster
Rep: Reputation: 36
Hi all, thanks for your comments.

Unfortunately I do need both tests in place for the rest of the code but do see what you meant with your example markush.

Cheers.
 
Old 07-03-2011, 12:59 PM   #8
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Well, the Perl people say TMTOWTDI (there's more than one way to do it)....

I did not understand that you need the arguments even if they don't match *.txt

Markus
 
Old 07-03-2011, 02:40 PM   #9
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr
Yabbut...
If there was an argument passed on the commandline, and you want to make use of it, you've just blown it away.
--- rod.
Good eye. Could be corrected by using shift to assign it to a variable (while doing the test).

Does usage() also die? If so, I still don't see the point of both tests. I may be missing some important context.
 
Old 07-03-2011, 02:49 PM   #10
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by anomie View Post
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.

Markus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy /bin/perl to /opt/bin/perl. What all things to consider while copying? rohit.dhaval1 Linux - Software 4 02-20-2011 04:06 PM
I uninstall PERL, and now the startx many other things is gone eunicewol Red Hat 2 11-21-2010 11:46 AM
Suse: confused on many things, why is installing things so hard? blackflare Linux - Newbie 11 10-16-2007 04:35 AM
What is the procedure for adding hard drives and moving things around? kuplo Linux - General 2 08-09-2006 03:31 AM
Partitioning on Non-Hard Drive Things timrs Linux - Software 4 03-21-2006 06:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration