Topics Search

CGI and Perl

Home » CGI and Perl (62)
  • Page 1 of 4
  • »
 

Exporting and importing functions easily

Exporting and importing functions easily Icon
Previously we talked about the use statement includes calling the import method of the module importing the functions of the module. How does the module author arrange for that to work? How can the module author declare which functions should the users be able to import? After all there might be helper function or internal functions that should not be called by an external user of the module.
 
Advertisement:
 

Difference between require and use

Difference between require and use Icon
We have learned about modules and how to load them to memory, but I have not explained the difference between use and require, and you probably have seen use in most places and I have only explained about require in the previous episode.
 

Switch-Case statement in Perl 5

Switch-Case statement in Perl 5 Icon
People coming from other languages often ask how to use the switch case statements in Perl 5. In a nutshell, you always use if, elsif, and else. The longer answer is as follows: Perl 5, the language itself does not have any statement similar to the switch and case statement of some other languages. (Neither does Python by the way.)
 

Test for expected warnings

Test for expected warnings Icon
There are many modules with function that would give a warning in certain situations. This can be a deprecation warning, when the function is left in to provide backward compability, but you actually want to remove it in the future. Or, it can be warning of improper input values if for some reason you do not want to throw an exception.
 

Organizing a test script with subtests

Organizing a test script with subtests Icon
As our test script grows we face several issues. One is that most humans cannot count more than 10. So when the number of test-units passes that number we will constantly make mistakes of the number of tests and soon will will give it up, using a solution such as no_plan.
 

Keep data in client and fetch only changes

Keep data in client and fetch only changes Icon
So far we have been fetching the full list of items on every change. Both when the user has added a new item and when the user has deleted an item. If there are only a few items this is not a big issue, but as more and more items are going to be in our database, and if each item will contain all kinds of other data fields, then fetching the full list will become a burden on the server and will generate unnecessary network traffic. So let's change how the system works. Send the full list only when first loaded and from there on send only the updates.
 

Basic Authentication with LWP::UserAgent and HTTP::Request::Common

Basic Authentication with LWP::UserAgent and HTTP::Request::Common Icon
A while ago I wrote an article on LWP::UserAgent and Basic Authentication and posted it on Reddit as well, where a user pointed to an even simpler solution, one that I did not find in the documentation myself. It uses LWP::UserAgent and HTTP::Request::Common which is a dependency of LWP::UserAgent anyway.
 

Test::Builder object Tutorial

Test::Builder object Tutorial Icon
As we saw earlier, Test::Builder is the backbone of the testing framework of Perl. The Test::Simple, Test::More are built on top of this module and there hundreds of other modules in the Test::* namespace on CPAN that use this same back-end. All those modules provide extra testing functionality.
 

Creating a testing module

Creating a testing module Icon
We can now reuse the is_any function in this test script, but what if we would like to use it in several scripts? What if we would like to allow other people in our organization to use it? And what, if we want to share it via CPAN so everyone using Perl will benefit from it?
 

Add a date stamp to the items in the database

Add a date stamp to the items in the database Icon
In order to expand the example, first we will add a timestamp to each item as it is inserted in the database and list the date on the web page when listing the items. MongoDB can automatically convert DateTime objects to its own native Date data-type, but DateTime is big and slow. A much smaller and faster module that works well in a lot of cases is DataTime::Tiny.
 

What should setters return? (Examples with core Perl OOP)

What should setters return? (Examples with core Perl OOP) Icon
In OOP (Object Oriented Programming), getter is the generic name for any method that will return the value of one of the attributes of the current instance. setter is the generic name of any method that will set the value of one of the attributes. It is clear that a getter needs to return the value of the attribute, but what should a setter return? There are a number of options.
 

Core Perl OOP: Constructor

Core Perl OOP: Constructor Icon
In this chapter we are going to see how write object oriented Perl code with core Perl. Without any additional modules. Manually blessing a hash reference. While in most new applications I'd probably recommend you either use Moose or Moo, there are plenty of places that make it hard to install CPAN modules and thus you cannot use those systems.
 

Is this IP in the given subnet?

Is this IP in the given subnet? Icon
In various situation, it is important to check if a given IP address is within a predefined subnet. For example if we would like to allow access from only a specific range of IP addresses we would create a "white list" of those addresses.
 

Deleting item using Ajax request with DELETE and OPTIONS

Deleting item using Ajax request with DELETE and OPTIONS Icon
We can now add new elements and list the existing elements from our list of items, but how can we delete an element? For this we need another ajax request that will send in the ID of the item. For that we'll have to create a button on the HTML page that already has this ID. Before doing the front-end though, let's add the API call for deleting an item.
 

Speed up calculation by running in parallel

Speed up calculation by running in parallel Icon
In this example we have a bunch of numbers. We need to make some heavy calculation on each number (using our calc function), and then collect the results in a hash where the keys are the original numbers and the values are the results. For the sake of our example the "heavy computation" is generating and summing up lots of random numbers.
 

Using 'like' to test without exact values

Using 'like' to test without exact values Icon
When testing a function, the best is if we can test that the function returns exactly what we expected. Unfortunately this is not always possible or worth the effort. For example, what if part of the return value is a timestamp that will be different every time we run the script. We can mock the time to pretend it is some other time of the year, but even that might not work out. If we get a newer machine, the process might run faster and by the returned time might not be exactly the same.
 

Testing timeout with cmp_ok

Testing timeout with cmp_ok Icon
As mentioned earlier in the testing series, the is() function provided by Test::More compares the values using string-eq which is correct in 99% of the cases. If you'd like to compare the values with the numerical == operator you can do it using the cmp_ok which is also provided by Test::More . More than that, with cmp_ok you can compare the two values with any operator, so you can check if the actual value is, let's say, smaller than an expected value.
 

Use of uninitialized value $got in pattern match (m//)

Use of uninitialized value $got in pattern match (m//) Icon
Test::Deep is a great module if you need to test deep and/or fuzzy data structures, but sometimes it does not give the correct error messages. Recently I've encountered such a case when I got tons of warnings like this: Use of uninitialized value $got in pattern match (m//) at Test/Deep/Regexp.pm line 57.
 

CSV - Comma Separated Values

CSV - Comma Separated Values Icon
CSV (where CSV stand for Comma-separated values) is one of the most common file formats as it can be used to easily represent table-like data. Similar to what you would put in an Excel file or in a Relational database where you used SQL.
 

String-eval in Perl

String-eval in Perl Icon
The eval keyword in Perl has two very different meanings depending on the syntax around it. If it is followed by a block as in eval { ... } then it is plain exception handling, which is usually called try in other languages. That is the "nice eval".
Home » CGI and Perl (62)
  • Page 1 of 4
  • »