/usr/share/perl5/Data/AMF/Formatter/AMF0.pm is in libdata-amf-perl 0.09-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | package Data::AMF::Formatter::AMF0;
use Any::Moose;
require bytes;
use Scalar::Util qw/looks_like_number blessed/;
use Data::AMF::IO;
has 'io' => (
is => 'rw',
isa => 'Data::AMF::IO',
lazy => 1,
default => sub {
Data::AMF::IO->new( data => q[] );
},
);
no Any::Moose;
sub format {
my ($self, @obj) = @_;
$self = $self->new unless blessed $self;
for my $obj (@obj) {
if (my $pkg = blessed $obj) {
$self->format_typed_object($obj);
}
elsif (my $ref = ref($obj)) {
if ($ref eq 'ARRAY') {
$self->format_strict_array($obj);
}
elsif ($ref eq 'HASH') {
$self->format_object($obj);
}
else {
Carp::confess qq[cannot format "$ref" object];
}
}
else {
if (looks_like_number($obj) && $obj !~ /^0\d/) {
$self->format_number($obj);
}
elsif (defined($obj)) {
$self->format_string($obj);
}
else {
$self->format_null($obj);
}
}
}
$self->io->data;
}
sub format_number {
my ($self, $obj) = @_;
$self->io->write_u8(0x00);
$self->io->write_double($obj);
}
sub format_string {
my ($self, $obj) = @_;
$self->io->write_u8(0x02);
$self->io->write_utf8($obj);
}
sub format_strict_array {
my ($self, $obj) = @_;
my @array = @{ $obj };
$self->io->write_u8(0x0a);
$self->io->write_u32( scalar @array );
for my $v (@array) {
$self->format($v);
}
}
sub format_object {
my ($self, $obj) = @_;
$self->io->write_u8(0x03);
for my $key (keys %$obj) {
my $len = bytes::length($key);
$self->io->write_u16($len);
$self->io->write($key);
$self->format($obj->{$key});
}
$self->io->write_u16(0x00);
$self->io->write_u8(0x09); # object-end marker
}
sub format_null {
my ($self, $obj) = @_;
$self->io->write_u8(0x05); # null marker
}
sub format_typed_object {
my ($self, $obj) = @_;
$self->io->write_u8(0x10);
my $class = blessed $obj;
$self->io->write_utf8($class);
$self->format_object($obj);
}
__PACKAGE__->meta->make_immutable;
__END__
=head1 NAME
Data::AMF::Formatter::AMF0 - AMF0 serializer
=head1 SYNOPSIS
my $amf0_data = Data::AMF::Formatter::AMF0->format($obj);
=head1 METHODS
=head2 format
=head2 format_number
=head2 format_string
=head2 format_strict_array
=head2 format_object
=head2 format_null
=head2 format_typed_object
=head1 AUTHOR
Daisuke Murase <typester@cpan.org>
=head1 COPYRIGHT
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|