use strict; use warnings;
and so on at the top of each script they write. For scripts which I don't intend to publish anywhere, I have a module (which I accidentally called Z not knowing there was already a module of the same name on CPAN), which switches on lots of things at once just by saying
use Z;
The top bit goes like this:
package Z; use warnings; use strict; use utf8; use Carp; use Deploy 'do_system'; use File::Slurper qw!read_text write_text read_lines!; use FindBin '$Bin'; use Getopt::Long; use Table::Readable ':all'; use v5.32; no warnings qw(experimental::signatures); use feature qw(signatures);
So far that is all fairly standard stuff, but what Z does is also to import all of the above things into my script using the EXPORT
variables from the above modules:
our $VERSION = '0.01'; require Exporter; our @ISA = qw(Exporter); our @EXPORT = ( @Carp::EXPORT, @Deploy::EXPORT_OK, @File::Slurper::EXPORT_OK, @FindBin::EXPORT_OK, @Getopt::Long::EXPORT, @Table::Readable::EXPORT_OK, );
This requires a special import
method:
sub import { my ($class) = @_; strict->import (); utf8->import (); warnings->import (); warnings->unimport (qw(experimental::signatures)); feature->import ('signatures'); Carp->import (); File::Slurper->import (qw!read_text write_text!); FindBin->import ('$Bin'); Getopt::Long->import (); Deploy->import ('do_system'); Table::Readable->import (':all'); Z->export_to_level (1); }
To save another bit of boilerplate I also have
binmode STDOUT, ":encoding(utf8)";
at the end of the module.
This is for personal convenience so it's not something I would use publicly, but perhaps people who want to save themselves a bit of boilerplate might find this useful for making their own "personal module".
]]>valgrind perl -I blib/lib -I blib/arch ./mytestscript
or if your problems are even more serious ones like segmentation fault errors you can run Perl under a debugger like gdb:
gdb perl
% run -I blib/lib -I blib/arch ./mytestscript
However, it might be worth noting some "easy" ways to catch memory errors which actually catch a lot of things.
The first thing is setting the variable to 0 after freeing:
free (result);
result = 0;
This prevents you from using the variable again accidentally after freeing, although free (0) is actually not an error, so it doesn't prevent you freeing it twice.
Another way which catches quite a lot of careless mistakes is counting mallocs and frees:
x = malloc (100);
n_mallocs++;
y = malloc (300);
n_mallocs++;
free (x);
n_mallocs--;
then at the end of the program:
if (n_mallocs != 0) {
fprintf (stderr, "n_mallocs = %d\n", n_mallocs);
}
I was talking to a friend who works as a surgical assistant about something I saw on "Tomorrow's World" where they had a very complicated machine for ensuring that surgical equipment doesn't get left in people's bodies. It used all kinds of pattern recognition to identify instruments as they were checked out of the sterilizer and then returned. I was quite surprised when she told me that the way that they do that is to always count how many instruments they have issued and then count how many they have returned to the sterilizer. Similar to just counting the mallocs, it's a much simpler but equally effective method for detecting unfreed memory.
For example, in this module I made the n_mallocs counter part of the structure which contains the JSON information. In this module I put the counter and the memory assignment inside a macro, which is probably a good way to make sure not to forget to increment. But if you do forget to increment or decrement, the error message will trip during testing, and you'll quickly find out.
There are lots of other things you can do, such as using memory sanitizer etc., but these easy methods actually get a lot of the problems.
]]>https://www.google.com/search?hl=en&q=site%3Ablogs.perl.org%20%22donald%20trump%22
It might be better to actually post your issues with individual bloggers as comments under their posts so they can work out what is going on.
I also don't remember seeing posts about system perl. When I searched on Google about that I found about one post from 2011:
https://www.google.com/search?hl=en&q=site%3Ablogs.perl.org%20%22system%20perl%22
Actually your blog post came above that in the search results.
Are you sure it's a real problem here?
I don't think there are that many "newbies" in Perl these days, it seems like people prefer to learn Python or other languages like Go or Rust rather than Perl. Perl seems to have become more like Lisp or something where there is a coterie of people who think it's good and the vast majority of people who avoid it, and prefer something easier to learn. Similar thing seems to have happened to Ruby. We probably should stop worrying about the needs of the "Perl newbies" in my opinion. There used to be lots of Perl newbies in the 1990s but those people probably aren't coming back unless Perl does something dramatic.
There was a discussion on Perl5 Porters mailing list the other day where someone was arguing that it was not a good idea to make "use strict;" default, because it would confuse the "Perl newbies". I think these Perl newbies are just a convenient excuse for various people these days!
If Perl wants to have any newbies, the best way is to remove all these insane legacy things like File::Find which people are keeping there for the sake of all the imaginary newbies.
]]>https://metacpan.org/release/Lingua-EN-Opinion/source/lib/Lingua/EN/Opinion
It has a dictionary of English words, and each word has its own hash reference, with ten different keys.
It might be better not to slurp them all in at once. Perhaps the PAUSE indexer could do something like
if (-s $pm > $max) {
line_by_line_processing;
}
else {
all_at_once_processing;
}
I think package NAMESPACE and package NAMESPACE VERSION are both always followed by one.
BLOCK just means {something} doesn't it, so the matching is package\s+.*?\s*(;|\{).
]]>It seems to me that PAUSE could check for a semicolon, but there are probably exceptions for that.
This line:
https://metacpan.org/release/Go-Tokenize/source/lib/Go/Tokenize.pm#L43
also produced an unusual error message, refer to the comment above that if interested.
Failed: PAUSE indexer report BKB/Go-Tokenize-0.01.tar.gz module : switch
It looks like it doesn't like this line of code containing Go keywords:
chan else goto package switch]]>
It is based on the Zopfli gzip compression library from Google Research.
Both Zopfli and libdeflate seem to excel at compressing JSON files compared to the ordinary zlib compression, sometimes with an improvement of up to 30%. Zopfli usually wins by a small margin over libdeflate. Here are some results of this script on random JSON files:
6194 698 703 682 3809 1184 977 953 1446 494 486 485 1572152 824430 710102 707844 28061 5080 4554 4546 565082 125097 118708 118494 317448 164741 140087 139829 2069690 1062866 865985 866129 565082 125097 118708 118494
On the left is the uncompressed size, then gzip -9, libdeflate, and zopfli number of bytes.
Zopfli almost always wins by a small margin over libdeflate.
People who want to try comparing the three things might find this repository useful:
At the time of publishing this blog post, the scripts will require a little editing to work.
]]>File::Find is not only on CPAN but in the Perl core and yet it is insufficiently mature to have sensible functionality or documentation.
Just think "My module cannot possibly be worse than File::Find" then it's OK to upload it.
Go go go!
This is the gzip compression method, but updated.
It's supposed to be much faster and better than libz (the original gzip library).
Sometimes I am getting compression of as much as 30% better on some files.
So far I haven't tested whether or not it is faster.
See the module above for links to the original library and so on.
Since this is an early release, it's very likely indeed that bugs in the module are my fault rather than any problem with libdeflate, so please report them to me.
]]>use lib ".";
when the library is in the same directory. This was changed a while ago due to some kind of "security" thing, so now I have all these
use FindBin '$Bin';
use lib "$Bin";
statements everywhere.
I have to say I think it would be easier to upload this to CPAN then one can install it just using cpanm.
]]>