XS Modules - why and when?
in Seekers of Perl Wisdom
⭐ (xs, b, and internals)
9 direct replies — Read more / Contribute
|
by Bod
on Dec 04, 2023 at 19:22
|
|
|
I know little about XS modules but I was looking at RPi::DHT11 and noticed that it is an XS module which seemed a little strange. Hence the question here. I was going to email stevieb, the author and ask but thought it would be good to get other people's take on this.
My understanding of an XS module is that it uses XSLoader to load C / C++ / C# code. The Perl is just a wrapper around the loaded code with the Perl doing little processing. I believe the main reason to do this is for speed as compiled languages of the C family are faster than interpreted languages such as Perl. Is that about right so far?
Assuming that is something like correct...
Why would the RPi::DHT11 module use XS? The DHT11 is a temperature and humidity sensor with a maximum frequency of 0.5Hz (one read per 2 seconds). So speed is not the issue here!
What reason would there be for creating an XS module in this use case? Would it not be easier and simpler and just as fast given the speed of the sensor, to write all the code in Perl?
I'm hoping for more insights into the world of XS modules here...
|
Capture Groups
in Seekers of Perl Wisdom
3 direct replies — Read more / Contribute
|
by Bod
on Nov 14, 2023 at 15:11
|
|
|
I've had cause to run some code from the console that usually only gets run on a webserver. In its usual environment, it doesn't give me warnings. But, from the console, I get a warning...
$result =~ s/(Mr(s)?) a/\1 A/s;
\1 better written as $1 at...
I have always been under the impression (or perhaps illusion) that \1 should be used within the regular expression and $1 outside of it. Like this:
if ($test =~ /some_test/) {
$foo = $1;
}
Has something changed in the preferred way to capture in a regular expression, or have I been doing it wrong all the time?
|
Wide characters and UTF8
in Seekers of Perl Wisdom
1 direct reply — Read more / Contribute
|
by Bod
on Nov 08, 2023 at 11:00
|
|
|
Could you please help me with this little script?
I am trying to pre-parse (i.e. check that I can get the data I need) a JSON file from the UK Charity Commision. The data source is not known for being well-formated but I am pretty sure the issue here is with my code and (lack of) understanding of character encoding!
use strict;
use warnings;
use Data::Dumper;
use utf8;
use JSON;
$/ = undef;
open my $fh, '<:encoding(UTF-8)', 'publicextract.charity.json' or die
+$!;
my $data = <$fh>;
close $fh;
$data =~ s/^\x{feff}//; # Strip off BOM
my $json = decode_json $data; # <-- Wide character in subroutine ent
+ry at json.pl line 15.
foreach my $j(@$json) {
print "$j->{'charity_contact_phone'},$j->{'charity_contact_email'}
+\n";
}
print "\nComplete!\n\n";
The documentation for decode_json says it takes a UTF8 encoded string. So I have saved the JSON file as UTF8 using TextPad and opened the file with the same encoding. But, decode_json is croaking Wide character in subroutine entry
What have I overlooked?
|
Displaying POD like MetaCPAN
in Seekers of Perl Wisdom
5 direct replies — Read more / Contribute
|
by Bod
on Nov 07, 2023 at 16:50
|
|
|
Do you know of any modules or services that can display POD in the same (or similar) style as MetaCPAN?
We want to document internal modules consistently and it seems sensible to keep the layout and style in one with public modules.
|
LICENCE file
in Seekers of Perl Wisdom
3 direct replies — Read more / Contribute
|
by Bod
on Oct 15, 2023 at 16:42
|
|
|
I've added a LICENCE file following advice from SankoR in Re: GitHub perl-ci.yml
I have used App::Software::License as suggested in this Perl blog
However, both the blog and the module are quite old. Plus, the blog comment suggests this might not be the best methodology...
Is there a 'better' method that Monks are using?
As an aside, for the benefit of anyone stumbling this way looking at using App::Software::License. The documentation is wrong - at least for Strawberry Perl on Windows. The documentation shows single quotes for the --holder attribute. If single quotes are used, just the firstname is shown in the LICENCE file. Using double quotes solves this problem: --holder "Joe Bloggs"
|
GitHub perl-ci.yml
in Seekers of Perl Wisdom
3 direct replies — Read more / Contribute
|
by Bod
on Oct 12, 2023 at 19:36
|
|
|
Whilst attempting to test a previous module, pryrt very helpfully introduced me to GitHub Continuous Integration.
I tried using this when I released WWW::Crawl and got lots of test configurations to pass, but others failed. When the tests ran again, different configurations passed and different configurations failed!
From this test run it seems much of the problem is from the $VERSION string. But this inconsistency is not reflected in the CPAN Testers results.
Is this an anomaly of GitHub Continuous Integration or have I got something wrong in the per-ci.yml file?
|
Storable for user preferences
in Seekers of Perl Wisdom
7 direct replies — Read more / Contribute
|
by Bod
on Sep 30, 2023 at 14:17
|
|
|
I need to store user preferences...
Currently, there are only two user preferences and these are stored as fields in the User table of the database. However, the number of preferences is going to increase. I guess there will be around 20 or so eventually. Experience suggests that most users won't change most of their preferences from the default setting. Therefore, I don't really want to add all the preferences to the User table when nearly all of them will be set to the default values.
I could implement the preferences in the database with an EAV model. But I wondered if Perl's core Storable might be a better solution.
I've not used Storable before so I'd appreciate the monastic wisdom about any issues I might face using Storable generally and it's suitability for this specific application...
|
Testing with Test::Mock::HTTP::Tiny
in Seekers of Perl Wisdom
2 direct replies — Read more / Contribute
|
by Bod
on Sep 26, 2023 at 16:11
|
|
|
I am writing some tests of a module that fetches a webpage using HTTP::Tiny->get
To test it, I am trying to use Test::Mock::HTTP::Tiny but I've never tried to use a Test::Mock module before. It's been on my radar since kcott mentioned their existence many moons ago. Now I have the need for one...but the documentation is lacking (to put it mildly!)
First I've run this code to get the mock data:
use strict;
use warnings;
use HTTP::Tiny;
use Test::Mock::HTTP::Tiny;
my $http = HTTP::Tiny->new;
my $resp = $http->get('http://www.way-finder.uk/');
open my $fh, '>', 'mock_html.dat';
print $fh Test::Mock::HTTP::Tiny->captured_data_dump;
close $fh;
Then I've renamed all the references to the domain www.way-finder.uk (a real domain) to www.testing.crawl (a mock domain). I've done this because I don't want the tests going out to a live site as it will change over time invalidating the tests.
My test file looks like this:
use strict;
use warnings;
use Test::More;
use Test::Mock::HTTP::Tiny;
use WWW::Crawl;
plan tests => 1;
$/ = undef;
open my $fh, '<', 't/mock_html.dat' or die "Can't open datafile";
my $replay = <$fh>;
close $fh;
die "Nothing to replay" unless $replay;
Test::Mock::HTTP::Tiny->set_mocked_data($replay);
my $crawl = WWW::Crawl->new(
'timestamp' => 'a',
);
my @links = $crawl->crawl('https://www.testing.crawl', \&link);
cmp_ok ( scalar @links, '==', 8, 'Correct link count');
sub link {
diag ($_[0]);
}
The method $crawl->crawl uses HTTP::Tiny->get to get the web address.
My expectation was that Test::Mock::HTTP::Tiny would replay the website to HTTP::Tiny but instead it gives the HTTP error 599 - Internal Exception
Is this the right way to use Test::Mock objects or am I completely off track here?
EDIT:
Corrected typo in title
ANOTHER EDIT:
Corrected module in title!
|
Finding when a feature or keyword was introduced
in Seekers of Perl Wisdom
5 direct replies — Read more / Contribute
|
by Bod
on Sep 26, 2023 at 13:12
|
|
|
Is there an easy way to find out when a feature or keyword was introduced to Perl?
In other words, which version introduced it...
I have a module that uses pos, a keyword I infrequently use. How can I find out when this, or any other keyword, was introduced so I can set the minimum required Perl version?
Obviously I could go through each delta until I find it but that seems rather tedious...
|
chomp and regexp
in Seekers of Perl Wisdom
2 direct replies — Read more / Contribute
|
by Bod
on Sep 14, 2023 at 10:38
|
|
|
I am enforcing some rules are CRM tags and the regexps are pushing my limits. So, a follow up to a little question over at Re^3: regex in REPLACEMENT in s///
Part A
I have never got chomp to work so I avoid it...
my $tag = 'test text ';
chomp $tag;
print $tag . '#';
> test text #
I understood it to equivalent to s/ +$//; when used as above with no new lines.
Part B
The tags should be lowercase and exclude most punctuation. Extraneous punctuation removed and uppercase characters converted to lowercase. Here is what I have tried
$tag = lc $tag;
$tag =~ s/[^az09 _\-\+\.]//g;
I expected the regexp to substitute anything that is not ^ in the character class [] with an empty string. But it seems to strip out anything that is not an 'a' character or a space.
How should I go about properly working out how to construct a regexp to do what I want?
|