Biblio-SICI

 view release on metacpan or  search on metacpan

lib/Biblio/SICI/Util.pm  view on Meta::CPAN


package Biblio::SICI::Util;
{
  $Biblio::SICI::Util::VERSION = '0.04';
}

# ABSTRACT: Utility functions

use strict;
use warnings;
use 5.010001;

BEGIN {
	$Biblio::SICI::Util::TITLE_CODE = qr/[0-9A-Z&´*\\\{\}\(\)\[\],\@\$=!#%.+?";\/^\`~_|-]/;
}

use Exporter 'import';
our @EXPORT_OK = qw( calculate_check_char titleCode_from_title );

use Try::Tiny;


sub titleCode_from_title {
	my $title = shift;

	die "Expected title string as parameter" unless defined($title) and $title;

	my $code = '';

	try {
		require Text::Unidecode;
	}
	catch {
		warn __PACKAGE__ . "::titleCode_from_title() - unable to load 'Text::Unidecode': " . $_;
	};

	try {
		require Text::Undiacritic;
	}
	catch {
		warn __PACKAGE__ . "::titleCode_from_title() - unable to load 'Text::Undiacritic': " . $_;
	};

	my @words = split( /\s+/, $title );
	my @chars = ();
	foreach my $word (@words) {
		my $firstChar = uc( substr( $word, 0, 1 ) );
		if ( $firstChar =~ $Biblio::SICI::Util::TITLE_CODE ) {
			push @chars, $firstChar;
		}
		else {
			try {
				$word = Text::Unidecode::unidecode($word);
			};
			try {
				$word = Text::Undiacritic::undiacritic($word);
			};
			$firstChar = uc( substr( $word, 0, 1 ) );
			if ( $firstChar =~ $Biblio::SICI::Util::TITLE_CODE ) {
				push @chars, $firstChar;
			}
		}
	}

	if ( @chars >= 1 ) {
		$code = join( "", splice( @chars, 0, 6 ) );
		return $code;
	}

	return;
} ## end sub titleCode_from_title


sub calculate_check_char {
	my $str = shift;

	return unless defined($str) and $str;

	state $charValues = {
		0 => 0,  1 => 1,  2 => 2,  3 => 3,  4   => 4,  5 => 5,  6 => 6,  7 => 7,



( run in 1.258 second using v1.01-cache-2.11-cpan-6de40a662fe )