File Coverage

blib/lib/POE/Component/IKC/Specifier.pm
Criterion Covered Total %
statement 22 43 51.1
branch 9 30 30.0
condition 6 9 66.6
subroutine 4 7 57.1
pod 2 4 50.0
total 43 93 46.2


line stmt bran cond sub pod time code
1             package POE::Component::IKC::Specifier;
2              
3             ############################################################
4             # $Id: Specifier.pm 1247 2014-07-07 09:06:34Z fil $
5             #
6             # Copyright 1999-2014 Philip Gwyn. All rights reserved.
7             # This program is free software; you can redistribute it and/or modify
8             # it under the same terms as Perl itself.
9             #
10             # Contributed portions of IKC may be copyright by their respective
11             # contributors.
12              
13 3     3   22012 use strict;
  3         5  
  3         128  
14 3     3   18 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  3         6  
  3         209  
15              
16 3     3   15 use Carp;
  3         6  
  3         1990  
17              
18             require Exporter;
19             @ISA = qw(Exporter);
20             @EXPORT = qw( specifier_parse specifier_name specifier_part);
21             $VERSION = '0.2402';
22              
23 0     0 0 0 sub DEBUG { 0 }
24              
25             #----------------------------------------------------
26             # Turn an specifier into a hash ref
27             sub specifier_parse ($)
28             {
29 18     18 1 10648 my($specifier)=@_;
30 18 50       40 return if not $specifier;
31 18         23 my $kernelRE = q((?:\*) |
32             (?:[-. \w]+) |
33             (?:[a-zA-Z0-9][-.a-zA-Z0-9]+:\d+) |
34             (?:unix:[-.\w]+(?::\d+-\d+)?)
35             );
36 18 50       30 unless(ref $specifier) {
    0          
37 18 100       2101 if($specifier=~m(^poe:
    50          
38             (?:
39             (//)
40             ($kernelRE)?
41             )?
42             (?:
43             (/)
44             ([- \w]+)
45             )?
46             (?:
47             (/)?
48             ([- \w]*)
49             )?
50             (?: \x3f
51             (\w+)
52             )?
53             $)x) {
54 10         44 $specifier={kernel=>$2, session=>$4, state=>$6};
55 10 100       31 $specifier->{args}=$7 if $7;
56             }
57             elsif( $specifier =~ m(^ (?:(?://)($kernelRE)/)?
58             (?:([- \w]+)/)?
59             (?:([- \w]+))?
60             (?: \x3f (\w+) )?
61             $)x ) {
62 8         44 $specifier = { kernel=>$1, session=>$2, state=>$3 };
63 8 100       33 $specifier->{args} = $4 if $4;
64             }
65             else {
66 0         0 return;
67             }
68             }
69             elsif('HASH' ne ref $specifier) {
70             # carp "Why is specifier a ", ref $specifier;
71 0         0 return;
72             }
73              
74 18   100     70 $specifier->{kernel}||='';
75 18   100     40 $specifier->{session}||='';
76 18   100     34 $specifier->{state}||='';
77 18         38 return $specifier;
78             }
79              
80             sub specifier_part ($$)
81             {
82 0     0 0   my($specifier, $part)=@_;
83 0 0         return if not $specifier;
84              
85 0 0 0       $specifier="poe://$specifier" unless ref $specifier or $specifier=~/^poe:/;
86 0           $specifier=specifier_parse $specifier;
87 0 0         return if not $specifier;
88 0           return $specifier->{$part};
89             }
90              
91             #----------------------------------------------------
92             # Turn an specifier into a string
93             sub specifier_name ($)
94             {
95 0     0 1   my($specifier)=@_;
96 0 0         return $specifier unless(ref $specifier);
97 0 0         if(ref($specifier) eq 'ARRAY')
98             {
99 0           $specifier={kernel=>'',
100             session=>$specifier->[0],
101             state=>$specifier->[1],
102             };
103             }
104              
105 0           my $name='poe:';
106 0 0         if($specifier->{kernel})
107             {
108 0           $name.='//';
109 0           $name.=$specifier->{kernel};
110             }
111 0 0         if($specifier->{session})
112             {
113 0           $name.='/'.$specifier->{session};
114             }
115 0 0         $name.="/$specifier->{state}" if $specifier->{state};
116 0           return $name;
117             }
118              
119              
120             1;
121              
122             __END__