hacker has asked for the wisdom of the Perl Monks concerning the following question:
<template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template>
...will end up looking like this, after I receive it in an email:
<template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template>
What I'm trying to do, is "unwrap" that wrapped field before I process the template with Config::General to parse out the keys. Once the keys are properly unwrapped, Config::General has no problem with it. I also looked into Config::General's '-SplitDelimiter' option, but it doesn't consider a newline as whitespace, and my attempts at defining a proper delimeter there failed.
I've been working with a few monks on the CB (thanks diotalevi) to try to work this out, and we've come up with two possibilities, each with a flaw of their own. The problem is that some keys can be left blank, with no value, while others can have values.
This code below, one example of the unwrap code, which properly treats blank keys as it should, but doesn't unwrap the 'home_url' field back up to the previous line:
use Data::Dumper; # This is actually in @body in the larger code open(TMPL, "<my.template") or die $!; my %template = map /(\S+)\s+=\s+(.*)/, split m((?<!\\)\n), ( do { local $/; readline *TMPL; } =~ m(<template>((?s:.*?)</template>)))[0]; $Data::Dumper::Sortkeys = \%template; print Dumper(\%template);
This next bit of code actually unpwraps the 'home_url' field properly, but also unwraps keys with blank values into the value field of the previous key:
use Data::Dumper; open(TMPL, "<my.template") or die $!; my $tmpl; for (<TMPL>) { next if /^#/; if (m!<template>!) { $templ = ""; } elsif (m!</template>!) { my %hash = ($tmpl =~ /^s+(\w+)\s+=\s+(.*)$/mg); if (%hash) { $Data::Dumper::Sortkeys = \%hash; print Dumper(\%hash); } else { print "Bad template: $tmpl\n"; } } elsif (defined $tmpl) { s/^\s+/ /; s/[ \t]$//; s/=\s*\z/= /; $tmpl .= $_; } } close TMPL;
This results in the output that looks like this:
$VAR1 = { 'bar' => '0', 'blort' => 'home_url = http://www.foo.com/some/really/long/ +url', 'foo' => 'Yes' };
Note how the 'home_url' key wraps up into the value side of the previous key.
What I'm trying to do, is keep all keys intact, including the 'home_url' one (which is the only key long enough to wrap, the others don't wrap).
Any helpful pointers here?
Update: I think jeffa is the winner on this one, the working code is now as follows:
my $conf = new Config::General( -ConfigFile => "pler.template", -ExtendedAccess => 1, -InterPolateVars => 1, -AutoTrue => 1, -StrictObjects => 0, ); my %config = $conf->getall; my $template = $conf->obj('template'); my %params = %{$config{'template'}}; for (keys %{$config{template}}) { if (/^http/) { $config{template}{home_url} = $_; delete $config{template}{$_}; last; } } $Data::Dumper::Sortkeys = \%config; print Dumper(\%config);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Unwrapping values in a template
by jeffa (Bishop) on Nov 14, 2003 at 14:16 UTC | |
by hacker (Priest) on Nov 15, 2003 at 02:26 UTC | |
by Anonymous Monk on Nov 15, 2003 at 03:16 UTC | |
by hacker (Priest) on Nov 15, 2003 at 17:56 UTC | |
by Anonymous Monk on Nov 15, 2003 at 19:14 UTC | |
Re: Unwrapping values in a template
by bart (Canon) on Nov 14, 2003 at 15:49 UTC | |
Re: Unwrapping values in a template
by valdez (Monsignor) on Nov 14, 2003 at 14:06 UTC | |
Re: Unwrapping values in a template
by Chmrr (Vicar) on Nov 14, 2003 at 14:35 UTC | |
Re: Unwrapping values in a template
by synistar (Pilgrim) on Nov 14, 2003 at 16:03 UTC | |
Re: Unwrapping values in a template
by EvdB (Deacon) on Nov 14, 2003 at 16:34 UTC | |
Re: Unwrapping values in a template
by eric256 (Parson) on Nov 14, 2003 at 14:10 UTC | |
Re: Unwrapping values in a template
by Anonymous Monk on Nov 14, 2003 at 14:25 UTC |