Acme-Lvalue
view release on metacpan or search on metacpan
lib/Acme/Lvalue.pm view on Meta::CPAN
sub {
$_[0]
? defined $_[1]
? $_[1]
: 1
: undef
}
],
[exp => sub { log $_[0] }],
[hex => sub { sprintf '%x', $_[0] }],
[length =>
sub {
my ($n, $x) = @_;
my $l = length $x;
$n <= $l
? substr $x, 0, $n
: $x . "\0" x ($n - $l)
}
],
[log => sub { exp $_[0] }],
[oct => sub { sprintf '%o', $_[0] }],
[ord => sub { chr $_[0] }],
[quotemeta => sub { $_[0] =~ s/\\(.)/$1/sgr }],
[reverse => sub { scalar reverse $_[0] }],
lib/Acme/Lvalue.pm view on Meta::CPAN
Acme::Lvalue - Generalized lvalue subroutines
=head1 SYNOPSIS
use Acme::Lvalue qw(:builtins)
my $x;
sqrt($x) = 3; # $x == 9
hex($x) = 212; # $x eq "d4"
$x = 2;
length(sqrt($x)) = 5; # $x == 1.999396
=head1 DESCRIPTION
This module makes a number of perl builtins return lvalues, letting you assign
to them. This lets you do things like:
reverse(hex $x) = '9558295373';
# $x eq 'deadbeef'
# because hex 'deadbeef' == 3735928559
# and reverse '3735928559' eq '9558295373'
lib/Acme/Lvalue.pm view on Meta::CPAN
=item * The string C<:builtins>.
This overrides the following builtins:
L<C<chr>|perlfunc/chr>,
L<C<cos>|perlfunc/cos>,
L<C<defined>|perlfunc/defined>,
L<C<exp>|perlfunc/exp>,
L<C<hex>|perlfunc/hex>,
L<C<length>|perlfunc/length>,
L<C<log>|perlfunc/log>,
L<C<oct>|perlfunc/oct>,
L<C<ord>|perlfunc/ord>,
L<C<quotemeta>|perlfunc/quotemeta>,
L<C<reverse>|perlfunc/reverse>,
L<C<sin>|perlfunc/sin>,
L<C<sqrt>|perlfunc/sqrt>.
=item * Any of the builtins listed above.
use Acme::Lvalue qw(:builtins), [succ => sub { $_[0] + 1 }, sub { $_[0] - 1 }];
is sqrt(9), 3;
sqrt(my $x) = 2;
is $x, 4;
is reverse("abcd"), "dcba";
reverse($x) = "ypnftm";
is $x, "mtfnpy";
is length("foobar"), 6;
length($x = "truism") = 4;
is $x, "trui";
length($x) = 10;
is $x, "trui\0\0\0\0\0\0";
is succ(3), 4;
succ($x) = 43;
is $x, 42;
my $r = \sqrt($x);
$$r = 3;
is $x, 9;
use strict;
use Test::More tests => 3;
use Acme::Lvalue [succ => sub { $_[0] + 1 }, sub { $_[0] - 1 }], qw(:builtins);
my $x;
succ(succ($x)) = 4;
is $x, 2;
length(sqrt($x)) = 5;
is $x, '1.999396';
reverse(hex $x) = '9558295373';
is $x, 'deadbeef';
( run in 0.525 second using v1.01-cache-2.11-cpan-65fba6d93b7 )