Skip to content

Commit

Permalink
Add clone() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
deven committed Dec 8, 2020
1 parent f31628f commit 17c4920
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/PDF/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ sub new {
return $pdf->validate;
}

# Deep clone entire PDF::Data object or specified data structure.
sub clone {
my ($self, $object, $previous) = @_;

# Default to cloning the entire PDF::Data object.
$object //= $self;

# Check object type.
if (is_hash $object) {
# Get hash keys.
my @keys = sort keys %{$object};

# Check if this is a parent object.
return undef if $previous and grep {
($object->{$_} // "") eq $previous or is_array $object->{$_} and grep { $_ eq $previous; } @{$object->{$_}}
} @keys;

# Clone the hash.
return { map { $_, ref $object->{$_} ? $self->clone($object->{$_}, $object) : $object->{$_}; } @keys};
} elsif (is_array $object) {
# Check if this is a parent object.
return undef if $previous and grep { $_ eq $previous; } @{$object};

# Clone the array.
return [ map { ref $_ ? $self->clone($_, $object) : $_; } @{$object} ];
} elsif (ref $object) {
# Sanity check.
die "Unknown reference type \"" . ref($object) . "\"!\n";
} else {
# Not a reference, return the value.
return $object;
}
}

# Create a new page with the specified size.
sub new_page {
my ($self, $x, $y) = @_;
Expand Down Expand Up @@ -902,6 +936,12 @@ structures that can be readily manipulated.
Constructor to create an empty PDF::Data object instance.
=head2 clone
my $clone = $pdf->clone($data);
Deep copy the PDF::Data object itself (by default), or the specified data structure.
=head2 new_page
my $page = $pdf->new_page(8.5, 11);
Expand Down

0 comments on commit 17c4920

Please sign in to comment.