String-Formatter

 view release on metacpan or  search on metacpan

lib/String/Formatter.pm  view on Meta::CPAN

  no strict 'refs';
  for my $method (keys %METHODS) {
    *$method = sub { $_[0]->{ $method } };

    my $default = "default_$method";
    *$default = sub { $METHODS{ $method } };
  }
}

#pod =method new
#pod
#pod   my $formatter = String::Formatter->new({
#pod     codes => { ... },
#pod     format_hunker   => ...,
#pod     input_processor => ...,
#pod     string_replacer => ...,
#pod     hunk_formatter  => ...,
#pod   });
#pod
#pod This returns a new formatter.  The C<codes> argument contains the formatting
#pod codes for the formatter in the form:
#pod
#pod   codes => {
#pod     s => 'fixed string',
#pod     S => 'different string',
#pod     c => sub { ... },
#pod   }
#pod
#pod Code values (or "conversions") should either be strings or coderefs.  This
#pod hashref can be accessed later with the C<codes> method.
#pod
#pod The other four arguments change how the formatting occurs.  Formatting happens
#pod in five phases:
#pod
#pod =for :list
#pod 1. format_hunker - format string is broken down into fixed and %-code hunks
#pod 2. input_processor - the other inputs are validated and processed
#pod 3. string_replacer - replacement strings are generated by using conversions
#pod 4. hunk_formatter - replacement strings in hunks are formatted
#pod 5. all hunks, now strings, are recombined; this phase is just C<join>
#pod
#pod The defaults are found by calling C<default_WHATEVER> for each helper that
#pod isn't given.  Values must be either strings (which are interpreted as method
#pod names) or coderefs.  The semantics for each method are described in the
#pod methods' sections, below.
#pod
#pod =cut

sub default_codes {
  return {};
}

sub new {
  my ($class, $arg) = @_;

  my $_codes = {
    %{ $class->default_codes },
    %{ $arg->{codes} || {} },
  };

  my $self = bless { codes => $_codes } => $class;

  for (keys %METHODS) {
    $self->{ $_ } = $arg->{ $_ } || do {
      my $default_method = "default_$_";
      $class->$default_method;
    };

    $self->{$_} = $self->can($self->{$_}) unless ref $self->{$_};
  }

  my $codes = $self->codes;

  return $self;
}

sub codes { $_[0]->{codes} }

#pod =method format
#pod
#pod   my $result = $formatter->format( $format_string, @input );
#pod
#pod   print $formatter->format("My %h is full of %e.\n", 'hovercraft', 'eels');
#pod
#pod This does the actual formatting, calling the methods described above, under
#pod C<L</new>> and returning the result.
#pod
#pod =cut

sub format {
  my $self   = shift;
  my $format = shift;

  Carp::croak("not enough arguments for stringf-based format")
    unless defined $format;

  my $hunker = $self->format_hunker;
  my $hunks  = $self->$hunker($format);

  my $processor = $self->input_processor;
  my $input = $self->$processor([ @_ ]);

  my $replacer = $self->string_replacer;
  $self->$replacer($hunks, $input);

  my $formatter = $self->hunk_formatter;
  ref($_) and $_ = $self->$formatter($_) for @$hunks;

  my $string = join q{}, @$hunks;

  return $string;
}

#pod =method format_hunker
#pod
#pod Format hunkers are passed strings and return arrayrefs containing strings (for
#pod fixed content) and hashrefs (for formatting code sections).
#pod
#pod The hashref hunks should contain at least two entries:  C<conversion> for the
#pod conversion code (the s, d, or u in %s, %d, or %u); and C<literal> for the
#pod complete original text of the hunk.  For example, a bare minimum hunker should

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.800 second using v1.00-cache-2.02-grep-82fe00e-cpan-9e6bc14194b )