Agile, test-driven development
Getters and Setters
For the constructor of the User
class to accept the email string parameter as a named parameter, an eponymous accessor method to give it back, and a setter to set new values, Perl hackers had to insert dozens of lines of code manually in the pre-Moose era. With Moose, this is a no-brainer because its has
function defines a class attribute that can, at the same time, be addressed using a constructor parameter, a getter – email()
– and a setter – email( $ email )
.
Listing 4 shows a later version of the User
class that uses has
to define the email
attribute. Its is
parameter uses rw
to make the value readable and writable; isa
defines it as a Str
(i.e., an arbitrary string).
Listing 4
User.pm
Running the test suite again in Figure 2 shows that all three defined test cases now complete successfully. So, development can proceed.
Putting the Customers in a Database
The next thing the product development specification requires is that users register in a customer database using their email addresses. True to the principles of TDD, Listing 5 defines the first test case with the test_customers()
routine. It uses the Customers
class and its new()
constructor to generate a new in-memory customer database.
Listing 5
Register.pm
Next, it feeds two new users with their email addresses to the database using the not yet existing sign_up()
method. In the second for
loop starting in line 19, the test routine uses ok
and the method user_find_by_email()
to check whether the customer file object can find the recently registered customers. In this case, the method will return a true value by definition, once it has been implemented.
Searching for Users
Again, all grinds to a halt if the failed test suite wants it to be that way. To fix the "bug," Listing 6 implements the Customers
class, again using Moose and two additional methods. Perl's object system passes in a reference to the object as the first argument in method calls. The class defines a global hash %USERS
in which the sign_up()
method stores the User
type object passed to it under the user's email address.
Listing 6
Customers.pm
The lookup method user_find_by_email()
calls exists
to check the global hash and returns either the user object it finds, if the user is already registered, or undef
if it does not find the user. Once the code in Listing 6 is free of errors, the green light comes on again, and a further milestone in the project is in the bag.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.