Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 89851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:46:53+00:00 2026-05-10T22:46:53+00:00

I have a Perl script that sets up variables near the top for directories

  • 0

I have a Perl script that sets up variables near the top for directories and files that it will use. It also requires a few variables to be set as command-line arguments. Example:

use Getopt::Long;  my ($mount_point, $sub_dir, $database_name, $database_schema); # Populate variables from the command line: GetOptions(     'mount_point=s'       => \$mount_point,     'sub_dir=s'           => \$sub_dir,     'database_name=s'     => \$database_name,     'database_schema=s'   => \$database_schema ); # ...  validation of required arguments here  ################################################################################ # Directory variables ################################################################################ my $input_directory    = '/${mount_point}/${sub_dir}/input'; my $output_directory   = '/${mount_point}/${sub_dir}/output'; my $log_directory      = '/${mount_point}/${sub_dir}/log'; my $database_directory = '/db/${database_name}'; my $database_scripts   = '${database_directory}/scripts';  ################################################################################ # File variables ################################################################################ my $input_file       = '${input_dir}/input_file.dat'; my $output_file      = '${output_dir}/output_file.dat'; # ... etc 

This works fine in my dev, test, and production environments. However, I was trying to make it easier to override certain variables (without going into the debugger) for development and testing. (For example, if I want to set my input_file = ‘/tmp/my_input_file.dat’). My thought was to use the GetOptions function to handle this, something like this:

GetOptions(     'input_directory=s'      => \$input_directory,     'output_directory=s'     => \$output_directory,     'database_directory=s'   => \$database_directory,     'log_directory=s'        => \$log_directory,     'database_scripts=s'     => \$database_scripts,     'input_file=s'           => \$input_file,     'output_file=s'          => \$output_file ); 

GetOptions can only be called once (as far as I know). The first 4 arguments in my first snippit are required, the last 7 directly above are optional. I think an ideal situation would be to setup the defaults as in my first code snippit, and then somehow override any of them that have been set if arguments were passed at the command line. I thought about storing all my options in a hash and then using that hash when setting up each variable with the default value unless an entry exists in the hash, but that seems to add a lot of additional logic. Is there a way to call GetOptions in two different places in the script?

Not sure if that makes any sense.

Thanks!

Related Questions

No related questions found

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:46:54+00:00Added an answer on May 10, 2026 at 10:46 pm

    Here’s another approach. It uses arrays of names and a hash to store the options. It makes all options truly optional, but validates the required ones unless you include ‘–debug’ on the command line. Regardless of whether you use ‘–debug’, you can override any of the others.

    You could do more explicit logic checks if that’s important to you, of course. I included ‘–debug’ as an example of how to omit the basic options like ‘mount_point’ if you’re just going to override the ‘input_file’ and ‘output_file’ variables anyway.

    The main idea here is that by keeping sets of option names in arrays, you can include logic checks against the groups with relatively little code.

    use Getopt::Long;  my @required_opts = qw(     mount_point     sub_dir     database_name     database_schema );  my @internal_opts = qw(     input_directory     output_directory     log_directory     database_directory     database_scripts     input_file     output_file );  my @opt_spec = ('debug', map { '$_:s' } @required_opts, @internal_opts);  # Populate variables from the command line: GetOptions( \(my %opts), @opt_spec );  # check required options unless  my @errors = grep { ! exists $opts{$_} } @required_options; if ( @errors && ! $opts{debug} ) {     die '$0: missing required option(s): @errors\n'; }  ################################################################################ # Directory variables ############################################################################### my $opts{input_directory}    ||= '/$opts{mount_point}/$opts{sub_dir}/input'; my $opts{output_directory}   ||= '/$opts{mount_point}/$opts{sub_dir}/output'; my $opts{log_directory}      ||= '/$opts{mount_point}/$opts{sub_dir}/log'; my $opts{database_directory} ||= '/db/$opts{database_name}'; my $opts{database_scripts}   ||= '$opts{database_directory}/scripts';  ################################################################################ # File variables ################################################################################ my $opts{input_file}    ||= '$opts{input_directory}/input_file.dat'; my $opts{output_file}   ||= '$opts{output_directory}/output_file.dat'; # ... etc 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Now, if you want to change the build configuration you… May 11, 2026 at 8:41 am
  • added an answer Entity Framework itself is provider-based, and is designed to operate… May 11, 2026 at 8:41 am
  • added an answer Yes, when you load your applet, if you choose to… May 11, 2026 at 8:41 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.