line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Turtle::Writer; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Turtle::Writer::VERSION = '0.004'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
#ABSTRACT: Write RDF/Turtle documents without non-core package dependencies |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
24641
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
111
|
|
12
|
1
|
|
|
1
|
|
6
|
use Scalar::Util qw(reftype); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
113
|
|
13
|
1
|
|
|
1
|
|
4
|
use base 'Exporter'; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
1001
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @EXPORT = qw(turtle_literal turtle_literal_list turtle_statement turtle_uri); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub turtle_statement { |
19
|
6
|
|
|
6
|
1
|
752
|
my ($subject, %statements) = @_; |
20
|
|
|
|
|
|
|
|
21
|
10
|
|
|
|
|
24
|
my @s = grep { defined $_ } map { |
|
10
|
|
|
|
|
16
|
|
22
|
6
|
|
|
|
|
12
|
my ($p,$o) = ($_,$statements{$_}); |
23
|
10
|
100
|
|
|
|
21
|
if ( ref($o) ) { |
24
|
4
|
100
|
|
|
|
15
|
if (reftype($o) eq 'HASH') { |
25
|
3
|
|
|
|
|
5
|
$o = [ map { turtle_literal($o->{$_},$_) } keys %$o ]; |
|
3
|
|
|
|
|
8
|
|
26
|
|
|
|
|
|
|
} |
27
|
4
|
50
|
|
|
|
20
|
if (reftype($o) eq 'ARRAY') { |
28
|
4
|
50
|
|
|
|
31
|
$o = join(", ", @$o) if ref($o); |
29
|
|
|
|
|
|
|
} else { |
30
|
0
|
|
|
|
|
0
|
$o = undef; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
10
|
100
|
100
|
|
|
49
|
(defined $o and $o ne '') ? "$p $o" : undef; |
34
|
|
|
|
|
|
|
} keys %statements; |
35
|
|
|
|
|
|
|
|
36
|
6
|
100
|
|
|
|
23
|
return "" unless @s; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
5
|
my $ttl = join(" ;\n" , shift @s, map { " $_" } @s); |
|
3
|
|
|
|
|
7
|
|
39
|
3
|
100
|
|
|
|
6
|
if (defined $subject) { |
40
|
2
|
|
|
|
|
9
|
return "$subject $ttl .\n"; |
41
|
|
|
|
|
|
|
} else { |
42
|
1
|
|
|
|
|
4
|
return "[ $ttl ] .\n"; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub turtle_literal { |
48
|
40
|
|
|
40
|
1
|
1696
|
my $value = shift; |
49
|
40
|
|
|
|
|
38
|
my %opt; |
50
|
|
|
|
|
|
|
|
51
|
40
|
100
|
66
|
|
|
112
|
if ( ref( $value ) and ref($value) eq 'ARRAY') { |
52
|
9
|
|
|
|
|
23
|
return join( ", ", map { turtle_literal( $_, @_ ) } @$value ); |
|
17
|
|
|
|
|
35
|
|
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
31
|
100
|
|
|
|
61
|
if ( @_ % 2 ) { |
56
|
10
|
|
|
|
|
12
|
my $v = shift; |
57
|
10
|
100
|
|
|
|
54
|
%opt = ($v =~ /^[a-zA-Z0-9-]+$/) ? ( lang => $v ) : ( type => $v ); |
58
|
|
|
|
|
|
|
} else { |
59
|
|
|
|
|
|
|
|
60
|
21
|
|
|
|
|
30
|
%opt = @_; |
61
|
21
|
50
|
66
|
|
|
62
|
croak "Literal values cannot have both language and datatype" |
62
|
|
|
|
|
|
|
if ($opt{lang} and $opt{type}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
31
|
100
|
100
|
|
|
136
|
return "" if not defined $value or $value eq ''; |
66
|
|
|
|
|
|
|
|
67
|
26
|
|
|
|
|
87
|
my %ESCAPED = ( "\t" => 't', "\n" => 'n', |
68
|
|
|
|
|
|
|
"\r" => 'r', "\"" => '"', "\\" => '\\' ); |
69
|
26
|
|
|
|
|
74
|
$value =~ s/([\t\n\r\"\\])/\\$ESCAPED{$1}/sg; |
70
|
|
|
|
|
|
|
|
71
|
26
|
|
|
|
|
43
|
$value = qq("$value"); |
72
|
|
|
|
|
|
|
|
73
|
26
|
100
|
|
|
|
58
|
if ($opt{lang}) { |
|
|
100
|
|
|
|
|
|
74
|
8
|
|
|
|
|
52
|
return $value.'@'.$opt{lang}; |
75
|
|
|
|
|
|
|
} elsif ($opt{type}) { |
76
|
5
|
|
|
|
|
33
|
return $value.'^^<'.$opt{type} .'>'; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
13
|
|
|
|
|
61
|
return $value; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub turtle_literal_list { |
84
|
10
|
100
|
100
|
10
|
1
|
57
|
if ( ref($_[0]) and ref($_[0]) eq 'HASH') { |
|
|
100
|
|
|
|
|
|
85
|
3
|
|
|
|
|
4
|
my $hash = $_[0]; |
86
|
2
|
|
|
|
|
5
|
return join( ", ", |
87
|
3
|
|
|
|
|
14
|
map { turtle_literal( $hash->{$_}, lang => $_ ) } |
88
|
|
|
|
|
|
|
keys %$hash |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
} elsif ( @_ > 1 ) { |
91
|
1
|
|
|
|
|
5
|
return turtle_literal( \@_ ); |
92
|
|
|
|
|
|
|
} else { |
93
|
6
|
|
|
|
|
12
|
return turtle_literal( $_[0] ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub turtle_uri { |
99
|
0
|
|
|
0
|
1
|
|
my $value = shift; |
100
|
0
|
0
|
|
|
|
|
return "" unless defined $value; |
101
|
|
|
|
|
|
|
# my $value = URI->new( encode_utf8( $value ) )->canonical; |
102
|
0
|
|
|
|
|
|
return "<$value>"; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__END__ |