This file is indexed.

/usr/share/perl5/RDF/TrineX/Functions.pm is in librdf-trinex-functions-perl 0.005-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
package RDF::TrineX::Functions;

use 5.010;
use strict qw< vars subs >;
no warnings;
use utf8;

BEGIN {
	$RDF::TrineX::Functions::AUTHORITY = 'cpan:TOBYINK';
	$RDF::TrineX::Functions::VERSION   = '0.005';
}

use Carp qw< croak >;
use IO::Detect 0.003 qw< is_filehandle is_filename is_fileuri >;
use PerlX::Maybe qw< maybe >;
use RDF::NS::Trine;
use RDF::Trine qw< store >;
use RDF::Trine::Namespace qw< rdf rdfs owl xsd >;
use Scalar::Util qw< blessed >;
use URI::file;

use Sub::Exporter -setup => {
	exports => [
		qw< curie iri blank variable literal statement store model parse >,
		serialize => \&_build_serializer,
	],
	groups => {
		nodes     => [qw< curie iri blank literal variable >],
		shortcuts => [
			parse     => { -as => 'rdf_parse' },
			serialize => { -as => 'rdf_string' },
		],
		shortcuts_nodes => [
			parse     => { -as => 'rdf_parse' },
			serialize => { -as => 'rdf_string' },
			iri       => { -as => 'rdf_resource' },
			blank     => { -as => 'rdf_blank' },
			literal   => { -as => 'rdf_literal' },
			variable  => { -as => 'rdf_variable' },
			statement => { -as => 'rdf_statement' },
		],
	},
};

foreach my $nodetype (qw< iri blank variable literal >)
{
	my $orig = $nodetype eq 'iri'
		? sub { RDF::Trine::Node::Resource->new(@_) }
		: RDF::Trine->can($nodetype);
		
	my $sub;
	$sub = sub
	{
		shift if blessed $_[0] && $_[0]->isa(__PACKAGE__);
		return $sub->(@{$_[0]}) if ref $_[0] eq 'ARRAY';
	
		my $node = shift;
		
		if (blessed($node) and $node->isa('RDF::Trine::Node'))
		{
			return $node;
		}
		
		if ($nodetype eq 'iri' and blessed($node) and $node->isa('URI'))
		{
			return $orig->("$node", @_);
		}
		elsif ($nodetype eq 'literal' and blessed($node) and $node->isa('URI'))
		{
			$_[1] //= $xsd->anyURI unless $_[0];
			return $orig->("$node", @_);
		}
		
		if ($nodetype =~ m[^(iri|blank)$] and $node =~ /^_:(.+)$/)
		{
			return RDF::Trine::blank($1, @_);
		}
		
		if ($nodetype =~ m[^(variable|blank)$] and $node =~ /^\?(.+)$/)
		{
			return RDF::Trine::variable($1, @_);
		}
		
		$orig->("$node", @_);
	};
	
	*$nodetype = $sub;
}

sub new
{
	my ($class, @args) = @_;
	bless \@args, $class;
}

sub curie
{
	shift if blessed $_[0] && $_[0]->isa(__PACKAGE__);
	return curie(@{$_[0]}) if ref $_[0] eq 'ARRAY';
	
	my $node = shift;
	
	if (blessed($node) and $node->isa('RDF::Trine::Node'))
	{
		return $node;
	}
	
	if (blessed($node) and $node->isa('URI'))
	{
		return RDF::Trine::iri("$node", @_);
	}
	
	if ($node =~ /^_:(.+)$/)
	{
		return RDF::Trine::blank($1, @_);
	}
	
	state $ns = RDF::NS::Trine->new('any');
	$ns->URI($node);
}

sub statement
{
	shift if blessed $_[0] && $_[0]->isa(__PACKAGE__);
	return statement(@{$_[0]}) if ref $_[0] eq 'ARRAY';
	
	my (@nodes) = map {
		if (blessed($_) and $_->isa('RDF::Trine::Node'))  { $_ }
		elsif (blessed($_) and $_->isa('URI'))            { iri($_) }
		else                                              { literal($_) }
	} @_;
	
	(@nodes==4)
		? RDF::Trine::Statement::Quad->new(@nodes)
		: RDF::Trine::Statement->new(@nodes)
}

sub model
{
	shift if blessed $_[0] && $_[0]->isa(__PACKAGE__);
	return model(@{$_[0]}) if ref $_[0] eq 'ARRAY';
	
	my $store = shift;
	return $store if blessed($store) && $store->isa('RDF::Trine::Model');
	
	$store
		? RDF::Trine::Model->new($store)
		: RDF::Trine::Model->new()
}

sub parse
{
	shift if blessed $_[0] && $_[0]->isa(__PACKAGE__);
	return parse(@{$_[0]}) if ref $_[0] eq 'ARRAY';
	
	my ($thing, %opts) = @_;
	
	my $model  = delete($opts{into})   // delete($opts{model});
	my $base   = delete($opts{base});
	my $parser = delete($opts{parser}) // delete($opts{type})  // delete($opts{as}) // delete($opts{using});
	my $graph  = delete($opts{graph})  // delete($opts{context});

	# Normalise $graph.
	#
	$graph = iri($graph) if defined $graph;

	if (blessed($thing) && $thing->isa('RDF::Trine::Store')
	or  blessed($thing) && $thing->isa('RDF::Trine::Model'))
	{
		return model($thing) unless $model;
		
		$thing->as_stream->each(sub {
			my ($s, $p, $o) = shift->nodes;
			$model->add_statement(
				$graph
					? statement($s, $p, $o, $graph)
					: statement($s, $p, $o)
			);
		});
		return $model;
	}
	
	$model //= model();
	return $model unless defined $thing;
	
	# Normalise $parser. It should be a class name or blessed object.
	# If undef, then 'RDF::Trine::Parser' class.
	# If media type then, figure out correct parser.
	# If format name then, figure out correct parser.
	#
	if (not $parser)
		{ $parser = 'RDF::Trine::Parser' }
	elsif (not blessed $parser and $parser =~ m{/} and $parser !~ m{^RDF/}i)
		{ $parser = RDF::Trine::Parser->parser_by_media_type($parser)->new }
	elsif (not blessed $parser)
		{ $parser = RDF::Trine::Parser->new($parser) }	

	# Normalise $base. Accept RDF::Trine::Nodes.
	#
	if (blessed $base and $base->isa('RDF::Trine::Node::Resource'))
	{
		$base = $base->uri;
	}	

	# Deal with $thing being a URI.
	# "file://" is explicitly not handled here.
	#
	if (blessed($thing) && $thing->isa('URI')
	or  blessed($thing) && $thing->isa('RDF::Trine::Node::Resource') && ($thing = $thing->uri)
	or !blessed($thing) && $thing =~ m{^(https?|ftp|file|data):\S+$})
	{
		if (is_fileuri $thing)
		{
			# Convert to a local path, and allow to fall through...
			URI->new("$thing")->file;
		}
		elsif (not ref $parser and $parser eq 'RDF::Trine::Parser')
		{
			RDF::Trine::Parser->parse_url_into_model(
				"$thing",
				$model,
				maybe context => $graph,
			);
			return $model;
		}
		else
		{
			# UA string consistent with RDF::Trine::Parser
			my $ua   = LWP::UserAgent->new(agent => "RDF::Trine/$RDF::Trine::VERSION");
			my $resp = $ua->get("$thing");
			$parser->parse_into_model(
				("$base"//"$thing"),
				$resp->decoded_content,
				$model,
				maybe context => $graph,
			);
			return $model;
		}
	}
	
	# Deal with $thing being a filename.
	#
	if (is_filename $thing)
	{
		$base //= URI::file->new_abs("$thing");
		$parser->parse_file_into_model(
			"$base",
			"$thing",
			$model,
			maybe context => $graph,
		);
		return $model;
	}
	
	croak "No base URI provided" unless $base;
	
	# Deal with $thing being a filehandle (or something similar).
	#
	if (is_filehandle $thing)
	{
		$parser->parse_file_into_model(
			"$base",
			$thing,
			$model,
			maybe context => $graph,
		);
		return $model;
	}
	
	croak "No parser provided for parsing" unless blessed $parser;
	$parser->parse_into_model(
		"$base",
		$thing,
		$model,
		maybe context => $graph,
	);
	
	return $model;
}

sub _build_serializer
{
	my ($class, $name, $arg) = @_;
	
	my $sub;
	$sub = sub
	{
		shift if blessed $_[0] && $_[0]->isa(__PACKAGE__);
		return $sub->(@{$_[0]}) if ref $_[0] eq 'ARRAY';
		
		my ($data, %opts) = do {
			(@_==2)
				? ($_[0], as => $_[1])
				: @_
		};
		
		my $ser = delete($opts{serializer})
			// delete($opts{type})
			// delete($opts{as})
			// delete($opts{using})
			// $arg->{type}
			// $arg->{-type}
			// 'Turtle';
		
		my $file = delete($opts{to})
			// delete($opts{file})
			// delete($opts{output});
		
		if (not blessed $ser)
		{
			$ser = RDF::Trine::Serializer->new($ser, %opts);
		}
		
		if (blessed $data and $data->isa('RDF::Trine::Iterator'))
		{
			return defined($file)
				? $ser->serialize_iterator_to_file($file, $data)
				: $ser->serialize_iterator_to_string($data);
		}
		
		return defined($file)
			? $ser->serialize_model_to_file($file, $data)
			: $ser->serialize_model_to_string($data);
	}
}

*serialize = __PACKAGE__->_build_serializer(serialize => {});

__PACKAGE__
__END__

=head1 NAME

RDF::TrineX::Functions - some shortcut functions for RDF::Trine's object-oriented interface

=head1 SYNOPSIS

  use RDF::TrineX::Functions -all;
  
  my $model = model();
  parse('/tmp/mydata.rdf', into => $model);
  
  $model->add_statement(statement(
      iri('http://example.com/'),
      iri('http://purl.org/dc/terms/title'),
      "An Example",
  ));
  
  print RDF::Trine::Serializer
      -> new('Turtle')
      -> serialize_model_to_string($model);

=head1 DESCRIPTION

This is a replacement for the venerable RDF::TrineShortcuts. Not a
drop-in replacement. It has fewer features, fewer dependencies,
less hackishness, less magic and fewer places it can go wrong.

It uses Sub::Exporter, which allows exported functions to be renamed
easily:

  use RDF::TrineX::Functions
    parse => { -as => 'parse_rdf' };

=head2 Functions

=over

=item C<iri>, C<literal>, C<blank>, C<variable>

As per the similarly named functions exported by L<RDF::Trine> itself.

These are wrapped with a very tiny bit of DWIMmery. A blessed L<URI>
object passed to C<iri> will be handled properly; a blessed URI
object passed to C<literal> will default the datatype to xsd:anyURI.
A string starting with "_:" passed to either C<iri> or C<blank> will
correctly create a blank node. A string starting with "?" passed to
either C<blank> or C<variable> will correctly create a variable. If
any of them are passed an existing RDF::Trine::Node, it will be
passed through untouched.

Other than that, no magic.

=item C<< curie >>

Like C<iri> but passes strings through L<RDF::NS::Trine>.

=item C<< statement(@nodes) >>

As per the similarly named function exported by L<RDF::Trine> itself.

Again, a tiny bit of DWIMmery: blessed URI objects are passed through
C<iri> and unblessed scalars (i.e. strings) are assumed to be literals.

=item C<store>

As per the similarly named function exported by L<RDF::Trine> itself.

=item C<model>

Returns a new RDF::Trine::Model. May be passed a store as a parameter.

=item C<< parse($source, %options) >>

Parses the source and returns an RDF::Trine::Model. The source may be:

=over

=item * a URI

A string URI, blessed URI object or RDF::Trine::Node::Resource, which
will be retrieved and parsed.

=item * a file

A filehandle, L<Path::Class::File>, L<IO::All>, L<IO::Handle> object,
or the name of an existing file (i.e. a scalar string). The file will
be read and parsed.

Except in the case of L<Path::Class::File>, L<IO::All> and strings,
you need to tell the C<parse> function what parser to use, and what
base URI to use.

=item * a string

You need to tell the C<parse> function what parser to use, and what
base URI to use.

=item * a model or store

An existing model or store, which will just be returned as-is.

=item * undef

Returns an empty model.

=back

The C<parser> option can be used to provide a blessed L<RDF::Trine::Parser>
object to use; the C<type> option can be used instead to provide a media
type hint. The C<base> option provides the base URI. The C<model> option
can be used to tell this function to parse into an existing model rather
than returning a new one. The C<graph> option may be used to provide a graph
URI.

C<into> is an alias for C<model>; C<type>, C<using> and C<as> are
aliases for C<parser>; C<context> is an alias for C<graph>.

Examples:

  my $model = parse('/tmp/data.ttl', as => 'Turtle');

  my $data   = iri('http://example.com/data.nt');
  my $parser = RDF::Trine::Parser::NTriples->new;
  my $model  = model();
  
  parse($data, using => $parser, into => $model);

=item C<< serialize($data, %options) >>

Serializes the data (which can be an RDF::Trine::Model or an
RDF::Trine::Iterator) and returns it as a string.

The C<serializer> option can be used to provide a blessed
L<RDF::Trine::Serializer> object to use; the C<type> option can be used
instead to provide a type hint. The C<output> option can be used to
provide a filehandle, IO::All, Path::Class::File or file name to
write to instead of returning the results as a string.

C<to> and C<file> are aliases for C<output>; C<type>, C<using> and C<as>
are aliases for C<serializer>.

Examples:

  print serialize($model, as => 'Turtle');

  my $file = Path::Class::File->new('/tmp/data.nt');
  serialize($iterator, to => $file, as => 'NTriples');

=back

=head2 Array References

In addition to the above interface, each function supports being called with a
single arrayref argument. In those cases, the arrayref is dereferenced into an
array, and treated as a list of arguments. That is, the following are
equivalent:

  foo($bar, $baz);
  foo([$bar, $baz]);

This is handy if you're writing a module of your own and wish to accept some
RDF data:

  sub my_method {
    my ($self, $rdf, $foo) = @_;
    $rdf = parse($rdf);
    
    ....
  }

Your method can now be called like this:

  $object->my_method($model, 'foo');
  
  $object->my_method($url, 'foo');
  
  $object->my_method(
      [ $filehandle, as => 'Turtle', base => $uri ],
      'foo',
  );

=head2 Export

By default, nothing is exported. You need to request things:

  use RDF::TrineX::Functions qw< iri literal blank statement model >;

Thanks to L<Sub::Exporter>, you can rename functions:

  use RDF::TrineX::Functions
    qw< literal statement model >,
    blank => { -as => 'bnode' },
    iri   => { -as => 'resource' };

If you want to export everything, you can do:

  use RDF::TrineX::Functions -all;

To export just the functions which generate RDF::Trine::Node objects:

  use RDF::TrineX::Functions -nodes;

Or maybe even:

  use RDF::TrineX::Functions -nodes => { -suffix => '_node' };

If you want to export something roughly compatible with the old
RDF::TrineShortcuts, then there's:

  use RDF::TrineX::Functions -shortcuts;

When exporting the C<serialize> function you may set a default format:

  use RDF::TrineX::Functions
      serialize => { -type => 'NTriples' };

This will be used when C<serialize> is called with no explicit type given.

=head2 Pseudo-OO interface

=over

=item C<new>

This acts as a constructor, returning a new RDF::TrineX::Functions object.

=back

All the normal functions can be called as methods:

 my $R = RDF::TrineX::Functions->new;
 my $model = $R->model;

There's no real advantage to using this module as an object, but it can
help you avoid namespace pollution.

=head1 BUGS

Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=RDF-TrineX-Functions>.

=head1 SEE ALSO

L<RDF::Trine>, L<RDF::QueryX::Lazy>, L<RDF::NS>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2012 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.