sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; sub subject { 'Subject: default subject'; } sub to { 'To: default_to@example.com'; } sub from { 'From: default_from@example.com'; } my %config = ( to => { regex => '^To:\s', sub => \&to }, from => { regex => '^From:\s', sub => \&from }, subject => { regex => '^Subject:\s', sub => \&subject }, ); my %cnf; # Load user data foreach (<DATA>) { chomp; foreach my $c (keys %config) { if (/$config{$c}{regex}/) { $cnf{$c} = $_; last; } } } # Look for missing values and fill in defaults foreach (keys %config) { unless (exists $cnf{$_}) { $cnf{$_} = $config{$_}{sub}->(); } } use Data::Dumper; print Dumper \%cnf; __DATA__ To: foo@example.com From: bar@example.com
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How the values of hash is refreshed for each request?
by GrandFather (Saint) on Oct 05, 2006 at 06:20 UTC | |
|
Re: How the values of hash is refreshed for each request?
by ysth (Canon) on Oct 05, 2006 at 06:33 UTC |