Apache-Wyrd
view release on metacpan or search on metacpan
Wyrd/Interfaces/Setter.pm view on Meta::CPAN
$result ||=
$string =~ s/(\!:([a-zA-Z_][a-zA-Z_0-9]*)\{(?!.*\{)([^\}]*)\})/defined($$hash{$2})?undef:$3/ges;
} while ($result);
return $string;
}
=pod
=item (scalar) C<_setter_replacements> (hashref, scalar)
internal method for performing value replacements.
=cut
sub _setter_replacements {
my ($self, $hash, $temp) = @_;
foreach my $i (sort {length($b) <=> length($a)} keys(%$hash)) {
#sorted so that the longest go first, avoiding problems where one variable (key) name is a
#substring of another
next unless ($i);#this is to prevent strange tied hashes from creating iloops
$self->_verbose("temp is '$temp', i is '$i' and value is '$$hash{$i}'");
$temp =~ s/\$:\Q$i\E/$$hash{$i}/gi;
}
return $temp;
}
=pod
=item (scalar) C<_setter_defaults> (hashref, scalar)
internal method for setting default template to BASECLASS::_data and default
parameters to null hash.
=cut
sub _setter_defaults {
my ($self, $hash, $temp) = @_;
#if a target ($temp) is provided, use it instead of the data
$temp ||= $self->{'_data'};
$hash = $self->_cgi_hash($temp) unless (ref($hash) eq 'HASH');
return $hash, $temp;
}
=pod
=item (scalar) C<_cgi_hash> (hashref, scalar)
internal method for interpreting the CGI environment into the template
data hashref.
=cut
sub _cgi_hash {
my ($self, $temp, $modifier) = @_;
my $hash = {};
my @params = ();
unless ($temp) {
#give up and use CGIs params
@params = $self->dbl->param;
} else {
#guess at the params from the template
@params = ($temp =~ m/[\$\?\!]\:([a-zA-Z_][a-zA-Z0-9_]+)/g);
}
foreach my $param (@params) {
if ($modifier eq 'escaped') {
$hash->{$param} = Apache::Util::escape_html(scalar($self->dbl->param($param)));
} elsif ($modifier eq 'quoted') {
#scalar is used because of some funny business in dbh -- worth investigating?
$hash->{$param} = $self->dbl->dbh->quote(scalar($self->dbl->param($param)));
} else {
$hash->{$param} = $self->dbl->param($param);
}
$self->_verbose("$param = $$hash{$param}");
}
$self->_debug("Found params ->" . join ', ', @params);
return $hash
}
=pod
=item (scalar) C<_attribute_template> (array)
Shortcut method for quickly creating templates of all attributes in a
wyrd, given an array of attribute names.
=cut
sub _attribute_template {
my ($self, @attributes) = @_;
my $string = join ('', map {qq(\?\:$_\{ $_="\$\:$_\"})} @attributes);
return $string;
}
=pod
=item (scalar) C<_template_hash> (string, [hashref])
Shortcut method for quickly creating a hash from a template. If the
template is not provided, the object's _data attribute is used. If a
hashref is supplied as the second value, values for the returned hashref are
based on that. Otherwise, the calling object itself provides the values. By
default, template items that are not defined by the attributes of the object
or provided hashref are ignored.
=cut
sub _template_hash {
my ($self, $template, $hash) = @_;
$template ||= $self->{'_data'};
$hash ||= $self;
my @keys = $template =~ /[\$\!\?]:([_a-zA-Z][_a-zA-Z0-9]+)/g;
my %out_hash = ();
foreach my $key (@keys) {
if (eval{exists($hash->{$key})}) {
$out_hash{$key} = $hash->{$key};
}
}
return \%out_hash;
}
=pod
( run in 2.311 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )