Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Unwrapping values in a template

by hacker (Priest)
on Nov 15, 2003 at 02:26 UTC ( [id://307286]=note: print w/replies, xml ) Need Help??


in reply to Re: Unwrapping values in a template
in thread Unwrapping values in a template

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.

Replies are listed 'Best First'.
Re: Re: Unwrapping values in a template
by Anonymous Monk on Nov 15, 2003 at 03:16 UTC

    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.

        What if every single key wraps, regardless of width? Again, his solution handles that.

        Really? It doesn't handle even the single extra wrapped key shown in the template in this node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found