Google Answers Logo
View Question
 
Q: Perl help with Modules ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Perl help with Modules
Category: Computers > Programming
Asked by: mickr-ga
List Price: $20.00
Posted: 23 Jul 2004 05:06 PDT
Expires: 22 Aug 2004 05:06 PDT
Question ID: 378055
Hi,

I am after some help with using PERL modules. 

I am trying to read options using the Getopt:Long module as follows;

#!/usr/bin/perl 

# import module
use Getopt::Long; 

# set default value for option
$debug = 0; 

# get value of debug flag
$result = GetOptions ("debug" => $debug);   

# print value
print "Debug flag is $debug";

However when I call this programme 
./script.pl
gives
Debug flag is 0
./script.pl --debug
gives
Debug flag is 0

The module seems to exist. If I try use Getopt::Long1; instead I get a
message that Getopt/Long1.pm doesn't exist and gives me the search
path as well. I can see Getopt/Long.pm module is in the search path
and it has the GetOptions subprogramme.

If I do 
perl ./script.pl -w --debug 
I get 
Name "main::result" used only once:possible typo at script.pl line 7.
Where $result = GetOptions ... is line 7.

What am I doing wrong?

Thanks,

Mick
Answer  
Subject: Re: Perl help with Modules
Answered By: palitoy-ga on 23 Jul 2004 06:08 PDT
Rated:5 out of 5 stars
 
Hi Mick

This had me stumped for a little while too until I noticed you had
made a minor typo by missing off a \.

The line: $result = GetOptions ("debug" => $debug);

should read:

$result = GetOptions ("debug" => \$debug);

Let me know if this solves your problem.

A couple of useful pages on this module are here:
http://perl.active-venture.com/lib/Getopt/Long.html
http://www.devshed.com/c/a/Perl/Processing-Command-Line-Options-with-PERL/0/

Note - on this second article the \'s are also all missing (I guess
this is due to the way the HTML is presented).

Request for Answer Clarification by mickr-ga on 23 Jul 2004 06:23 PDT
Hi Palitoy,

That worked as usual.

Another question $30 tip, if you choose to answer.

I want to pass filenames to a perl script, number of input files
unknown, output file one. Can you give me some examples, maybe the
following:

a) using ARGV where the command line might look like 
  script.pl infile1 infile2 ... infile5 outfile

b) using the getopt::long module where the command line might look like 
  script.pl -in=infile1 -in=infile2 ... -in=infile5 -out=outfile

c) any other better way of doing this.

Thanks,

Mick

Clarification of Answer by palitoy-ga on 23 Jul 2004 06:52 PDT
Hello Mick

Is this the kind of thing you require:

# BEGIN #

#!/usr/bin/perl
# import module
use Getopt::Long;

# read options
$result = GetOptions ("i=s" => \@list, "o=s" => \$output);

# save the file using the $output name
open(FILE, ">$output.txt");
foreach $input_value (@list) {
  print FILE "$input_value\n";
}
close (FILE);

# END #

Syntax:
scriptname.pl --i=hello --i=goodbye --o=outputfilename

This would then produce a file called outputfilename.txt with "hello"
on line 1 and "goodbye" on line 2.

Let me know if this is correct and whether you need any more examples.

Clarification of Answer by palitoy-ga on 23 Jul 2004 06:58 PDT
I forgot to add the ARGV version:

# BEGIN #

#!/usr/bin/perl

# process the command line and save it to a file with a filename of the
# last argument on the command line
open(FILE, ">$ARGV[$#ARGV].txt");
foreach $argnum (0 .. $#ARGV-1) {
  print FILE "$ARGV[$argnum]\n";
}
close (FILE);

# END #

Syntax:
scriptname.pl hello goodbye outputfilename

This would then produce a file called outputfilename.txt with "hello"
on line 1 and "goodbye" on line 2.

Clarification of Answer by palitoy-ga on 23 Jul 2004 06:59 PDT
Thanks for the generous tip and 5-star rating, both are very much appreciated.
mickr-ga rated this answer:5 out of 5 stars and gave an additional tip of: $30.00
Thanks, I am a happy customer as usuall.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy