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 Fwd: [Mimedefang] graphdefang perl module install help:: Previous message: [Mimedefang] graphdefang perl module install help; Next message: [Mimedefang] Mimedefang installed, but doesn't scan anything http://lists.roaringpenguin.com/pipermail/mimedefang/2003-September/016935.htmlHOME | HelpSpy: Nmap::Scanner CPAN (Perl) Module Help:: This set of modules provides perl class wrappers for the network mapper (nmap) Until it is officially launched you may wish to visit Help-Site Computer http://helpspy.com/c.m/programming/lang/perl/cpan/c14/Nmap/Scanner/HOME |
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). Learning Perl Objects, References, and Modules: Randal Schwartz :: Learning Perl Objects, References, and Modules: Randal Schwartz, Tom Phoenix: Amazon.co.uk: Books. recent book no help at all for the more advanced Perl http://www.amazon.co.uk/Learning-Perl-Objects-References-Modules/dp/0596004788HOME | The perl program will automatically find perl lib and module files :: Perl Library and Module help information. The perl program will automatically find perl lib and module files that have been added to the system. http://crux.baker.edu/~rnewma01/classes/Perlhelp.htmlHOME |
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
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_valuen";
}
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.
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.
Thanks for the generous tip and 5-star rating, both are very much appreciated.
Red Hat's Rough Recovery From CFO Exit
Windows Live Finds a New, Pre-installed Home
|