CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
cfg files in perl



Sep 21, 2015, 7:38 AM

Post #1 of 6 (1759 views)
cfg files in perl Can't Post

How is a cfg file used in perl? I have reviewed some scripts and what I am finding is that there appear to be criteria that the mail .pl script is using. Does anyone have a simple example that I could review? Some of these scripts are very high level and I am unable to see what's going on.
Actually, what I am trying to do is present some criteria in the cfg file for the pl script to read, but I am not sure how the criteria need to be organized.

(This post was edited by regex2012 on Sep 21, 2015, 8:42 AM)

Veteran / Moderator

Sep 21, 2015, 10:40 AM

Post #2 of 6 (1753 views)
Re: [regex2012] cfg files in perl [In reply to] Can't Post

Hum, not sure to understand.

Do you have an existing cfg file that you need to use? Then, please show a sample of it.

Or do you need to design a cfg file for future use? Then please explain what type of information you need to retrieve from such a cfg file.

There are some more or less standard formats, such as Windows .ini, and some well established standards (CSV, JSON, XML, YAML, etc.). They might sometimes seem to be overkill, but the good thing about them is that there are ready-made tools (e.g. CPAN modules) to use them.

But for very simple configuration files, you sometimes might as well roll out your own little subset of CSV- or JSON-like format.


Sep 21, 2015, 10:47 AM

Post #3 of 6 (1751 views)
Re: [Laurent_R] cfg files in perl [In reply to] Can't Post

I am trying to have a perl script check certain files to make sure they have the correct date and listing in them. Example, a file should have the following:


I want to use a pl script to see if 10 files in a certain directory correspond to that criteria. Anyone have any beginning for that?

For example, I am able to make this file:


chdir "/tmp/test";

$CONFIG = "123*";

@files = glob("$CONFIG");

foreach $file (@files) {
print "$file \n";

it will return all the files with 123 in them. But I'd like the cfg file to store some lines that are standard for a file and then use it to check and make sure all files contain these lines.

(This post was edited by regex2012 on Sep 21, 2015, 3:16 PM)

Veteran / Moderator

Sep 23, 2015, 2:32 AM

Post #4 of 6 (1717 views)
Re: [regex2012] cfg files in perl [In reply to] Can't Post

then you should probably first read the config file and store its content un memory (e.g. in a hash), and then read all the files in your directory and see if they match your criteria in the hash built upon the config file.

For loading the config file, perhaps something like this:

use strict;
use warnings;

my %config_hash;
open my $CONFIG, "<", "config.txt" or die "Unable to open config file $!";
while (my $line = <$CONFIG>) {
chomp $line;
my ($key, $value) = split /=/, $line;
$config_hash{$key} = $value;
close $CONFIG;
# ...

Now, your config data is in the %config_hash hash. You can now read each file in your directory and check is they match the config data. But I can't help much further at this point because you did not say how these config data elements are supposed to be verified in the files to be checked. If you need further help, please provide a sample of one of those files with some indication on how they relate to the configuration, unless it is obvious from the sample.

(This post was edited by Laurent_R on Sep 23, 2015, 2:32 AM)


Sep 23, 2015, 4:40 AM

Post #5 of 6 (1712 views)
Re: [Laurent_R] cfg files in perl [In reply to] Can't Post

Use a module such as Config::Lite to build the hash.
Good Luck,

Veteran / Moderator

Sep 23, 2015, 7:31 AM

Post #6 of 6 (1708 views)
Re: [regex2012] cfg files in perl [In reply to] Can't Post

I agree with Bill. It's much better to use one of the well established config modules on CPAN rather than rolling you own parser.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives