If
you have a question on how to do something in Perl, or
you need a Perl solution to an actual real-life problem, or
you're unsure why something you've tried just isn't working...
then this section is the place to ask.
However, you might consider asking in the chatterbox first (if you're a
registered user). The response time tends to be quicker, and if it turns
out that the problem/solutions are too much for the cb to handle, the
kind monks will be sure to direct you here.
use strict;
use warnings;
use Math::BigInt;
# The following line of code will die with:
# Can't locate object method "palt" via package "Math::BigInt" at try.
+pl line 7.
# palt Math::BigInt->new(123456);
# But this next rendition is fine, and works as expected:
palt(Math::BigInt->new(123456));
sub palt {
my $arg= shift;
print "$arg\n";
}
__END__
Outputs:
123456
I need to calculate CRC-16/X25 checksum/digest in order to communicate with some instrument. I am using Digest::CRC module and https://crccalc.com/ site for both the source of appropriate parameters and to check the result. (The examples in the instrument manual agree with results on csccalc.com.) However, I cannot seem to get correct results. Here is a snippet of may code:
I am either interpreting the parameters incorrectly, on missunderstand the arcana of pack/unpack functions and the need for them, or something else entirely.
A bit of enlightenment would be appreciated.
Blessed Monks, I humbly prithee guidance bestow.
I'm looking to benchmark for a variant of the binary search algorithm, but before I begin I'm trying to bake it down to the most efficient Perl code I possibly can. I'm looking for the most efficient way to find the Most Significant Set Bit of a given integer value. I am unaware of a Perl function that does this. I have almost no experience in C, but I read that GCC does have an algorithm for this. Up to now, I have been using the following code to find the most significant set bit of a given 64-bit integer. I did my best to format it legibly here; though it looks like puke, it's a single statement. Is there a better performing solution?
As bliako generously published GCC::Builtins in response to this question, and a benchmark in the documentation refers back to this code for comparison, I've posted an updated version below which is more directly comparable. The code I originally posted above is exactly as slow, but the output constants represented the answer in different terms to clz(). The new code is just for comparing apples to apples (and heeding some legibility advice from hv).
I was surprised that GCC::Builtins::clz is only twice as fast, considering the essential part is just one CPU instruction (LZCNT) which should take practically no time at all. I guess most of that time is spent on the call stack and related.
There really isn't any good reason to use this, as bliako's GCC::Builtins::clz is about twice as fast (and about 64 times simpler) - unless, of course, you're modifying the constants to return some other pre-computed result for which there is no builtin alternative, to eliminate a few instructions. Personally, I'm going to see if I can write the whole encompassing piece of work in Perl+Assembly, as suggested. Straight down the rabbit hole at terminal velocity.
Hi Monks, how can I extract the email button the js button on this page:
https://reporter.nih.gov/project-details/10573139
Here's some code I've written that gives me a pdf but I need to have the script click the button before I grab the pdf and I can't figure that out. I tried to see if I could 'tab' to it as I could automate that with 13 tabs and a return or something but the button doesn't select. Any ideas/help are greatly appreciated.
use strict;
use File::Spec;
use File::Basename 'dirname';
use Log::Log4perl qw(:easy);
use WWW::Mechanize::Chrome;
my $url = 'https://reporter.nih.gov/project-details/11011199';
my $mech = WWW::Mechanize::Chrome->new(
headless => 1, # otherwise, PDF printing will not work
);
print "Loading $url";
$mech->get($url);
sleep 5;
my $fn= 'screen.pdf';
my $page_pdf = $mech->content_as_pdf(
filename => $fn,
);
print "\nSaved $url as $fn\n";
I am not very experienced with perl, but I know enough that this looks pretty bad
$dbh->begin_work;
my $href = $dbh->selectall_hashref("SELECT * FROM data WHERE ToScan IS
+ NULL AND title IS NOT NULL limit 1 FOR UPDATE SKIP LOCKED", "someid"
+);
foreach my $tmp (keys %$href) {
$ID=$href->{$tmp}->{'id'};
$title=$href->{$tmp}->{'title'};
$channel=$tmp;
}
my $rsth=$dbh->prepare("UPDATE data SET lock=1 WHERE id='$ID'"); $rsth
+->execute() || die $rsth->errstr;
$dbh->commit;
is there any cleaner way of doing this? I need to "...FOR UPDATE" then get the ID of selected row to set a lock on it. My code works but is very ugly. Thank you
Hello fellow monks,
I've been Perl'ing for a long time and maybe I just haven't consumed sufficient caffeine today, but... I am at a loss.
I have two hashes:
%A = (a => 1, b => 2, c => 3);
%Z = (z => 9, y => 8, x => 7);
that works fine but it's a little extra (and I know my code is soon going to inflicted on a junior dev, so...) and anyway it seems like I ought to be able to do something simpler.
So where it blows up is when I try to do:
Experimental keys on scalar is now forbidden at - line 3. Type of arg 1 to keys must be hash or array (not list) at - line 3, near "%Z)" Execution of - aborted due to compilation errors.
There isn't a scalar in sight, the type of arg1 is definitely a hash, and I am trying to figure out what is going on. I would have expected this just to function as a hash overlay, with the results passed to keys(), wham, bam, eat some jam.
OK, maybe keys() is doing something fancy to its args? Functions like pop() know that the first arg is a real array, so maybe keys() is treating this as two args and not doing the overlay (and I've never passed more than one arg to keys()---why would I?---but my various attempts to fool the compiler clarify that the overlay should happen don't work either:
...not that the last one is "cleaner" that the version above that works
All generate the same error as above, and in fact none but the first work even if I take out the overlay part:
Which is to say, scalar returns a PV if the array is empty, and an IV if it is not. This can be a problem, because most serializers, including JSON modules, will encode the PV as "0" and the IV as 1, which will cause a problem if the consumer of the resulting JSON expects strict type conformance.
Is there a logical reason for this discrepancy?
(Yes, I know that I could just do 0+scalar @array - my point is that that shouldn't be necessary.)
With CGI.pm you can avoid using query string parameters listed in the URL with a GET request by checking $cgi->request_method() eq 'POST', but is there a way to keep those GET parameters from populating $cgi->param(), other than hacking the module?
I'm creating user-defined custom fields in Maria DB using the JSON Data Type. I've never had cause to use this data type before so I've been playing learning by running queries directly against a test database to get the syntax right. I thought I'd got this to the point where I can use it and imagined that there could be some issues with getting the DBI placeholders right.
$row->{'inuse'} = $dbh->selectrow_array("SELECT COUNT(*) FROM Person W
+HERE Account_idAccount = ? AND JSON_EXISTS(custom, ?)", undef, $accou
+nt, "$." . $row->{'name'});
But this didn't work. I assumed it was a placeholder issue.
However, I slowly removed the placeholders to test where things were going wrong. To the point where I arrived at this with no placeholders...
$row->{'inuse'} = $dbh->selectrow_array("SELECT COUNT(*) FROM Person W
+HERE Account_idAccount = 35 AND JSON_EXISTS(custom, '$.test3')");
die $dbh->errstr if $dbh->err;
die $row->{'inuse'} if $row->{'inuse'};
Snippets of code should be wrapped in
<code> tags not<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).