| Class-MakeMethods documentation | view source | Contained in the Class-MakeMethods distribution. |
Class::MakeMethods::Utility::ArraySplicer - Common array ops
use Class::MakeMethods::Utility::ArraySplicer; # Get one or more values $value = array_splicer( $array_ref, $index ); @values = array_splicer( $array_ref, $index_array_ref ); # Set one or more values array_splicer( $array_ref, $index => $new_value, ... ); # Splice selected values in or out array_splicer( $array_ref, [ $start_index, $end_index], [ @values ]);
This module provides a utility function and several associated constants which support a general purpose array-splicer interface, used by several of the Standard and Composite method generators.
This is a general-purpose array accessor function. Depending on the arguments passed to it, it will get, set, slice, splice, or otherwise modify your array.
# Get all values $value_ref = array_splicer( $array_ref ); @values = array_splicer( $array_ref );
# Get one value $value = array_splicer( $array_ref, $index );
# Set contents of array array_splicer( $array_ref, [ $value1, $value2, ... ] );
# Reset the array contents to empty array_splicer( $array_ref, [] );
# Get slice of values @values = array_splicer( $array_ref, undef, [ $index1, $index2, ... ] );
# Set one or more values by index array_splicer( $array_ref, $index1 => $value1, $index2 => $value2, ... );
# Splice selected values in or out array_splicer( $array_ref, [ $start_index, $count], [ @values ]);
# Insert an item at position in the array $obj->bar([3], 'Potatoes' );
# Remove 1 item from position 3 in the array $obj->bar([3, 1], undef );
# Set a new value at position 2, and return the old value print $obj->bar([2, 1], 'Froth' );
# Unshift an item onto the front of the list array_splicer( $array_ref, [0], 'Bubbles' );
# Shift the first item off of the front of the list print array_splicer( $array_ref, [0, 1], undef );
# Push an item onto the end of the list array_splicer( $array_ref, [undef], 'Bubbles' );
# Pop the last item off of the end of the list print array_splicer( $array_ref, [undef, 1], undef );
There are also constants symbols to facilitate some common combinations of splicing arguments:
# Reset the array contents to empty array_splicer( $array_ref, array_clear ); # Set the array contents to provided values array_splicer( $array_ref, array_splice, [ 2, 3 ] ); # Unshift an item onto the front of the list array_splicer( $array_ref, array_unshift, 'Bubbles' ); # Shift it back off again print array_splicer( $array_ref, array_shift ); # Push an item onto the end of the list array_splicer( $array_ref, array_push, 'Bubbles' ); # Pop it back off again print array_splicer( $array_ref, array_pop );
See Class::MakeMethods for general information about this distribution.
See Class::MakeMethods::Standard::Hash and numerous other classes for examples of usage.
| Class-MakeMethods documentation | view source | Contained in the Class-MakeMethods distribution. |