Difference between revisions of "Perl 101 for EPrints"

From EPrints Documentation
Jump to: navigation, search
(Arrays)
(Retrieving data from a config file)
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category:Documentation_Needed]]
 
[[category:Documentation_Needed]]
  
===Retrieving data from a structure in a config file===
+
===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
 
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)
 +
 
<source lang="perl">
 
<source lang="perl">
 
$c->{etd_ms}->{fields} = [
 
$c->{etd_ms}->{fields} = [
{ tagname => 'etd_ms:title', eprint_fieldname => 'title', type => 'simple_text', include_null_value => 1 },
 
{ tagname => 'etd_ms:creator', eprint_fieldname => 'creators_name', type => 'name', multiple => 1},
 
{ tagname => 'etd_ms:subject', eprint_fieldname => 'keywords', type => 'simple_text', include_null_value => 1 },
 
{ tagname => 'etd_ms:description', eprint_fieldname => 'abstract', type => 'simple_text' },
 
{ tagname => 'etd_ms:publisher', eprint_fieldname => 'institution', type => 'simple_text' },
 
{ tagname => 'etd_ms:contributor', eprint_fieldname => 'thesis_advisors_name', type => 'name', opts => { role => 'advisor' }, multiple => 1},
 
{ tagname => 'etd_ms:date', eprint_fieldname => 'date', type => 'simple_text' },
 
 
{ tagname => 'etd_ms:type', type=> 'constant', value => "Electronic Thesis or Dissertation" },
 
{ tagname => 'etd_ms:type', type=> 'constant', value => "Electronic Thesis or Dissertation" },
{ tagname => 'etd_ms:identifier', type => 'function', function => 'generate_etd_ms_item_identifier' },
 
{ tagname => 'null', type => 'function', function => 'generate_etd_ms_document_tags' },
 
 
{ tagname => 'etd_ms:degree', type => 'compound', parts => [
 
{ tagname => 'etd_ms:degree', type => 'compound', parts => [
 
{ tagname => 'etd_ms:name', type => 'simple_text', eprint_fieldname => 'thesis_degree_name' },
 
{ tagname => 'etd_ms:name', type => 'simple_text', eprint_fieldname => 'thesis_degree_name' },
Line 24: Line 42:
 
];
 
];
 
</source>
 
</source>
Retrieving values from this config file in an export plugin under /plugins/EPrints/Plugin/Export/example.pm
+
 
 +
Retrieving values from this config file in an "Example" export plugin under /plugins/EPrints/Plugin/Export/example.pm
  
 
<source lang="perl">
 
<source lang="perl">
 +
              package EPrints::Plugin::Export::Example;
 +
              use EPrints::Plugin::Export;
 +
              @ISA = ( "EPrints::Plugin::Export" );
 +
              [...]
 
               my $fields = $session->get_conf('etd_ms','fields');
 
               my $fields = $session->get_conf('etd_ms','fields');
 
               foreach my $field_conf (@{$fields})
 
               foreach my $field_conf (@{$fields})
Line 36: Line 59:
 
  }
 
  }
 
}
 
}
 +
</source>
 +
 +
Supporting function that returns an arrayref
 +
 +
<source lang="perl">
 +
sub generate_tag {
 +
my ($plugin, $eprint, $field_conf) = @_;
 +
 +
#Constant value
 +
if ($field_conf->{type} eq 'constant')
 +
{
 +
return $plugin->generate_constant_tag($eprint, $field_conf);
 +
}
 +
}
 +
</source>
 +
 +
Supporting function that generates a tag
 +
 +
<source lang="perl">
 +
sub generate_constant_tag{
 +
my ($plugin, $eprint, $field_conf) = @_;
 +
 +
my $tag = $plugin->_simple_tag($field_conf, $field_conf->{value});
 +
return [ $tag ];
 +
}
 +
</source>
 +
 +
===Function parameters===
 +
In perl, the following: <source lang="perl">$obj->function($arg1, $arg2)</source> is equivalent to: <source lang="perl">Class::function($obj, $arg1,$arg2)</source>
 +
In the example above, when we call:
 +
<source lang="perl">
 +
my $tags = $plugin->generate_tag($eprint, $field_conf);
 +
</source>
 +
It is equivalent to:
 +
<source lang="perl">
 +
my $tags = Example::generate_tag($plugin, $eprint, $field_conf);
 +
</source>
 +
This explains the three parameters listed in the top of the generate_tag function definition:
 +
<source lang="perl">
 +
sub generate_tag {
 +
my ($plugin, $eprint, $field_conf) = @_;
 
</source>
 
</source>

Revision as of 17:57, 28 July 2015


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) = @_;