Creating and displaying terms
Posted on September 12th, 2007 by Eric Lease Morgan
Here is a simple command-line program to create a term. It works much like the facets example: 1) prompt for a name, 2) prompt for a note, 3) display the facets, 4) prompt for a facet ID, and 5) commit.
#!/usr/bin/perl
# require mylibrary & practice good programming
use MyLibrary::Core;
use strict;
# scope our variables
my ( $name, $note, $id, $term, $results, $facet );
# get user input
print "Enter information about your term.\n";
print " Name: "; chop ( $name = <STDIN> );
print " Note: "; chop ( $note = <STDIN> );
# list all the facets and select an id
foreach ( MyLibrary::Facet->get_facets ) {
print $_->facet_id . "\t" . $_->facet_name . "\n";
}
print " Enter a facet ID: "; chop ( $id = <STDIN> );
# create a term object
$term = MyLibrary::Term->new;
# assign the name, note, and facet id to the term
$term->term_name( $name );
$term->term_note( $note );
$term->facet_id( $id );
# save the term to the database
$results = $term->commit;
# check the results
if ( $results == 1 ) {
# get the facet name
my $facet = MyLibrary::Facet->new( id => $term->facet_id );
# echo
print "\n";
print "The term was successfully created:\n";
print " ID - " . $facet->facet_id . "\n";
print " name - " . $facet->facet_name . "\n";
print " note - " . $facet->facet_note . "\n";
print " facet - " . $facet->facet_name . "\n";
print "\n";
}
# done
exit;
You can then use this code to display all of your terms:
#!/usr/bin/perl
# require mylibrary & practice good programming
use MyLibrary::Core;
use strict;
# loop through all the terms
foreach my $term ( MyLibrary::Term->get_terms ) {
# get the associated facet
my $facet = MyLibrary::Facet->new( id => $term->facet_id );
# display this facet's characteristics
print ' ID: ' . $term->term_id . "\n";
print ' Name: ' . $term->term_name . "\n";
print ' Note: ' . $term->term_note . "\n";
print ' Facet: ' . $facet->facet_name . "\n";
print "\n";
}
# done
exit;
Discussion Area - Leave a Comment
You must be logged in to post a comment.