| 1 | # | ||||||||||||||
| 2 | # Copyright (c) 2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||
| 8 | # | ||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | FP::Id | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | ▶ | use FP::Id; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | is id("a"),"a"; | ||||||||||||||
| 18 | my $a = []; | ||||||||||||||
| 19 | my $b = []; | ||||||||||||||
| 20 | ok(id($a) eq id($a)); | ||||||||||||||
| 21 | ok not id($a) eq id($b); | ||||||||||||||
| 22 | # Objects can implement FP::Abstract::Id to override using their | ||||||||||||||
| 23 | # pointer as the id. *Or* should the default be the show() string? | ||||||||||||||
| 24 | |||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 SEE ALSO | ||||||||||||||
| 28 | |||||||||||||||
| 29 | L<FP::Abstract::Id>. | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =head1 NOTE | ||||||||||||||
| 32 | |||||||||||||||
| 33 | This is alpha software! Read the status section in the package README | ||||||||||||||
| 34 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =cut | ||||||||||||||
| 37 | |||||||||||||||
| 38 | package FP::Id; | ||||||||||||||
| 39 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 2 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 2 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 2 | ▶ | use Exporter "import"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 2 | our @EXPORT = qw(id); | |||||||||||||
| 46 | 2 | our @EXPORT_OK = qw(); | |||||||||||||
| 47 | 2 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||
| 48 | |||||||||||||||
| 49 | 2 | ▶ | use FP::Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | # XX confusion with the `identity` function? What other name would be | ||||||||||||||
| 53 | # appropriate? | ||||||||||||||
| 54 | 25 | ▶ | sub id { | ||||||||||||
|
Subroutine
id: called CC=5 SCAR=19.6
Pod
undocumented
| |||||||||||||||
| 55 | 25 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 25 | my ($v) = @_; | |||||||||||||
| 57 | 25 | ▶ | if (blessed $v) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 58 | 0 | ▶ | if (defined(my $m = $v->can("FP_Id_id"))) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 0 | $m->($v) | |||||||||||||
| 60 | } else { | ||||||||||||||
| 61 | 0 | $v +0 | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | } elsif (length ref $v) { | ||||||||||||||
| 64 | 4 | $v +0 | |||||||||||||
| 65 | } else { | ||||||||||||||
| 66 | 21 | "$v" | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 2 | 1 | |||||||||||||