The situation to use the one liner (Re: APM: One liner to get hash from an environment list)

Wayne Walker wwalker at bybent.com
Thu Jan 22 23:32:46 CST 2004


Thanks Bill, I hadn't thought of the "execute the interpreter and parse
the output of env"  That's sweet.  My solution looks ugly now...

On Thu, Jan 22, 2004 at 07:16:28PM -0800, Bill Raty wrote:
> Fellow Mongers (and Sam),
> 
> This note isn't written to say the one-liner is superior to the
> other fine means from CPAN, but rather to describe the one
> situation which best exhibits its strength.  Read on if you're
> interested.
> 
> Yes you can combine the regexes (or transform them, or use
> grep) to filter out comments if such is your which.  How is
> left as an excercise of the class.  Hint: Schwarzian transform.
> 
> However this brings us full circle to why the one liner was
> written in the first place.  The original code that the one
> liner replaced was written to parse a shell script provided by
> a third party that set-up a database environment that had to be
> available from processes executed from Perl.  When the DB
> engine was updated by the third party, the updated shell script
> broke the rather simple parsing rules of the perl code used,
> although the shell script remained perfectly legal.  
> 
> The one liner replaced about of score of lines of logic and
> relied upon the fact that a shell best knows how to interpret
> shell commands, and made the Perl script immune from changes to
> the third-party maintained script.
> 
> The original one liner:
> 
> %ENV = map /(^.*?)=(.*)/, qx( /some/script; env );
> 
> Will work for any version of /some/script that is valid for its
> shell interpreter, without reduplication of shell logic in
> Perl.  This holds with standard Unix philosophy: stand on the
> shoulders of giants.
> 
> The same holds for the CPAN modules (of which there are dozens
> of configuration schemes from Winduhs style .ini files to XML).
>  If you're trying to leverage a shell script provided by a
> third party (DB vendor) that is written for a particular shell,
> its probably best to have the referenced shell interpret said
> script.
> 
> My $0.02,
> 
> -Bill
> 
> --- Sam Foster <austin.pm at sam-i-am.com> wrote:
> > > %config = map { /(.*?)=['"]?(.*)['"]?/ } qx(cat
> > /etc/defaults/some_prog_env.sh);
> > > 
> > > This should parse any valid bourne shell syntax key=value
> > config file
> > > into a hash.
> > 
> > but if you want to allow #comments, blank lines etc, you
> > might end up 
> > with something like this?
> > 
> > my %config;
> > foreach ( qx(cat /your/input/file) ) {
> > 	next if /^\s*#/;	# skip commented lines
> > 
> > 	# match for name=value pairs, allowing for indentation and
> > 	# options whitespace around the '='.
> > 	# We should really use a backreference to pair up the
> > quotes?
> > 	next unless ( /\s*(.*?)\s*=\s*['"]?(.*)['"]?/ );
> > 	$config{$1} = $2;
> > }
> > 
> > Can you combine the regexps to get this back on one line?
> > (for those 
> > who take joy from this sort of thing?)
> > 
> > Sam
> > 
> > _______________________________________________
> > Austin mailing list
> > Austin at mail.pm.org
> > http://mail.pm.org/mailman/listinfo/austin
> 
> _______________________________________________
> Austin mailing list
> Austin at mail.pm.org
> http://mail.pm.org/mailman/listinfo/austin

-- 

Wayne Walker
wwalker at bybent.com                 Do you use Linux?!
http://www.bybent.com              Get Counted!  http://counter.li.org/
Perl - http://www.perl.org/        Perl User Groups - http://www.pm.org/
Jabber IM:  wwalker at jabber.phototropia.org       AIM:     lwwalkerbybent



More information about the Austin mailing list