-
February 24th, 2013, 05:27 AM
#1
passing parameters
Technically, this isn't a server side question but it is a Perl question and this seems to be the only Perl forum here!
I want to specify part of a Perl script such that it will only get executed if I pass a specific command line parameter. For example, if I specify --build-all on my command line, the whole script will get processed - but if I don't pass that parameter, part of the script will get skipped. Here's the kind of thing:-
Code:
#! e:/program files/perl/bin/perl.exe
$major = 1;
$minor = 3;
$micro = 7;
sub process_file
{
my $outfilename = shift;
my $infilename = $outfilename . ".in";
open (INPUT, "< $infilename") || exit 1;
open (OUTPUT, "> $outfilename") || exit 1;
while (<INPUT>) {
s/\@GLIB_MAJOR_VERSION\@/$major/g;
s/\@GLIB_MINOR_VERSION\@/$minor/g;
s/\@GLIB_MICRO_VERSION\@/$micro/g;
print OUTPUT;
}
}
process_file ("my-project-config.win32");
if (build-all-specified)
process_file ("my-project.rc");
process_file ("my-project.some_other_file");
I want the blue section to get skipped unless I specify --build-all as a parameter when running the script. I'm sure this is probably child's play for an experienced Perl programmer but I'm very unfamiliar with Perl. How can I do it?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 24th, 2013, 07:22 AM
#2
Re: passing parameters
This is rather crude but it seems to work unless anyone's got a more elegant solution....
(changed lines are in red)
Code:
#! e:/program files/perl/bin/perl.exe
$major = 1;
$minor = 3;
$micro = 7;
sub process_file
{
my $outfilename = shift;
my $infilename = $outfilename . ".in";
open (INPUT, "< $infilename") || exit 1;
open (OUTPUT, "> $outfilename") || exit 1;
while (<INPUT>) {
s/\@GLIB_MAJOR_VERSION\@/$major/g;
s/\@GLIB_MINOR_VERSION\@/$minor/g;
s/\@GLIB_MICRO_VERSION\@/$micro/g;
print OUTPUT;
}
}
process_file ("my-project-config.win32");
my $command=join(' ',@ARGV);
if ($command eq -buildall) {
process_file ("my-project.rc");
process_file ("my-project.some_other_file");
}
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 24th, 2013, 03:20 PM
#3
Re: passing parameters
That will work.
FYI...PERL questions do go here because it is a typical server-side language. Forums aren't divided by implementation, rather the language they are written in.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|