in reply to Unwrapping values in a template

How about munging the resulting datastructure instead of trying to massage the incoming data? (note: this works for the data you gave, but not necessarily for all data you will have to deal with - milleage will vary)
use strict; use warnings; use Data::Dumper; use Config::General; my %config = ParseConfig(\*DATA); for (keys %{$config{template}}) { if (/^http/) { $config{template}{home_url} = $_; delete $config{template}{$_}; last; } } print Dumper \%config; __DATA__ <template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template>

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Unwrapping values in a template
by hacker (Priest) on Nov 15, 2003 at 02:26 UTC
    Ooooo, so close!

    I was all ready to hand you the gold star for this solution, until I realized, it drops the key entirely. Try this somewhere in the code you've suggested, and then try it again after putting the url back up into place in the template, manually:

    my %params = %{$config{'template'}}; print $params{'home_url'};

    However, this works, but isn't as elegant:

    print $config{'template'}->{'home_url'};

    Update: After screwing my head on straight, I realized that my initializer for %params was before the modification to the hash, and should have been after. This works properly, as it should:

    my %config = ParseConfig(\*DATA); for (keys %{$config{template}}) { if (/^http/) { $config{template}{home_url} = $_; delete $config{template}{$_}; last; } } $Data::Dumper::Sortkeys = \%config; print Dumper(\%config); print $params{'home_url'};

    Please ignore that man behind the curtain, it's just me making silly mistakes again.

      Just so you realize that you've hardcoded a solution for a single data set, whereas some of the other posted solutions are more generalized.

        "Just so you realize that you've hardcoded a solution for a single data set, whereas some of the other posted solutions are more generalized."

        Actually, yes. The "hardcoded" solution provided works in a much larger set of scenarios than the home-grown regex solutions provided. What if there are 200 ^M characters before and after the wrapped key? jeffa's solution handles that (these regexes do not). What if every single key wraps, regardless of width? Again, his solution handles that.

        The other solutions, while unary and interesting on their own, are actually much more "hardcoded" (i.e. will only work on a very small subset of the strings involved) than the solution jeffa provided.

        Can each of the other solutions be modified to work with all scenarios? Yes, sure, but at what cost and maintenance headache a year from now?

        Can jeffa's solution be modified to not be so hard-coded? Yes, and with a much longer-term benefit and maintenance advantage over debugging regexes each time the template format changes ever-so-slightly.