Skip Menu |

This queue is for tickets about the Scalar-List-Utils CPAN distribution.

Report information
The Basics
Id: 105290
Status: new
Priority: 0/
Queue: Scalar-List-Utils

People
Owner: Nobody in particular
Requestors: leonerd-cpan [...] leonerd.org.uk
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: a deref function to complement reftype
--- every so often, i find myself wanting to write code like: my @vals = ref $arg ? @$arg : $arg; i was looking around cpan and was tempted to write a generic Sub::Deref, until i realized such a function is a nice complement to Scalar::Util::reftype. Below i have 2 pure perl implementations. I have no idea what the XS code would look like, but i wanted to see if any of the maintainers would be interested in including such a function. I'd be up for writing the unit tests. sub deref($) { my $type = Scalar::Util::reftype $_[0]; return $type ? $type eq 'ARRAY' ? @{$_[0]} : $type eq 'CODE' ? &{$_[0]} : $type eq 'GLOB' ? *{$_[0]} : $type eq 'HASH' ? %{$_[0]} : $type eq 'REF' ? ${$_[0]} : $type eq 'SCALAR' ? ${$_[0]} : die "$type can't be dereferenced" : $_[0]; } my %reftype_to_dereferencer = ( ARRAY => sub { @{$_[0]} }, CODE => sub { &{$_[0]} }, GLOB => sub { *{$_[0]} }, HASH => sub { %{$_[0]} }, REF => sub { ${$_[0]} }, SCALAR => sub { ${$_[0]} }, ); sub deref($) { my $type = Scalar::Util::reftype $_[0]; return $_[0] unless $type; my $dereferencer = $reftype_to_dereferencer{$type}; die "$type can't be dereferenced" unless $dereferencer; return $dereferencer->($_[0]); } --- [copied from https://github.com/Scalar-List-Utils/Scalar-List-Utils/issues/3]