Data::Reuse

Share constant values with Data::Alias
Download

Data::Reuse Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Elizabeth Mattijsen
  • Publisher web site:
  • http://search.cpan.org/~elizabeth/

Data::Reuse Tags


Data::Reuse Description

Share constant values with Data::Alias By default, Perl doesn't share literal ( 0, 'foo' , "bar" ) values. That's because once a literal value is stored in variable (a container), the contents of that container can be changed. Even if such a container is marked "read-only" (e.g. with a module such as Scalar::ReadOnly), it will not cause the values to be shared. So each occurrence of the same literal value has its own memory location, even if it is internally marked as read-only.In an ideal world, perl would keep a single copy of each literal value (container) and have all occurrences in memory point to the same container. Once an attempt is made to change the container would perl make a copy of the container and put the new value in there. This principle is usually referred to as Copy-On-Write (COW). Unfortunately, perl doesn't have this.Comes in the Data::Alias module which allows you to share containers between different variables (amongst other things). But it still does not allow you to have literal values share the same memory locations.Data::Reuse is a Perl module which allows you to easily have literal and read-only values share the same memory address. Which can save you a lot of memory when you are working with large data structures with similar values. Which is especially nice in a mod_perl environment, where memory usage of persistent processes is one of the major issues..Of course, no memory savings will occur for literal values that only occur once. So it is important that you use the functionality of this module wisely, only on values that you expect to be duplicated at least two times.SYNOPSIS use Data::Reuse qw(fixate); fixate my @array => ( 0, 1, 2, 3 ); # initialize and fixate my @filled_array = ( 0, 1, 2, 3 ); fixate @filled_array; # fixate existing values print \$array == \$filled_array ? "Share memory\n" : "Don't share memory\n"; fixate my %hash => ( zero => 0, one => 1, two => 2, three => 3 ); my %filled_hash = ( zero => 0, one => 1, two => 2, three => 3 ); fixate %filled_hash; print \$hash{zero} == \$filled_hash{zero} ? "Share memory\n" : "Don't share memory\n"; use Data::Reuse qw(reuse); reuse my $listref = ; reuse my $hashref = { zero => 0, one => 1, two => 2, three => 3 }; print \$listref-> == \$hashref->{zero} ? "Share memory\n" : "Don't share memory\n"; use Data::Alias qw(alias); use Data::Reuse qw(reuse); alias my @foo = reuse ( 0, 1, 2, 3 ); print \$foo == \$hashref->{zero} ? "Share memory\n" : "Don't share memory\n"; use Data::Reuse qw(spread); spread my %spread_hash => undef, qw(foo bar baz); print \$spread_hash{foo} == \$spread_hash{bar} ? "Share memory\n" : "Don't share memory\n"; spread my @spread_array => 1, 0 .. 3; print \$spread_array == \$spread_array ? "Share memory\n" : "Don't share memory\n"; use Data::Reuse qw(forget); forget(); # free memory used for tracking constant values Requirements: · Perl


Data::Reuse Related Software