Devel-Trepan
view release on metacpan or search on metacpan
lib/Devel/Trepan/Util.pm view on Meta::CPAN
# -*- coding: utf-8 -*-
# Copyright (C) 2011, 2012, 2014 Rocky Bernstein <rocky@cpan.org>
package Devel::Trepan::Util;
use strict; use warnings; use English qw( -no_match_vars );
use vars qw(@EXPORT @ISA @YN);
@EXPORT = qw( hash_merge safe_repr uniq_abbrev extract_expression
parse_eval_suffix parse_eval_sigil
YES NO YES_OR_NO @YN bool2YN);
@ISA = qw(Exporter);
use constant YES => qw(y yes oui si yep ja);
@YN = YES;
use constant NO => qw(n no non nope nein);
push(@YN, NO);
sub YN($)
{
my $response = shift;
!!grep(/^${response}$/i, @YN);
}
# Return 'Yes' for True and 'No' for False, and ?? for anything else
sub bool2YN($)
{
my $bool = shift;
$bool ? 'Yes' : 'No';
}
# Hash merge like Ruby has.
sub hash_merge($$) {
my ($config, $default_opts) = @_;
while (my ($field, $default_value) = each %$default_opts) {
$config->{$field} = $default_value unless defined $config->{$field};
};
$config;
}
sub safe_repr($$;$)
{
my ($str, $max, $elipsis) = @_;
$elipsis = '... ' unless defined $elipsis;
my $strlen = length($str);
return '' unless $strlen;
$str = '' unless $str or $str =~ /\d+/;
if ($max > 0 && $strlen > $max && -1 == index($str, "\n")) {
sprintf("%s%s%s", substr($str, 0, $max/2),
$elipsis, substr($str, $strlen+1-($max)/2));
} else {
$str;
}
}
# name is String and list is an Array of String.
# If name is a unique leading prefix of one of the entries of list,
# then return that. Otherwise return name.
sub uniq_abbrev($$)
{
my ($list, $name) = @_;
my @candidates = ();
for my $try_name (@$list) {
push @candidates, $try_name if 0 == index($try_name, $name);
}
scalar @candidates == 1 ? $candidates[0] : $name;
}
# extract the "expression" part of a line of source code.
# Specifically
# if (expression) -> expression
# elsif (expression) -> expression
# else (expression) -> expression
# until (expression) -> expression
# while (expression) -> expression
# return (expression) -> expression
# my (...) = (expression) -> (...) = (expression)
# my ... = expression -> expression
# ditto for "our" and "local", e.g.
( run in 3.649 seconds using v1.01-cache-2.11-cpan-b301d465b3d )