use Data::Munge;
use overload
- '""' => 'escaped_string',
- '.' => 'dot',
- 'bool' => 'is_true',
+ '""' => '_hsv_escaped_string',
+ '.' => '_hsv_dot',
+ 'bool' => '_hsv_is_true',
fallback => 1,
;
sub new {
+ if (ref($_[0])) { my $c = shift; return $c->_hsv_unescaped_string->new(@_) }
my ($class, @raw_parts) = @_;
my $opts = (ref($raw_parts[-1]) eq 'HASH') ? pop(@raw_parts) : {};
return $self;
}
-sub escaped_string {
+sub AUTOLOAD {
+ my $invocant = shift;
+ (my $meth = our $AUTOLOAD) =~ s/.*:://;
+ die "No such method ${meth} on ${invocant}"
+ unless ref($invocant);
+ return $invocant->_hsv_unescaped_string->$meth(@_);
+}
+
+sub _hsv_escaped_string {
my $self = shift;
if ($self->{ignore}{scalar caller}) {
- return $self->unescaped_string;
+ return $self->_hsv_unescaped_string;
}
return join '', map +(
), @{$self->{parts}};
}
-sub unescaped_string {
+sub _hsv_unescaped_string {
my $self = shift;
return join '', map $_->[0], @{$self->{parts}};
}
-sub dot {
+sub _hsv_dot {
my ($self, $str, $prefix) = @_;
return $self unless $str;
return ref($self)->new(@parts, { ignore => $self->{ignore} });
}
-sub is_true {
+sub _hsv_is_true {
my ($self) = @_;
return 1 if grep length($_), map $_->[0], @{$self->{parts}};
}
is("$four", '<tag>Hi <bob></tag>');
+{
+ package MyPkg;
+
+ sub new { 'foo' }
+
+ sub load { 'bar' }
+}
+
+is(html('MyPkg')->new, 'foo');
+
+is(html('MyPkg')->load, 'bar');
+
done_testing;