| 1 | package Crypt::Perl::X::Base; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp (); | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ( $class, $string, $props_hr ) = @_; | ||
| 10 | |||
| 11 | $class->_check_overload(); | ||
| 12 | |||
| 13 | my %attrs = $props_hr ? %$props_hr : (); | ||
| 14 | |||
| 15 | return bless [ $string, \%attrs ], $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub get { | ||
| 19 | my ( $self, $attr ) = @_; | ||
| 20 | |||
| 21 | #Do we need to clone this? Could JSON suffice, or do we need Clone? | ||
| 22 | return $self->[1]{$attr}; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub to_string { | ||
| 26 | my ($self) = @_; | ||
| 27 | |||
| 28 | return sprintf '%s: %s', ref($self), $self->[0]; | ||
| 29 | } | ||
| 30 | |||
| 31 | #---------------------------------------------------------------------- | ||
| 32 | |||
| 33 | my %_OVERLOADED; | ||
| 34 | |||
| 35 | sub _check_overload { | ||
| 36 | my ( $class, $str ) = @_; | ||
| 37 | |||
| 38 | #cf. eval_bug.readme | ||
| 39 | my $eval_err = $@; | ||
| 40 | |||
| 41 | $_OVERLOADED{$class} ||= eval qq{ | ||
| 42 | package $class; | ||
| 43 | use overload (q<""> => __PACKAGE__->can('__spew')); | ||
| 44 | 1; | ||
| 45 | }; | ||
| 46 | |||
| 47 | #Should never happen as long as overload.pm is available. | ||
| 48 | warn if !$_OVERLOADED{$class}; | ||
| 49 | |||
| 50 | $@ = $eval_err; | ||
| 51 | |||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub __spew { | ||
| 56 | my ($self) = @_; | ||
| 57 | |||
| 58 | my $spew = $self->to_string(); | ||
| 59 | |||
| 60 | if ( substr( $spew, -1 ) ne "\n" ) { | ||
| 61 | $spew .= Carp::longmess(); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $spew; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |