Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Cross-platform config file parsing

by ybiC (Prior)
on Oct 30, 2000 at 12:08 UTC ( [id://39053]=perlquestion: print w/replies, xml ) Need Help??

ybiC has asked for the wisdom of the Perl Monks concerning the following question:

Work continues on script at (code)) Cisco Pass Mass - IOS (deprecated by node 123464) - next on list is moving many config parameters to external config file.   Lots o' good info at PM on such doings.   AgentM and arturo both ++ for good info in ChatBox.

Looks to me like a roll-yer-own parser can best fit the requirements, with Config::IniFiles a possible second choice.   So I ask for review of my findings, and perhaps example(s) of a roll-yer-own parser to help me get started on the right foot.
    cheers,
    Don
    striving for Perl Adept
    (also tired, bleary-eyed, and quite possibly subject to incoherent rambling)

Requirements:

  1. Config file format can't include markup or Perl code.   (must be editable by "code illiterate" support staff)
  2. Parser must run on Linux and WinNT4, possibly AIX and Sun also.
  3. Parsing can be done using CPAN modules.

Relevant PM nodes from Super Search:

Potential modules/methods to parse config file:

  • Roll-yer-own
    • good - platform independant if done right, flexible
    • bad - more work for ybiC(?)
  • Config::IniFiles
    • good - runs on Win32-x86(?)
    • bad - only a few months old (still bugs?)
  • AppConfig
    • good - been around almost exactly 2 years (more stable/secure?)
    • bad - tested FAIL Win32-x86
  • Data::Dumper, do
    • good - part of Perl standard dist
    • bad - config file is Perl code (?)
  • Storable
    • good - ?
    • bad - config file is Perl code (?)
  • FreezeThaw
    • good - ?
    • bad - config file is Perl code (?)

Config file would be something like this:
(param=value's read into scalars)
(others read into arrays)

[parm commands] snmp-server community RO=notpublic timeout=10 [noparm commands] logging buffer errors logging console errors logging trap errors [no snmp-server community] public private secret bogus [targets] routerA routerB.sub1.dom switchA.sub2.dom switchB.sub2.dom 10.1.2.3 172.16.1.1 172.31.254.254 192.168.105.55

Replies are listed 'Best First'.
Re: Cross-platform config file parsing
by ChOas (Curate) on Oct 30, 2000 at 14:02 UTC
    Hi there!!!, this is how I usually do it, hmmm
    but without the error checking here ;))
    sub ReadConfig($) { my $ConfigFile=shift; my %Config; my $Item; open CONFIG,"<$ConfigFile" or return %Config; while(<CONFIG>) { chomp; next if ((/^#/)||(/^\s*$/)); # Yeah, I'm sorry for the .* here... if (/^\[(.*)\]$/) {$Item=$1;next;}; if($Item) { my ($SubOption,$Value)=split /\=/; if ($Value) { push @{$Config{$Item}{$SubOption}},$Value; } else push @{$Config{$Item}},$_; }; }; close CONFIG; return(%Config); };

    Anyways.. this is close to how I do it, this code not tested 'n stuff

    GrtZ!
  • ChOas


  • Upd. Oh yeah, I usually read the config as quick as possible,
    and do the error/sanity checking in another sub, dunno why,
    but it gives me kinda clean code, I like that...

    Even though I'm Lazy ;))

    Upd2. Just to clear things up, this will ofcourse leave you with a hash something like this:
    $Config{'parm commands'} |-{'snmp-server community RO'} eq 'notpublic'; |-{'timeout'}==10; @{$Config{'no snmp-server community'}}=>(public,private,secret,bogus);

    Hope it helps....
      Thanks ChOas.   That's just what I had in mind.
          cheers,
          Don
          striving for Perl Adept
Re: Cross-platform config file parsing
by cianoz (Friar) on Oct 30, 2000 at 17:26 UTC
    What about XML::Simple ?
Re: Cross-platform config file parsing
by Fastolfe (Vicar) on Oct 30, 2000 at 19:45 UTC
    Don't underestimate XML! It's got parsers already written out the wazoo, it's simple enough that a user of moderate abilities can figure it out and edit it (if Joe Blow can learn HTML, surely he can learn this), and if he can't, there are XML editors out the wazoo as well to help him on his way. A structure as returned from XML::Simple can be just as efficient and look exactly the same as you'd expect a complex data structure as returned from a proprietary config file format.
      Fastolfe - thanks for reminding me of XML::Simple.   Had forgot all about it as an option.   ++ to you and to cianoz for suggesting it.

      But... I just can't require my coworkers to learn even simple XML to use the script.   Some are gurus on EIGRP, ATM, etc. but only markup I've seen from any is with Frontpage or MS-Word "save as HTML".   First time someone wrote bad config markup, my code would get blamed 8^(

      If I were submitting the code to Freshmeat it'd be a different story.
          cheers,
          Don
          striving for Perl Adept

      /me wanders over to Editor Requests to see if Ed will let me add XML::Simple to list in original post...

        If an 8-year-old can learn HTML, with dozens of different tags and formatting things, surely an adult can learn an XML schema with as many tags as you have configuration options.. *shrug*. I would present them with a sample and see how easily they learn it. Your XML parser should inform them of errors in the XML file as well as early in the process as you can (perhaps a separate process from your application that checks the syntax of the file, which they can run after each change they make).

        In addition, there are lots of simple XML editors on the market now too, complete with nice GUI front-ends, and IE 5 does a pretty good job of displaying it.

        I personally don't think learning XML is going to be your biggest hurdle, but obviously you know the users better than I do, so it's your call... Good luck though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://39053]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found