Acme-Tie-Formatted

 view release on metacpan or  search on metacpan

lib/Acme/Tie/Formatted.pm  view on Meta::CPAN

  my $class = shift;

  # Someplace to hang our hat.
  bless \my($self), $class;
}

sub FETCH {
  my ($self, $key) = @_;
  return '' unless $key;

  my @args = split $;, $key, -1;

  # Return a null string if nothing was passed in.
  return '' unless @args;

  my $format = pop @args;

  # Return a null string if there were no arguments
  # to be formatted.
  return '' unless @args;

  # Format arguments and return.
  local $_;
  return join($", map { sprintf($format, $_) } @args);
}

# Stolen directly from Tie::Comma.
# Invalidate all other hash access.
use subs qw(
 STORE    EXISTS    CLEAR    FIRSTKEY    NEXTKEY  );
*STORE = *EXISTS = *CLEAR = *FIRSTKEY = *NEXTKEY =
  sub {
    croak "You can only use %format by accessing it";
  };


1; # Magic true value required at end of module

__END__

=head1 NAME

Acme::Tie::Formatted - embed sprintf() formatting in regular print()


=head1 VERSION

This document describes Acme::Tie::Formatted version 0.03


=head1 SYNOPSIS

    use Acme::Tie::Formatted;
    print "The value is $format{$number, "%3d"} ",
          "(or $format{$number, "%04x"} in hex)\n";

    print "some numbers: $format{ 12, 492, 1, 8753, "%04d"}\n";

=head1 DESCRIPTION

This module creates a global read-only hash, C<%format>, for formatting
data items with standard C<sprintf> format specifications. Since it's a
hash, you can interpolate it into strings as well as use it standalone.

The hash should be "accessed" with two or more "keys". The last key
is interpreted as a C<sprintf> format for each data item specified in the
preceeding arguments. This allows you to format multiple items at once
using the same format for each.

=head2 Alternate name

If you prefer, you can specify a different name for the magical
formatting hash by supplying it as as argument when C<use>ing the
module:

  use Acme::Tie::Formatted qw(z);

This makes C<%z> the magic hash instead.

  print "This is hex: $z{255, "%04x"}\n";

C<Acme::Tie::Formatted> currently supports only one format in the final
argument; this may change if there is demand for it.

=head1 DIAGNOSTICS

=over

=item C<< You can only use %format by accessing it >>

You tried to store something in C<%format>, check if an element
exists in it, delete an element, empty out the hash, or
access the key in it.

None of these operations do anything; only reading elements
of the hash works.

=back


=head1 CONFIGURATION AND ENVIRONMENT

Tie::Formatted requires no configuration files or environment variables.

=head1 DEPENDENCIES

None.

=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

Fixed in 0.04: A number of POD errors and typos. Thanks to Frank Wiegand
(frank.weigand@gmail.com) for the patch.

Please report any bugs or feature requests to
C<bug-tie-formatted@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.



( run in 2.769 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )