newdevs:perl
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| newdevs:perl [2024/01/17 16:40] – [Evergreen Conference Sessions] sandbergja | newdevs:perl [2024/01/18 10:11] (current) – fix headings, add a link sandbergja | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ====== The Perl Logic Layer ====== | ====== The Perl Logic Layer ====== | ||
| + | ===== Structure ===== | ||
| - | |||
| - | |||
| - | |||
| - | ==== Structure ==== | ||
| Most of Evergreen' | Most of Evergreen' | ||
| Line 14: | Line 11: | ||
| There are many Perl scripts throughout the project. | There are many Perl scripts throughout the project. | ||
| - | ==== Dependencies ==== | + | ===== Dependencies |
| Evergreen' | Evergreen' | ||
| - | ==== Checking your work ==== | + | ===== Checking your work ===== |
| Here are some things you can do to check your work while working with Evergreen' | Here are some things you can do to check your work while working with Evergreen' | ||
| - | * If it is in an OpenSRF method: Build it, install it, and restart your services. | + | * If it is in an OpenSRF method: Build it, install it, and restart your services. |
| + | * To build and install the perl code, you can run '' | ||
| * Write some tests to confirm that your code is working as intended, without any unintended side effects. | * Write some tests to confirm that your code is working as intended, without any unintended side effects. | ||
| - | * Run `perl -cw [name of file]` to check your syntax and check for warnings. | + | * Run '' |
| - | * Run perlcritic on your code to check for common gotchas and readability. | + | * Run perlcritic on your code to check for common gotchas and readability. |
| * Temporarily adding print or logging statements to your work. If you'd like to print an array or hash, try using Data:: | * Temporarily adding print or logging statements to your work. If you'd like to print an array or hash, try using Data:: | ||
| - | ==== Using Cstore to interact with the database ==== | + | ===== Using Cstore to interact with the database |
| - | ==== Evergreen Conference Sessions ==== | + | A lot of the Perl code uses a Cstore editor to create, retrieve, update, delete, and search records in the database. |
| + | |||
| + | use strict; | ||
| + | use warnings; | ||
| + | use OpenILS:: | ||
| + | use OpenILS:: | ||
| + | |||
| + | ######### | ||
| + | # Setup # | ||
| + | ######### | ||
| + | |||
| + | # Create a new Cstore editor | ||
| + | my $e = new_editor(authtoken => $auth); | ||
| + | |||
| + | # Check that the auth token is valid | ||
| + | return $e-> | ||
| + | |||
| + | # Check that the user has the ADMIN_CAROUSEL permission. | ||
| + | # CStore doesn' | ||
| + | # so your Perl code needs to explicity do these checks. | ||
| + | return $e-> | ||
| + | |||
| + | ######################### | ||
| + | # Create a new carousel # | ||
| + | ######################### | ||
| + | |||
| + | # Assemble a new fieldmapper object in memory | ||
| + | my $carousel = Fieldmapper:: | ||
| + | $carousel-> | ||
| + | $carousel-> | ||
| + | $carousel-> | ||
| + | $carousel-> | ||
| + | $carousel-> | ||
| + | $carousel-> | ||
| + | $carousel-> | ||
| + | |||
| + | # Then, start a transaction in the database. | ||
| + | # required any time you are changing data in the database | ||
| + | $e-> | ||
| + | |||
| + | # Persist the carousel to the database | ||
| + | $e-> | ||
| + | |||
| + | # Commit the transaction. | ||
| + | # transactions open for a long time. | ||
| + | $e-> | ||
| + | |||
| + | ############################# | ||
| + | # Retrieve a carousel by ID # | ||
| + | ############################# | ||
| + | |||
| + | my $other_carousel = $e-> | ||
| + | print "The other carousel name is $other_carousel-> | ||
| + | |||
| + | ##################### | ||
| + | # Update a carousel # | ||
| + | ##################### | ||
| + | |||
| + | # Again, make our changes in memory | ||
| + | $carousel-> | ||
| + | |||
| + | # Open a transaction, | ||
| + | $e-> | ||
| + | $e-> | ||
| + | $e-> | ||
| + | |||
| + | |||
| + | ##################### | ||
| + | # Delete a carousel # | ||
| + | ##################### | ||
| + | |||
| + | # Similarly, open a transaction, | ||
| + | # Note that the delete_container_carousel method accepts a Fieldmapper object, rather | ||
| + | # than just an ID. So, if you only have an ID, you'll need to retrieve the object | ||
| + | # from the database before you can delete it. | ||
| + | $e-> | ||
| + | $e-> | ||
| + | $e-> | ||
| + | |||
| + | #################################### | ||
| + | # Search carousels by field values # | ||
| + | #################################### | ||
| + | |||
| + | # No transaction needed, since we aren't making changes | ||
| + | my $carousels_with_bucket_forty = $e-> | ||
| + | |||
| + | # The search_* methods return a reference. | ||
| + | # putting the array sigil (@) in front of our variable name. | ||
| + | for my $found_carousel (@$carousels_with_bucket_forty) { | ||
| + | print $found_carousel-> | ||
| + | } | ||
| + | |||
| + | # We can turn it back into a scalar to find the number of matching rows. | ||
| + | my $matching_rows = scalar @$carousels_with_bucket_forty; | ||
| + | |||
| + | # Perl critic recommends putting curly braces around the reference for clarity | ||
| + | $matching_rows = scalar @{ $carousels_with_bucket_forty }; | ||
| + | |||
| + | # You can also use the arrow operator and square brackets to get data directly from the array reference. | ||
| + | print 'The name of the first carousel is: ' . $carousels_with_bucket_forty-> | ||
| + | |||
| + | 1; | ||
| + | |||
| + | ===== Evergreen Conference Sessions | ||
| * [[https:// | * [[https:// | ||
| + | |||
| + | ===== Further Reading ===== | ||
| + | |||
| + | * [[http:// | ||
newdevs/perl.1705527649.txt.gz · Last modified: 2024/01/17 16:40 by sandbergja