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
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs