Acme-List-CarCdr
view release on metacpan or search on metacpan
lib/Acme/List/CarCdr.pm view on Meta::CPAN
# -*- Perl -*-
#
# c[ad]+r list-operation support for Perl, based on (car) and (cdr) and
# so forth of lisp fame, though with a limit of 704 as to the maximum
# length any such shenanigans.
#
# Run perldoc(1) on this file for additional documentation.
package Acme::List::CarCdr;
use 5.010000;
use strict;
use warnings;
use Carp qw(croak);
lib/Acme/List/CarCdr.pm view on Meta::CPAN
my $method = our $AUTOLOAD;
if ( $method =~ m/::c([ad]{1,704})r$/ ) {
my $ops = reverse $1;
my $self = shift;
my $ref = \@_;
my $start = 0;
my $end;
my $delve = 0;
while ( $ops =~ m/\G([ad])(\1*)/cg ) {
my $op = $1;
my $len = length $2 || 0;
if ( $op eq 'a' ) {
if ( $len > 0 ) {
for my $i ( 1 .. $len ) {
if ( ref $ref->[$start] ne 'ARRAY' ) {
croak "$method: " . $ref->[$start] . " is not a list";
}
$ref = $ref->[$start];
$start = 0;
}
}
eval 'use Test::Differences'; # display convenience
my $deeply = $@ ? \&is_deeply : \&eq_or_diff;
########################################################################
use Acme::List::CarCdr;
my $can = Acme::List::CarCdr->new;
# The reason for using names instead of numbers is that numbers may be
# returned by various array operations (length, index) that might be
# improperly confused with a test list that contains numbers instead of
# names. (Alternative: make the numbers contained by the list larger
# than the number of elements possible in the list.)
$deeply->( $can->car(qw/cat dog fish/), qw/cat/ );
$deeply->( [ $can->cdr(qw/cat dog fish/) ], [qw/dog fish/] );
$deeply->( $can->cddr(qw/cat dog fish/), qw/fish/ );
# Slightly more complicated, plus comparison with GNU CLISP 2.48
# [1]> (setf numlist '(("one" "two") ("three" "four") ("five" "six")))
my @numlist = ([q/one two/],[qw/three four/],[qw/five six/]);
( run in 0.527 second using v1.01-cache-2.11-cpan-65fba6d93b7 )