Today we are going to learn how to simplify repetitive Perl variables declarations.
Posts for: #Perl
Data::Printer joy Series
This is a series of short notes about the famous Perl objects an data structures inspector. After reading this, you should have learned a couple of useful tricks that will simplify every-day’s debugging:
Maximize Data::Printer strings length Demand Data::Printer to dump politely if it doesn’t want to Preload Data::Printer and avoid polluting your code with debug symbols
Preload Data::Printer and avoid polluting your code with debug symbols
When I work, I tend to have a lot of boilerplate. I am a great user of Data::Printer for debugging. There is no way I wouldn’t use it, even on the smallest project. Usually, I have this on all my projects:
use strict; use warnings; use Data::Printer; p $object; Fellow Peter coworker saw me doing that once and said I liked typing too much: only use DDP was enough.
Later I learned another solution was provided in the DDP documentation.
Demand Data::Printer to dump politely if it doesn’t want to
If your project got objects that use the following methods names:
to_string(), as_string(), stringify(), … Data::Printer might not behave as expected… It will dump the “stringified” object (as defined in your own code, possibly $self->name or something slightly more complicated).
It’s possible to disable that feature on p() call:
p $fancy_object, class => { stringify => 0 }; But honestly, that’s too much typing verbosity. I prefer, to have a ~/.
Notes about the ongoing Perl logo discussion
More talks popped up recently about the Perl logo topic. I feel involved into this subject because even if my activities today are mostly about Perl programming and systems maintenance, I am from an artistic background, including graphic design. I was also interested about Camelia design talks in the past and the initial mascot “specifications”.
A professional graphic designer should be involved in the core discussion around the Perl logo.
Maximize Data::Printer strings length
The Perl data structures and objects pretty printer Data::Printer would trim strings longer than 4096 characters (this is the default value).
This is something that can be parameterized in the ~/.dataprinter file:
# No limit! string_max = 0 This default is sensible, though, because you usually don’t want the program output to fill with too much text.
It might be necessary to change this value, only in a specific place. You can pass a false value to string_max to display the whole string fields:
Load a list of lines into an array (easily)
This blog post describes a common task my colleagues often ask about repeating a dynamic string in a defined token, and adding some or, and, = in between, plus finishing smartly.
I like to use the Perl’s __DATA__ token at the end of my scripts for this. The __DATA__ token strength is to make possible to « “embed” a file inside a Perl program then read it from the DATA filehandle ».
A concise mtime sorted directory listing application
Today we will focus on a simple task: listing the files contained in a directory, sort them by modification time (mtime) and display the result in a JSON array.
We are gonna use Mojo::File for the file system part and Mojolicious::Lite to expose those data on a simple but effective JSON API.
Prerequisites apt install libmojolicious-perl touch mtime.pl && chmod +x mtime.pl The code Our lite application is separated in two main parts:
While Loops That Have an Index
Perl got so many functionnalities that it’s possible to get an index when iterating over a while loop.