Perl passing parameters
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: passing parameters

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,242

    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

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,242

    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

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,871

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center