Difference between revisions of "Perl 101 for EPrints"

From EPrints Documentation
Jump to: navigation, search
m (adding categories)
(add minimal script example)
Line 104: Line 104:
 
my ($plugin, $eprint, $field_conf) = @_;
 
my ($plugin, $eprint, $field_conf) = @_;
 
</source>
 
</source>
 +
 +
=== A basic EPrint extracting script ===
 +
 +
The simplest of examples, use eprints to register a session in the repository then read an (arbitrary) eprint; accessing two bits of information about it. The attributes accessed come from [[EPrint Object]] and [[API:EPrints/DataObj]].
 +
<source>
 +
 +
#!/usr/bin/perl -w                                                                                                                                       
 +
                                                                                                                                                         
 +
use EPrints;                                                                                                                                             
 +
                                                                                                                                                         
 +
use strict;                                                                                                                                             
 +
                                                                                                                                                         
 +
# Need repo id (second val) or errors of                                                                                                                 
 +
#Use of uninitialized value $repository_id in string eq at /usr/share/eprints/perl_lib/EPrints.pm line 551.                                             
 +
#Can't call method "get_database" on an undefined value at /usr/share/eprints/perl_lib/EPrints/DataObj.pm line 132.                                     
 +
# Create new session for interacting with eprints                                                                                                       
 +
my $session = EPrints::Session->new( 1, 'epprod');                                                                                                       
 +
my $id = 23743;                                                                                                                                         
 +
                                                                                                                                                         
 +
# Create new object referency thing for our desired eprint (23743)                                                                                       
 +
$::pulled_eprint = EPrints::DataObj::EPrint->new( $session, $id );                                                                                       
 +
                                                                                                                                                         
 +
print $::pulled_eprint->get_dataset_id . "\n";                                                                                                           
 +
print $::pulled_eprint->get_type . "\n";                                                                                                                 
 +
                                                                                                                                                         
 +
                                                                                                                                                         
 +
$session->terminate(); 
 +
 +
</source>
 +
 +
This example can be expanded to

Revision as of 05:50, 13 September 2018


Where to start

Learning Perl: Helping you get started with Perl 5.... http://learn.perl.org/

Perl Tutorials http://perldoc.perl.org/perl.html#Tutorials

Recommended books for learning Perl

Learning Perl, 6th Edition by Randal Schwartz, brian d foy, Tom Phoenix http://www.amazon.com/Learning-Perl-Randal-L-Schwartz/dp/1449303587

Programming Perl: Unmatched power for text processing and scripting Fourth Edition Edition by Tom Christiansen, brian d foy, Larry Wall, Jon Orwant http://www.amazon.com/Programming-Perl-Unmatched-processing-scripting/dp/0596004923/

Intermediate Perl Second Edition Edition by Randal L. Schwartz, brian d foy, Tom Phoenix http://www.amazon.com/Intermediate-Perl-Randal-L-Schwartz/dp/1449393098/

Using Perl in EPrints

Retrieving data from a config file

Example of a data config file under /cfg/cfg.d/example.pl

The data structure is an array of hashes (http://perldoc.perl.org/perldsc.html#ARRAYS-OF-HASHES)

$c->{etd_ms}->{fields} = [
	{ tagname => 'etd_ms:type', type=> 'constant', value => "Electronic Thesis or Dissertation" },
	{ tagname => 'etd_ms:degree', type => 'compound', parts => [
		{ tagname => 'etd_ms:name', type => 'simple_text', eprint_fieldname => 'thesis_degree_name' },
		{ tagname => 'etd_ms:level', type => 'simple_text', eprint_fieldname => 'thesis_type' },
		{ tagname => 'etd_ms:discipline', type => 'simple_text', eprint_fieldname => 'department' },
		{ tagname => 'etd_ms:grantor', type => 'simple_text', eprint_fieldname => 'institution' },
	] },
];

Retrieving values from this config file in an "Example" export plugin under /plugins/EPrints/Plugin/Export/example.pm

               package EPrints::Plugin::Export::Example;
               use EPrints::Plugin::Export;
               @ISA = ( "EPrints::Plugin::Export" );
               [...]
               my $fields = $session->get_conf('etd_ms','fields');					
               foreach my $field_conf (@{$fields})
		{
		   my $tags = $plugin->generate_tag($eprint, $field_conf);			
		   foreach my $tag (@{$tags})
		   {
					push @dcdata, ($tag) if $tag; 
		   }
		}

Supporting function that returns an arrayref

sub generate_tag {
	my ($plugin, $eprint, $field_conf) = @_;
 
	#Constant value
	if ($field_conf->{type} eq 'constant')
	{
		return $plugin->generate_constant_tag($eprint, $field_conf);
	}
}

Supporting function that generates a tag

sub generate_constant_tag{
	my ($plugin, $eprint, $field_conf) = @_;
 
	my $tag = $plugin->_simple_tag($field_conf, $field_conf->{value});
	return [ $tag ];
}

Function parameters

In perl, the following:
$obj->function($arg1, $arg2)
is equivalent to:
Class::function($obj, $arg1,$arg2)

In the example above, when we call:

my $tags = $plugin->generate_tag($eprint, $field_conf);

It is equivalent to:

my $tags = Example::generate_tag($plugin, $eprint, $field_conf);

This explains the three parameters listed in the top of the generate_tag function definition:

sub generate_tag {
my ($plugin, $eprint, $field_conf) = @_;

A basic EPrint extracting script

The simplest of examples, use eprints to register a session in the repository then read an (arbitrary) eprint; accessing two bits of information about it. The attributes accessed come from EPrint Object and API:EPrints/DataObj.

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


#!/usr/bin/perl -w                                                                                                                                        
                                                                                                                                                          
use EPrints;                                                                                                                                              
                                                                                                                                                          
use strict;                                                                                                                                               
                                                                                                                                                          
# Need repo id (second val) or errors of                                                                                                                  
#Use of uninitialized value $repository_id in string eq at /usr/share/eprints/perl_lib/EPrints.pm line 551.                                               
#Can't call method "get_database" on an undefined value at /usr/share/eprints/perl_lib/EPrints/DataObj.pm line 132.                                       
# Create new session for interacting with eprints                                                                                                         
my $session = EPrints::Session->new( 1, 'epprod');                                                                                                        
my $id = 23743;                                                                                                                                           
                                                                                                                                                          
# Create new object referency thing for our desired eprint (23743)                                                                                        
$::pulled_eprint = EPrints::DataObj::EPrint->new( $session, $id );                                                                                        
                                                                                                                                                          
print $::pulled_eprint->get_dataset_id . "\n";                                                                                                            
print $::pulled_eprint->get_type . "\n";                                                                                                                  
                                                                                                                                                          
                                                                                                                                                          
$session->terminate();

This example can be expanded to