How to handle this situatiuon in Perl
I am having a configuration INI file to store all configuration required for my script to run. I have a Logger.PM which uses Log4Perl, and ConfigReader.PM which reads the INI file and stores the value in global variable. My Start.PL is the entry point where i call the methods from Logger and configreader.
What I do currently
- In Start.PL I hardcoded the INI file path
- In Logger.Pm I harcoded the directory name where log files should be stored开发者_开发知识库
What I want
- I want the INI file path as configurable
- I want the log folder path to be taken from the INI file
I could do this by following
- Pass the INI file path as a parameter to the start.pl
- Read the INI file and get the folder path from INI file
What I could face is that
- I cannot use the Logger.PM in ConfigReader (partially) since the folder name required for logger is part of INI file
I want to log every step of my script (for logging/debugging purpose in case of failure. )
I can use print
but this will write to console and to capture i need to use >>log.txt. Then i will be forced to maintain 2 logs for my application which is not what I wanted
Anyone have a good solution for this scenario?
You can pass INI file path in command line using Getopt::Long, and command line switches for istance:
Start.pl --ini=/path/to/INI_file
Here is a code sample to show what changes are needed in Start.pl, in order to have switches:
#!/usr/bin/env perl
use v5.12;
use strict;
use Getopt::Long;
# That little tiny 's' after 'ini=' is for string
GetOptions ( 'ini=s' => \my $ini_file );
say $ini_file;
After this change, you can read all options from your INI file, including log folder path ( are you already using a module to manage INI files like Config::IniFiles? ).
There is something still unclear in your question about print
: although one of my master said that print with a pair of square brackets is the best debugger in the world, why use print
when you have set up Log::Log4perl
?
When you say that Logger.PL can't be used in ConfigReader, are you referring to the log object?
精彩评论