/usr/share/perl5/Lire/Utils.pm is in lire 2:2.1.1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 | package Lire::Utils;
use strict;
use base qw/ Exporter /;
use File::Spec;
use Fcntl;
use File::Basename;
use Carp;
use Socket; # for gethostbyaddr
use Time::Local;
use vars qw( @EXPORT_OK );
BEGIN {
@EXPORT_OK = qw/ xml_encode latex_encode diff_lists tilde_expand
tempfile tempdir tmpdir min max ratio ratio100
shell_quote indent file_content create_file
sql_quote_name tree_apply item_index
check_param check_object_param
deep_copy host_by_addr is_url parse_url
period_range unique text_for_width /;
}
=pod
=head1 NAME
Lire::Utils - Various general-purpose function.
=head1 SYNOPSIS
use Lire:Utils qw/ xml_encode /;
=head1 DESCRIPTION
This module defines several general purpose functions. No functions
are exported by default, you have to specify the one you want to
import in your namespace when you use the module.
=head2 xml_encode( $str )
Converts standard the characters <,>,&," and ' to their XML entities.
Example
print XML_STREAM xml_encode( $value );
=cut
sub xml_encode {
my $value = $_[0];
$value =~ s/&/&/g;
$value =~ s/</</g;
$value =~ s/"/"/g;
$value =~ s/'/'/g;
return $value;
}
=pod
=head2 latex_encode( $str )
Returns $str with all LaTeX special characters escaped.
=cut
sub latex_encode {
my $str = $_[0];
$str =~ s/\\/--LIRE--BACKSLASH--/g;
$str =~ s/([#\$&_%{}])/\\$1/g;
$str =~ s/([~^])/\\$1\{\}/g;
$str =~ s/(\[|\]|<|>)/\$$1\$/g;
$str =~ s/--LIRE--BACKSLASH--/\$\\backslash\$/g;
return $str;
}
=pod
=head2 tilde_expand( $path )
Does tilde-expansion on a path, if possible. This means that paths
of the form ~/foo are transformed to something like /home/user/foo,
where "/home/user" is the content of the $HOME variable. Paths of
the form ~otheruser/foo are translated similary by a passwd lookup.
=cut
sub tilde_expand {
my $path = $_[0];
return $path if ord( $path ) != 126; # ascii code for ~
my ( $tilde, $rest ) = split( '/', $path, 2 );
my $user = substr( $tilde, 1 ); # skip the initial ~
if ( $user eq '' ) {
croak('$HOME not set')
unless exists $ENV{'HOME'};
$tilde = $ENV{'HOME'};
} else {
my ( $name, $pass, $uid, $gid, $quota, $comment, $gecos, $dir) =
getpwnam( $user );
$tilde = $dir
if defined $dir;
}
$tilde .= "/$rest" if defined $rest;
return $tilde;
}
=pod
=head2 diff_lists( $list1, $list2 )
Compare two list, if the two list contains the same items (even if in
different order) it returns undef. Otherwise an hash reference is
returned which contains the list of new items in the 'new' key and the
list of items to remove (to $list1 to make $list2) in the 'remove'
key.
=cut
sub diff_lists {
my ( $list1, $list2 ) = @_;
croak "list1 param must be an array ref: $list1"
unless ref $list1 eq 'ARRAY';
croak "list2 param must be an array ref: $list2"
unless ref $list2 eq 'ARRAY';
# Sort the items
my @list1 = sort @$list1;
my @list2 = sort @$list2;
my @new = ();
my @remove = ();
while ( @list1 && @list2 ) {
my $d = $list1[0] cmp $list2[0];
if ( $d < 0 ) {
push @remove, shift @list1;
} elsif ( $d == 0 ) {
shift @list1;
shift @list2;
} else {
push @new, shift @list2;
}
}
# All other elements are removed
push @remove, @list1;
push @new, @list2;
if ( @remove || @new ) {
return { 'remove' => \@remove, new => \@new };
} else {
return undef;
}
}
=pod
=head1 PORTABILITY FUNCTIONS
For portability across Perl versions, this module defines some
functions that are usually found in the latest version of Perl but
that may not be present in some old ones (the oldest version of Perl
we support is 5.00503).
=head2 tmpdir()
This method (provided by recent versions of File::Spec) returns where
temporary files should go.
=cut
sub tmpdir {
return ( File::Spec->can( "tmpdir" )
? return File::Spec->tmpdir
: _tmpdir() );
}
sub _tmpdir {
foreach my $dir ( $ENV{'TMPDIR'}, "/var/tmp", "/tmp" ) {
return $dir if -r $dir && -w $dir;
}
croak "no writeable temporary directory available\n";
}
=pod
=head2 tempfile()
my $fh = tempfile();
my $fh = tempfile( $template, 'SUFFIX' => ".txt" )
my ( $fh, $name ) = tempfile( $template, 'SUFFIX' => ".dlf" );
This is a wrapper around the File::Temp::tempfile Perl function when
available, and it offers a home grown version which should be safe
when it isn't available. The only difference is that the file will
always be created in the directory specified in $ENV{'TMPDIR'} or
F</tmp> when unset.
The first argument to the function should be a template name
containing at least 6 X (i.e. tempXXXXXX) which will get replaced to
generate a random name. When no arguments are passed, a default
template of tempfileXXXXXX will be use.
Other options can be passed to the function by using 'key' => value
pairs. The only option understood by the home grown version is SUFFIX
which will be appended to the filename. (The Perl version understands
more options, but you shouldn't use them for compatibility.)
The function takes precautions against symlink attacks (and creates the
file with readwrite permission for the owner only). It will die(), if
it fails to create a temporary file after 10 attempts. (This shouldn't
happen unless someone is seriously trying to race with us.)
The function will return in scalar context an anonymous file handle
opened on the temporary file. The temporary file was unlinked after
creation and will thus be deleted automatically when you close the
file handle.
When used in an array context, the function will return a file handle
and the path to the temporary file (this can be useful for debugging
purpose or when you can't pass the file by file handle to another
process). In this case, the file should be deleted manually.
=cut
my @chars = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9, );
sub tempfile {
eval "use File::Temp";
if ( $@ ) {
return _tempfile( @_ );
} elsif ( @_ ) {
return File::Temp::tempfile( @_, 'DIR' => tmpdir() );
} else {
return File::Temp::tempfile( 'DIR' => tmpdir() );
}
}
sub _tempfile {
# Poor's man tempfile, File::Temp is only part of Perl 5.6.1
my ($tmpl, %args);
if (@_) {
($tmpl, %args) = @_;
$tmpl = tmpdir() . "/" . basename( $tmpl );
} else {
$tmpl = tmpdir() . "/tempfileXXXXXX";
}
# Try 10 times to open a file
for (0..10) {
$tmpl =~ s/X/$chars[rand @chars]/ge;
$tmpl .= $args{'SUFFIX'} if defined $args{'SUFFIX'};
if ( sysopen( my $fh, $tmpl, O_RDWR|O_CREAT|O_EXCL, 0600 ) ) {
binmode $fh;
unlink $tmpl unless wantarray;
return wantarray ? ($fh, $tmpl): $fh;
}
sleep 1;
}
die "tempfile(): somebody is trying to race with us!\n";
}
=pod
=head2 tempdir()
my $dir = tempdir();
my $dir = tempdir( $template )
This is a wrapper around the File::Temp::tempdir Perl function when
available, and it offers a home grown version which should be safe
when itsn't available. The only difference is that the directory will
always be created in the directory specified in $ENV{'TMPDIR'} or
F</tmp> when unset.
The first argument to the function should be a template name
containing at least 6 X (i.e. tempXXXXXX) which will get replaced to
generate a random name. When no arguments are passed, a default
template of tempdirXXXXXX will be used.
Other options can be passed to the function by using 'key' => value
pairs. The only option understood by the home grown version is DIR
which specifies where the directory will be created (The Perl version
understands more options, but you shouldn't use them for
compatibility.)
The function takes precautions against symlink attacks (and create the
file with readwrite permission for the owner only). It will die(), if
it fails to create a temporary directory after 10 attempts. (This
shouldn't happen unless someone is seriously trying to race with us.)
The function will return the name of the directory that was created.
=cut
sub tempdir {
eval "use File::Temp;";
if ( $@ ) {
return _tempdir( @_ );
} elsif (@_) {
return File::Temp::tempdir( @_, 'DIR' => tmpdir() );
} else {
return File::Temp::tempdir( 'DIR' => tmpdir() );
}
}
sub _tempdir {
my $self = $_[0];
# Poor's man tempdir, File::Temp is only part of Perl 5.6.1
my ($tmpl, %args);
if (@_) {
($tmpl, %args) = @_;
$tmpl = tmpdir() . "/" . basename( $tmpl );
} else {
$tmpl = tmpdir() . "/tempdirXXXXXX";
}
# Try 10 times to create a directory
for (0..10) {
$tmpl =~ s/X/$chars[rand @chars]/ge;
if ( mkdir $tmpl, 0700 ) {
return $tmpl;
}
sleep 1;
}
croak "tempdir() somebody is trying to race with us!\n";
}
=pod
=head2 min()
my $least = min(@values);
my $least = min($a,$b,$c);
=head2 max()
my $greatest = max(@values);
my $greatest = max($a,$b,$c);
These find the smallest or largest value in a list of numbers. An
empty list will return undef. Undef values are ignored and will only
be returned if the list is empty or contains only undefined value.
=cut
sub min {
my $min = undef;
foreach my $x ( @_ ) {
next unless defined $x;
if ( defined $min ) {
$min = $x if $x < $min;
} else {
$min = $x;
}
}
return $min;
}
sub max {
my $max = undef;
foreach my $x ( @_ ) {
next unless defined $x;
if ( defined $max ) {
$max = $x if $x > $max;
} else {
$max = $x;
}
}
return $max;
}
=pod
=head2 ratio( $dividend, $divisor )
Returns $divivend / $divisor and returns "NaN" when $divisor is equals
to 0. It rounds the result to the second decimal.
=cut
sub ratio {
my ( $dividend, $divisor ) = @_;
if ( $divisor ) {
return sprintf "%.2f", $dividend / $divisor;
} else {
return "NaN";
}
}
=pod
=head2 ratio100( $part, $total )
Returns as a percentage $part on $total. This function is safe to use
when $total is equal to 0 (it will returns NaN). The percentage is
rounded to the first decimal.
=cut
sub ratio100 {
my ( $dividend, $divisor ) = @_;
if ( $divisor ) {
return sprintf "%.1f", ($dividend / $divisor) * 100;
} else {
return "NaN";
}
}
=pod
=head2 shell_quote($string)
Return $string in a format that make it safe to hand out to the shell
so that metacharacters are not interpreted by the shell. This is done
by returning $string wrapped in single quotes and escaping the single
quotes contained in the $string.
=cut
sub shell_quote {
my $string = $_[0];
if (defined $string ) {
# Make sure that there is no way to
# escape the shell single quotes
$string =~ s/'/'\\''/g
} else {
$string = '';
}
return "'" . $string . "'";
}
=pod
=head2 indent( $string, [ $count ] )
Return $string, indented by $count spaces. If $count is not specified, a
default of 2 will be assumed.
=cut
sub indent {
my ( $text, $step ) = @_;
return ''
unless $text;
$step = 2
unless defined $step;
my @lines = split( /\n/, $text );
my $indented = join( "\n", map( { ' ' x $step . $_ } @lines ) );
$indented .= "\n"
if $text =~ m/\n$/;
return $indented;
}
=pod
=head2 check_param( $param, $name, [ $regex | $coderef, $msg ] )
Check that param "$name" is not undefined, and that it optionally match the
given regexp. Validation can also be achieved through a code reference passed
as the third parameter. The convention in this case is that the subroutine
will return a boolean indicating whether $param is valid or not.
Examples:
check_param( $req_param, 'req_param' );
check_param( $param_string, 'param_string', qr/^[a-z]+$/ );
check_param( $integer, 'integer', qr/^[0-9]+$/, "not a valid integer" );
check_param( $bool, 'bool', sub { return $_[0] }, "boolean was false" );
=cut
sub check_param {
my ( $param, $name, $regex, $msg ) = @_;
my ( $package, $filename, $line ) = caller();
die "check_param needs at least 2 arguments: a parameter, the parameter\'s name at $filename:$line\n"
unless ( @_ >= 2 );
die "parameter 'name' is not a valid name: '$name' at $filename:$line\n"
unless ( $name =~ m/^[a-zA-Z0-9_]+$/ );
( $package, $filename, $line ) = caller( 1 );
die "missing '$name' parameter at $filename:$line\n"
unless defined $param;
if ( defined $regex ) {
if ( ref $regex eq 'CODE' ) {
die ( defined $msg
? "$msg: '$param' at $filename:$line\n"
: "'$name' parameter value ('$param') doesn't validate with subroutine at $filename:$line\n" )
unless ( $regex->( $param ) );
} else {
die ( defined $msg
? "$msg: '$param' at $filename:$line\n"
: "'$name' parameter doesn't match '$regex': '$param' at $filename:$line\n" )
if ( $param !~ $regex );
}
}
return;
}
=pod
=head2 check_param( $instance, $name, $class )
Check that param "$instance" is a valid of one or more classes specified in
the 'class' parameter. The latter being either a string or a reference to an
array containing one or more such strings.
Examples:
check_param( $object, 'object', 'Wawa::Class' );
check_param( $object, 'object', [ 'Wawa::Class', 'Other::Class' ] );
=cut
sub check_object_param {
my ( $instance, $name, $class ) = @_;
my ( $package, $filename, $line ) = caller();
die "check_object_param needs 3 arguments: an object, the object parameter's name, the object's class name at $filename:$line\n"
unless ( @_ == 3 );
die "parameter 'name' is not a valid name: '$name' at $filename:$line\n"
unless ( $name =~ m/^[a-zA-Z0-9_]+$/ );
$class = [ $class ]
unless ( ref $class eq 'ARRAY' );
die "parameter 'class' should contain at least one class name at $filename:$line\n"
unless ( @{$class} > 0 );
foreach my $cl ( @{$class} ) {
die "parameter 'class' contains an invalid class name: '$cl' at $filename:$line\n"
unless ( $cl =~ m/^[a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)*$/ );
}
( $package, $filename, $line ) = caller( 1 );
die "missing '$name' parameter at $filename:$line\n"
unless defined $instance;
foreach my $cl ( @{$class} ) {
return if UNIVERSAL::isa( $instance, $cl );
}
if ( @{$class} == 1 ) {
die "'$name' parameter should be a '$class->[0]' instance, not '$instance' at $filename:$line\n";
} else {
my $string = join "', '", @{$class}[0..@{$class}-2];
$string .= "' or '$class->[-1]" ;
die "'$name' parameter should be a '$string' instance, not '$instance' at $filename:$line\n";
}
}
=pod
=head2 sql_quote_name( $name )
=cut
sub sql_quote_name {
my $name = $_[0];
return ( $name =~ /[-:.]/ ) ? '"' . $name . '"' : $name;
}
=pod
=head2 tree_apply( $root, $children_func, $apply_func )
Preorder processsing
=cut
sub tree_apply {
my ( $root, $children_func, $apply_func ) = @_;
check_param( $root, 'root' );
check_param( $children_func, 'children_func' );
check_param( $apply_func, 'apply_func' );
my $results = [ $root->$apply_func() ];
my $children = $root->$children_func();
while ( @$children ) {
my $node = shift @$children;
push @$results, $node->$apply_func();
unshift @$children, @{$node->$children_func()};
}
return $results;
}
=pod
=head2 item_index( $array_ref, $item )
Returns the index of $item in $array. It returns undef if the item
isn't found. $array_ref should be an ARRAY reference and $item should
be a non-null scalar.
=cut
sub item_index {
my ( $array, $item ) = @_;
check_param( $array, 'array' );
croak "'array' should be an array ref, not '$array'"
unless ( ref $array eq 'ARRAY' );
check_param( $item, 'item' );
for ( my $i = 0; $i < @$array; $i++ ) {
return $i
if ( $array->[$i] eq $item );
}
return undef;
}
=pod
=head2 deep_copy( $object, [$exclusion] )
Makes a recursive copy of $object. Cyclic references are maintained
in the copy. Optionnally, an array ref of packages for which the
objects shouldn't be deeply copied can be provided.
=cut
sub deep_copy {
my ( $object, $exclusions ) = @_;
check_param( $object, 'object' );
croak "'exclusions' should be an array reference, not '$exclusions'"
if defined $exclusions && ref $exclusions ne 'ARRAY';
$exclusions ||= [];
return _deep_copy( $object, {}, $exclusions );
}
sub _deep_copy {
my ( $object, $seen, $exclusions ) = @_;
return $object unless ref $object;
return $seen->{$object} if exists $seen->{$object};
if ( ref $object eq 'SCALAR' || ref $object eq 'REF' ) {
return _clone_scalar_ref( $object, $seen, $exclusions );
} elsif ( ref $object eq 'ARRAY' ) {
return _clone_array_ref( $object, $seen, $exclusions );
} elsif ( ref $object eq 'HASH' ) {
return _clone_hash_ref( $object, $seen, $exclusions );
} elsif ( ref $object eq 'Regexp' ) {
# This is a read-only object
return $object;
} elsif ( UNIVERSAL::isa( $object, 'UNIVERSAL' ) ){ # blessed object
return _clone_object( $object, $seen, $exclusions );
} else {
croak "ref type unsupported by deep_copy(): $object";
}
}
sub _clone_scalar_ref {
my ( $scalar_ref, $seen_refs, $exclusions ) = @_;
my $scalar;
$seen_refs->{$scalar_ref} = \$scalar;
$scalar = _deep_copy( $$scalar_ref, $seen_refs, $exclusions );
return \$scalar;
}
sub _clone_array_ref {
my ( $array_ref, $seen_refs, $exclusions ) = @_;
my @array_copy = ();
$seen_refs->{$array_ref} = \@array_copy;
for my $elmnt ( @$array_ref ) {
push @array_copy, _deep_copy( $elmnt, $seen_refs, $exclusions );
}
return \@array_copy;
}
sub _clone_hash_ref {
my ( $hash_ref, $seen_refs, $exclusions ) = @_;
my %hash = ();
$seen_refs->{$hash_ref} = \%hash;
while ( my( $key, $value ) = each %$hash_ref ) {
$hash{$key} = _deep_copy( $value, $seen_refs, $exclusions );
}
return \%hash;
}
sub _clone_object {
my ( $object, $seen, $exclusions ) = @_;
foreach my $excl ( @$exclusions ) {
return $object if $object->isa( $excl );
}
if ( index( $object, '=SCALAR(' ) >= 0 ) {
return bless( _clone_scalar_ref( $object, $seen, $exclusions ),
ref $object);
} elsif ( index( $object, '=HASH(' ) >= 0 ) {
return bless( _clone_hash_ref( $object, $seen, $exclusions ),
ref $object);
} elsif ( index( $object, '=ARRAY(' ) >= 0 ) {
return bless( _clone_array_ref( $object, $seen, $exclusions ),
ref $object);
} else {
croak "unsupported object storage: '$object'";
}
}
# could better use a Perl interface to getnameinfo(3)
sub host_by_addr {
## perldoc -f gethostbyaddr
## # gethostbyaddr(3)
## # See also: logresolve, from e.g. the Debian apache-utils package
##
## # $? holds:
## #
## # HOST_NOT_FOUND
## # The specified host is unknown.
## #
## # NO_ADDRESS or NO_DATA
## # The requested name is valid but does not have an IP address.
## #
## # NO_RECOVERY
## # A non-recoverable name server error occurred.
## #
## # TRY_AGAIN
## # A temporary error occurred on an authoritative name server. Try
## # again later.
my $ip = $_[0] or die "usage: gethostbyaddr IPv4address\n";
my $iaddr = inet_aton($ip) or die "$ip isn't an IPv4 address\n";
my $name;
unless ( $name = gethostbyaddr( $iaddr, AF_INET ) ) {
warn "can't resolve $ip: $? ($!)\n";
$name = undef;
}
return $name;
}
=pod
=head2 unique( $list )
Returns an array reference with the duplicates elements of $list removed.
=cut
sub unique {
my $list = $_[0];
check_object_param( $list, 'list', 'ARRAY' );
my $unique = [];
my %cache = ();
foreach my $element ( @$list ) {
push @$unique, $element
unless exists $cache{$element};
$cache{$element} = 1;
}
return $unique;
}
=pod
=head2 text_for_width( $text, $width )
Returns a stripped-down representation of 'text', ensuring its length is
shorter or equal to 'width'. If the original text already fits the given
width, it is returned unchanged, otherwise, it is shortened and '...' is put
in the middle to indicate the cut.
=cut
sub text_for_width {
my ( $text, $width ) = @_;
check_param( $text, 'text' );
check_param( $width, 'width',
sub { return ( $_[0] =~ m/^[0-9]+$/ && $_[0] >= 5 ) },
"'width' should be a positive, greater than or equal to 5, integer" );
my $length = length( $text );
return $text if $length <= $width;
my $segment = ( $width - 3 ) / 2;
my $start = substr( $text, 0, $segment );
$start =~ s/\s*$//g;
my $end = substr( $text, $length - $segment );
$end =~ s/^\s*//g;
return $start . '...' . $end;
}
=pod
=head2 is_url( $string )
Determines whether $string is a url or not, returning 'true' or 'false' as
result code.
=cut
sub is_url {
my $string = $_[0];
check_param( $string, 'string' );
return $string =~ m@^[a-zA-Z0-9]+://@ || 0;
}
=pod
=head2 parse_url( $string )
Returns an hash reference containing keys for the following URL parts:
'scheme', 'host', 'port', 'path', 'query', 'fragment'. The value will
be empty if this wasn't present in the URL. This function is somewhat
'http' biased and one should use the URI module for full blown URI
parsing.
The function dies if the URL cannot be parsed.
=cut
sub parse_url {
check_param( $_[0], 'url' );
# e.g.
# http://ds.internic.net/instructions/overview.html#WARNING
my ( $scheme, $authority, $path, $query, $fragment) = (
$_[0] =~ m/^
(?:
([^:\/?\#]+): # scheme
)?
(?:
\/\/([^\/?\#]*) # authority
)?
([^?\#]*) # path
(?:
\?([^\#]*) # query
)?
(?:
\#(.*) # fragment
)?$/x
) or die "invalid url: '$_[0]'";
# fullblown
#
# authority = server | reg_name
# server = [ [ userinfo "@" ] hostport ]
# hostport = host [ ":" port ]
# host = hostname | IPv4address
#
# is not yet supported
my ( $host, $port );
( $host, $port ) = $authority =~ m/^([-\.a-zA-Z0-9]+)(?::(\d+))?$/
if defined $authority;
return {
'scheme' => $scheme,
'host' => $host,
'port' => $port,
'path' => $path,
'query' => $query,
'fragment' => $fragment
};
}
=pod
=head2 file_content( $filename )
Returns the content of $filename. Dies if an error occurs.
=cut
require Lire::Error;
sub file_content {
my $file = $_[0];
check_param( $file, 'filename' );
open my $fh, $file
or croak( Lire::Error::file_not_readable( $file ) );
local $/ = undef;
my $content = <$fh>;
close $fh;
return $content;
}
=pod
=head2 create_file( $filename, [$content], [$utf8_encoding] )
Creates file $filename with $content. The $utf8_encoding flag specifies
whether you want to keep strings in their UTF-8 encoding with versions of Perl
that supports it (5.8.0 and above).
=cut
require Lire::I18N;
sub create_file {
my ( $filename, $content, $utf8_encoding ) = @_;
check_param( $filename, 'filename' );
$content = ""
unless defined $content;
open( my $fh, '>', $filename )
or croak "error creating '$filename': $!";
Lire::I18N::set_fh_encoding( $fh, 'utf-8' )
if $utf8_encoding;
print $fh $content;
close $fh;
return;
}
=pod
=head2 period_range( $period, $time )
Returns an array reference containing the starting and ending
boundaries for the $period that includes $time. $period should be
one of 'hourly', 'daily', 'weekly', 'monthly' or 'yearly'. $time should
be in seconds since epoch.
=cut
require Lire::WeekCalculator;
sub period_range {
my ( $period, $time ) = @_;
check_param( $period, 'period', qr/^(hourly|daily|weekly|monthly|yearly)$/,
"'period' parameter should be one of 'hourly', 'daily', 'weekly', 'monthly' or 'yearly'" );
check_param( $time, 'time', qr/^\d+$/, "'time' paremeter should be seconds since epoch" );
my ( $hour, $day, $month, $year ) = (localtime( $time || time() ))[2..5];
if ( $period eq 'hourly' ) {
my $start = timelocal( 0, 0, $hour, $day, $month, $year );
return [ $start, $start + 3600 ];
} elsif ( $period eq 'daily' ) {
my $start = timelocal( 0, 0, 0, $day, $month, $year );
return [ $start, $start + 3600*24 ];
} elsif ( $period eq 'weekly' ) {
my $calc = new Lire::WeekCalculator();
my $week_no = $calc->week_number( $time );
my $start = $calc->week_start( $year, $week_no );
return [ $start, $start + 86400*7 ];
} elsif ( $period eq 'monthly' ) {
my $start = timelocal( 0, 0, 0, 1, $month, $year );
if ( $month == 11 ) {
return [ $start, timelocal( 0, 0, 0, 1, 0, $year+1 ) ]
} else {
return [ $start, timelocal( 0, 0, 0, 1, $month + 1, $year ) ]
}
} elsif ( $period eq 'yearly' ) {
return [ timelocal( 0, 0, 0, 1, 0, $year ),
timelocal( 0, 0, 0, 1, 0, $year+1 ) ]
}
}
# keep perl happy
1;
__END__
=pod
=head1 AUTHORS
Francis J. Lacoste <flacoste@logreport.org>
Joost van Baal <joostvb@logreport.org>
Wessel Dankers <wsl@logreport.org>
Wolfgang Sourdeau <wolfgang@logreport.org>
=head1 VERSION
$Id: Utils.pm,v 1.67 2006/07/23 13:16:30 vanbaal Exp $
=head1 COPYRIGHT
Copyright (C) 2001, 2002, 2004 Stichting LogReport Foundation
LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html.
=cut
|