| 1 | package Moo::Google::Util; | ||
| 2 | $Moo::Google::Util::VERSION = '0.03'; | ||
| 3 | |||
| 4 | # ABSTRACT: Portable functions | ||
| 5 | |||
| 6 | # use Exporter 'import'; # gives you Exporter's import() method directly | ||
| 7 | # our @EXPORT_OK = qw(substitute_placeholders); # symbols to export on request | ||
| 8 | |||
| 9 | use Moo; | ||
| 10 | |||
| 11 | has 'debug' => ( is => 'rw', default => 0, lazy => 1 ); | ||
| 12 | |||
| 13 | # has 'calendarId' => ( is => 'rw' ); | ||
| 14 | |||
| 15 | use Data::Dumper; | ||
| 16 | |||
| 17 | |||
| 18 | sub substitute_placeholders { | ||
| 19 | my ( $self, $string, $parameters ) = @_; | ||
| 20 | |||
| 21 | # find all parameters in string | ||
| 22 | my @matches = $string =~ /{([a-zA-Z_]+)}/g; | ||
| 23 | |||
| 24 | warn "Util substitute_placeholders() matches: " . Dumper \@matches | ||
| 25 | if ( $self->debug ); | ||
| 26 | |||
| 27 | for my $prm (@matches) { | ||
| 28 | |||
| 29 | # warn $prm; | ||
| 30 | if ( defined $parameters->{$prm} ) { | ||
| 31 | my $s = $parameters->{$prm}; | ||
| 32 | warn "Value of " . $prm . " took from passed parameters: " . $s | ||
| 33 | if ( $self->debug ); | ||
| 34 | $string =~ s/{$prm}/$s/g; | ||
| 35 | |||
| 36 | #} | ||
| 37 | # elsif (defined $self->$prm) { | ||
| 38 | # my $s = $self->$prm; | ||
| 39 | # warn "Value of ".$prm." took from class attributes: ".$s; | ||
| 40 | # $string =~ s/{$prm}/$s/g; | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | die "cant replace " . $prm . " placeholder: no source"; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return $string; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub substitute_placeholder { | ||
| 51 | my ( $self, $string, $var ) = @_; | ||
| 52 | my $param_name; | ||
| 53 | if ( $string =~ /{([a-zA-Z]+)}/ ) { | ||
| 54 | $param_name = $1; | ||
| 55 | } | ||
| 56 | if ( defined $var ) { | ||
| 57 | $string =~ s/{([a-zA-Z]+)}/$var/; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | my $subst = $self->$param_name; | ||
| 61 | $string =~ s/{([a-zA-Z]+)}/$subst/; | ||
| 62 | } | ||
| 63 | return $string; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ |