| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #=============================================================================== | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | #         FILE:  Misc.pm | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | #  DESCRIPTION: | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | #        NOTES:  --- | 
| 8 |  |  |  |  |  |  | #       AUTHOR:  Michael Bochkaryov (Rattler), | 
| 9 |  |  |  |  |  |  | #      COMPANY:  Net.Style | 
| 10 |  |  |  |  |  |  | #      VERSION:  1.044 | 
| 11 |  |  |  |  |  |  | #      CREATED:  17.08.2008 17:01:48 EEST | 
| 12 |  |  |  |  |  |  | #=============================================================================== | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 NAME | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | NetSDS::Util::Misc - miscelaneous utilities | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | use NetSDS::Util::Misc; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | C module contains miscelaneous functions. | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =cut | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | package NetSDS::Util::Misc; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 2 |  |  | 2 |  | 4703 | use 5.8.0; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 96 |  | 
| 31 | 2 |  |  | 2 |  | 9 | use warnings 'all'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 79 |  | 
| 32 | 2 |  |  | 2 |  | 9 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 63 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 2 |  |  | 2 |  | 8 | use base 'Exporter'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 166 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 2 |  |  | 2 |  | 10 | use version; our $VERSION = '1.044'; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | our @EXPORT = qw( | 
| 39 |  |  |  |  |  |  | cmp_version | 
| 40 |  |  |  |  |  |  | usage | 
| 41 |  |  |  |  |  |  | get_cli | 
| 42 |  |  |  |  |  |  | make_uuid | 
| 43 |  |  |  |  |  |  | csv_num | 
| 44 |  |  |  |  |  |  | format_msisdn | 
| 45 |  |  |  |  |  |  | ); | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 2 |  |  | 2 |  | 1610 | use Getopt::Long; | 
|  | 2 |  |  |  |  | 18989 |  | 
|  | 2 |  |  |  |  | 12 |  | 
| 48 | 2 |  |  | 2 |  | 1636 | use Pod::Usage; | 
|  | 2 |  |  |  |  | 71364 |  | 
|  | 2 |  |  |  |  | 306 |  | 
| 49 | 2 |  |  | 2 |  | 1159 | use Data::UUID; | 
|  | 2 |  |  |  |  | 1268 |  | 
|  | 2 |  |  |  |  | 1104 |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | #*********************************************************************** | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =head1 EXPORTED FUNCTIONS | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =over | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =item B - compare versions | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | Funcion comapres two version strings. | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =cut | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 64 |  |  |  |  |  |  | sub cmp_version { | 
| 65 | 0 |  |  | 0 | 1 |  | my ( $ver1, $ver2 ) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | return sprintf( "%03d.%03d", split( m/\./, $ver1 ) ) cmp sprintf( "%03d.%03d", split( m/\./, $ver2 ) ); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | #*********************************************************************** | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =item B - print C text | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | This function is wapper to L module printing POD to STDERR. | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =cut | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 79 |  |  |  |  |  |  | sub usage { | 
| 80 | 0 |  |  | 0 | 1 |  | pod2usage( | 
| 81 |  |  |  |  |  |  | -message => sprintf( shift(@_), @_ ), | 
| 82 |  |  |  |  |  |  | -verbose => 0, | 
| 83 |  |  |  |  |  |  | -exitval => 2, | 
| 84 |  |  |  |  |  |  | -output  => \*STDERR | 
| 85 |  |  |  |  |  |  | ); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | #*********************************************************************** | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =item B - get CLI parameters | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Return command line arguments | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =cut | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 97 |  |  |  |  |  |  | sub get_cli { | 
| 98 | 0 |  |  | 0 | 1 |  | my ( $res, @opa ) = @_; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | my $ret  = undef; | 
| 101 | 0 |  |  |  |  |  | my @argv = @ARGV;    # save @ARGV | 
| 102 |  |  |  |  |  |  | { | 
| 103 |  |  |  |  |  |  | # Switch off warnings because of other CLI parameters | 
| 104 |  |  |  |  |  |  | # still not known | 
| 105 | 0 |  |  |  |  |  | my $warn = $SIG{__WARN__}; | 
|  | 0 |  |  |  |  |  |  | 
| 106 | 0 |  |  | 0 |  |  | $SIG{__WARN__} = sub { }; | 
|  | 0 |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | $ret = GetOptions( $res, @opa, 'help|h|?', 'man|m' ); | 
| 108 | 0 |  |  |  |  |  | $SIG{__WARN__} = $warn; | 
| 109 |  |  |  |  |  |  | } | 
| 110 | 0 |  |  |  |  |  | @ARGV = @argv;       # restore @ARGV | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | # GetOptions bug workaround | 
| 113 |  |  |  |  |  |  | #	if ( !$ret ) { | 
| 114 |  |  |  |  |  |  | #		pod2usage( -verbose => 0, -exitval => 2, -output => \*STDERR ); | 
| 115 |  |  |  |  |  |  | #	} elsif ( exists( $res->{help} ) and $res->{help} ) { | 
| 116 | 0 | 0 | 0 |  |  |  | if ( exists( $res->{help} ) and $res->{help} ) { | 
|  |  | 0 | 0 |  |  |  |  | 
| 117 | 0 |  |  |  |  |  | pod2usage( -verbose => 1, -exitval => 2, -output => \*STDERR ); | 
| 118 |  |  |  |  |  |  | } elsif ( exists( $res->{man} ) and $res->{man} ) { | 
| 119 | 0 |  |  |  |  |  | pod2usage( -verbose => 2, -exitval => 2, -output => \*STDERR ); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  |  | return $res; | 
| 123 |  |  |  |  |  |  | } ## end sub get_cli | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | #*********************************************************************** | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =item B - make UUD string | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | Create upper case UUID string. | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =cut | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 134 |  |  |  |  |  |  | sub make_uuid { | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 0 |  |  | 0 | 1 |  | return Data::UUID->new()->create_str(); | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | #*********************************************************************** | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =item B - format number for CSV | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | Paramters: numeric value | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Returns: CSV formatted | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =cut | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub csv_num { | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 0 |  |  | 0 | 1 |  | my ($num) = @_; | 
| 153 | 0 |  |  |  |  |  | $num =~ s/\./,/g; | 
| 154 | 0 |  |  |  |  |  | $num = "\"$num\""; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 |  |  |  |  |  | return $num; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | #*********************************************************************** | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =item B - format MSISDN | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Paramters: phone number | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | Returns: well formed MSISDN without leading +. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =cut | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | sub format_msisdn { | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 0 |  |  | 0 | 1 |  | my ($msisdn) = @_; | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 0 |  |  |  |  |  | $msisdn =~ s/[\-\(\)\.\s]//g; | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 0 | 0 |  |  |  |  | if ( $msisdn =~ /^\+?(\d{12})$/ ) { | 
|  |  | 0 |  |  |  |  |  | 
| 178 | 0 |  |  |  |  |  | return $1; | 
| 179 |  |  |  |  |  |  | } elsif ( $msisdn =~ /^\s*(\d{9,12})\s*$/ ) { | 
| 180 | 0 |  |  |  |  |  | return "380" . substr( $msisdn, length($1) - 9, 9 ); | 
| 181 |  |  |  |  |  |  | } else { | 
| 182 | 0 |  |  |  |  |  | return undef; | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | #************************************************************************** | 
| 188 |  |  |  |  |  |  | 1; | 
| 189 |  |  |  |  |  |  | __END__ |