Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2003
    Posts
    1

    Unanswered: How to include configuration file in Perl script

    Hi,

    In my perl script ,I need to include a configuration file to get the user information.
    In shell script we include the config file as . /config.cfg ( suppose the configuration file is say config.cfg)

    I want to know the equivalent of ". /config.cfg " in perl script

    Thanks,

  2. #2
    Join Date
    Oct 2003
    Posts
    18
    I use require for stuff like this:

    require "config.pl";

    Of course the config.pl needs to be valid perl, like:

    $username="test";
    $password="testtest";
    $default_dir="/tmp";

    it also needs to end with a true statement, otherwise the require will fail. Put a "1;" on the last line to get this.

    hth
    /Hacker

  3. #3
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89

    cfg --- perhaps an .ini?

    If a Windows 3.0 style .ini file meets your requirements, you can use something like
    Code:
    #!\bin\perl -w
    
    #Usage:
    #$0  [switches & values]
    #
    #Switches:
    # -h		display this help message
    # -e argument   example argument
    
    use Config::IniFiles;
    use Getopt::Std;
    #use ...;
    
      # User specified options
      my(%option);
         {
        getopts('he:',\%option) || PrintUsageAndQuit(1);
        PrintUsageAndQuit(0) if $option{'h'}; # Doesn't need arguments
         };
    
      # Ini file
      my($o_cfg);
         { # Default to $0
        my($s_0)=split(/\./,$0,2);
        if (-e "$s_0.ini") { # Ini file exists
          $o_cfg=Config::IniFiles->new(-file=>"$s_0.ini");
           } else { # Ini file doesn't exists
          $o_cfg=Config::IniFiles->new();
          $o_cfg->SetFileName("$s_0.ini");
           };
        # Interpret the ini file
         };
    
    # Set configuration parameters
    
      # A specified parameter value overrides and sets the .ini value
         {
        if (defined $option{e}) { # example arguement
          $o_cfg->setval('EXAMPLE','ARGUEMENT',$option{e}) || $o_cfg->newval('EXAMPLE','ARGUEMENT',$option{e});
           };
         };
    
      # Write the .ini file
      $o_cfg->RewriteConfig;
    
    #
    # Your code ...
    #
    
      sub PrintUsageAndQuit {
        # Emit usage message, then exit with given error code.
        print <<"END_OF_HELP";    exit($_[0] || 0);
    
    Usage:
    $0  [switches & values]
    
    Switches:
     -h		display this help message
     -e argument    example argument
    
    END_OF_HELP
         };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •