Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Seekers of Perl Wisdom

( [id://479]=superdoc: print w/replies, xml ) Need Help??

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.

Post a new question!

User Questions
Calling a sub without enclosing its argument inside brackets
2 direct replies — Read more / Contribute
by syphilis
on Mar 18, 2024 at 00:05
    Hi,

    The demo:
    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
    Is there some technique that will allow me to make that (failing) call work - without having to place that argument in brackets ?

    Cheers,
    Rob
The Y2K 2038 problem
2 direct replies — Read more / Contribute
by harangzsolt33
on Mar 17, 2024 at 21:05
CRC-16/X25 problem
3 direct replies — Read more / Contribute
by bonzi
on Mar 16, 2024 at 15:25

    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:

    use Digest::CRC; my ($crc, $input, $width, $init, $xorout, $refout, $poly, $refin, $con +t, $digest, $ctx); $width = 16; $init = 0xffff; $xorout = 0xffff; $refout = 1; $poly = 0x1021; $refin = 1; $cont = 0; # ??? $input = 0xaabbccdd; $ctx = Digest::CRC->new(width=>$width, init=>$init, xorout=>$xorout, refout=>$refout, poly=>$poly, refin=>$refin, cont=>$cont); $ctx->add($input); $digest = $ctx->hexdigest; say $digest;
    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.
Most Significant Set Bit
11 direct replies — Read more / Contribute
by coldr3ality
on Mar 15, 2024 at 09:19
    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?
    $m is assigned the most significant set bit of 64-bit integer $z, for +the cost of 6 ANDs and 6 truth tests. my $m=-4294967296&$z? -281474976710656&$z? -72057594037927936&$z? -1152921504606846976&$z? -4611686018427387904&$z? -9223372036854775 +808&$z? 4611686018427387904: 2305843009213693952 : -2305843009213693952&$z? 1152921504606846976: 576460752303423488 : -288230376151711744&$z? -576460752303423488&$z? 288230376151711744: 144115188075855872 : -144115188075855872&$z? 72057594037927936: 36028797018963968 : -4503599627370496&$z? -18014398509481984&$z? -36028797018963968& +$z? 18014398509481984: 9007199254740992 : -9007199254740992&$z? 4503599627370496: 2251799813685248 : -1125899906842624&$z? -2251799813685248&$z? 1125899906842624: 562949953421312 : -562949953421312&$z? 281474976710656: 140737488355328 : -1099511627776&$z? -17592186044416&$z? -70368744177664&$z? -1407374 +88355328&$z? 70368744177664: 35184372088832 : -35184372088832&$z? 17592186044416: 8796093022208 : -4398046511104&$z? -8796093022208&$z? 4398046511104: 2199023255552 : -2199023255552&$z? 1099511627776: 549755813888 : -68719476736&$z? -274877906944&$z? -549755813888&$z? 274877906944: 137438953472 : -137438953472&$z? 68719476736: 34359738368 : -17179869184&$z? -34359738368&$z? 17179869184: 8589934592 : -8589934592&$z? 4294967296: 2147483648 : -65536&$z? -16777216&$z? -268435456&$z? -1073741824&$z? -2147483648& +$z? 1073741824: 536870912 : -536870912&$z? 268435456: 134217728 : -67108864&$z? -134217728&$z? 67108864: 33554432 : -33554432&$z? 16777216: 8388608 : -1048576&$z? -4194304&$z? -8388608&$z? 4194304: 2097152 : -2097152&$z? 1048576: 524288 : -262144&$z? -524288&$z? 262144: 131072 : -131072&$z? 65536: 32768 : -256&$z? -4096&$z? -16384&$z? -32768&$z? 16384: 8192 : -8192&$z? 4096: 2048 : -1024&$z? -2048&$z? 1024: 512 : -512&$z? 256: 128 : -16&$z? -64&$z? -128&$z? 64: 32 : -32&$z? 16: 8 : -4&$z? -8&$z? 4: 2 : 2 ;
    EDIT 2024/3/25: Updating for completeness.

    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.

    my $z=17; my $m; { no warnings; ## to suppress warning ## "Hexadecimal number > 0xffffffff non-portable" $m= 0xFFFFFFFF80000000&$z? 0xFFFF800000000000&$z? 0xFF80000000000000&$z? 0xF800000000000000&$z? 0xE000000000000000&$z? 0xC000000000000000&$z? 0x8000000000000000&$z? 0 : 1: 2 : 0xF000000000000000&$z? 3: 4 : 0xFE00000000000000&$z? 0xFC00000000000000&$z? 5: 6 : 0xFF00000000000000&$z? 7: 8 : 0xFFF8000000000000&$z? 0xFFE0000000000000&$z? 0xFFC0000000000000&$z? 9: 10 : 0xFFF0000000000000&$z? 11: 12 : 0xFFFE000000000000&$z? 0xFFFC000000000000&$z? 13: 14 : 0xFFFF000000000000&$z? 15: 16 : 0xFFFFFF8000000000&$z? 0xFFFFF80000000000&$z? 0xFFFFE00000000000&$z? 0xFFFFC00000000000&$z? 17: 18 : 0xFFFFF00000000000&$z? 19: 20 : 0xFFFFFE0000000000&$z? 0xFFFFFC0000000000&$z? 21: 22 : 0xFFFFFF0000000000&$z? 23: 24 : 0xFFFFFFF800000000&$z? 0xFFFFFFE000000000&$z? 0xFFFFFFC000000000&$z? 25: 26 : 0xFFFFFFF000000000&$z? 27: 28 : 0xFFFFFFFE00000000&$z? 0xFFFFFFFC00000000&$z? 29: 30 : 0xFFFFFFFF00000000&$z? 31: 32 : 0xFFFFFFFFFFFF8000&$z? 0xFFFFFFFFFF800000&$z? 0xFFFFFFFFF8000000&$z? 0xFFFFFFFFE0000000&$z? 0xFFFFFFFFC0000000&$z? 33: 34 : 0xFFFFFFFFF0000000&$z? 35: 36 : 0xFFFFFFFFFE000000&$z? 0xFFFFFFFFFC000000&$z? 37: 38 : 0xFFFFFFFFFF000000&$z? 39: 40 : 0xFFFFFFFFFFF80000&$z? 0xFFFFFFFFFFE00000&$z? 0xFFFFFFFFFFC00000&$z? 41: 42 : 0xFFFFFFFFFFF00000&$z? 43: 44 : 0xFFFFFFFFFFFE0000&$z? 0xFFFFFFFFFFFC0000&$z? 45: 46 : 0xFFFFFFFFFFFF0000&$z? 47: 48 : 0xFFFFFFFFFFFFFF80&$z? 0xFFFFFFFFFFFFF800&$z? 0xFFFFFFFFFFFFE000&$z? 0xFFFFFFFFFFFFC000&$z? 49: 50 : 0xFFFFFFFFFFFFF000&$z? 51: 52 : 0xFFFFFFFFFFFFFE00&$z? 0xFFFFFFFFFFFFFC00&$z? 53: 54 : 0xFFFFFFFFFFFFFF00&$z? 55: 56 : 0xFFFFFFFFFFFFFFF8&$z? 0xFFFFFFFFFFFFFFE0&$z? 0xFFFFFFFFFFFFFFC0&$z? 57: 58 : 0xFFFFFFFFFFFFFFF0&$z? 59: 60 : 0xFFFFFFFFFFFFFFFE&$z? 0xFFFFFFFFFFFFFFFC&$z? 61: 62 : 0xFFFFFFFFFFFFFFFF&$z? 63: 64; }


    32-bit version:
    my $m=0xFFFF8000&$z? 0xFF800000&$z? 0xF8000000&$z? 0xE0000000&$z? 0xC0000000&$z? 0x80000000&$z? 0 : 1: 2 : 0xF0000000&$z? 3: 4 : 0xFE000000&$z? 0xFC000000&$z? 5: 6 : 0xFF000000&$z? 7: 8 : 0xFFF80000&$z? 0xFFE00000&$z? 0xFFC00000&$z? 9: 10 : 0xFFF00000&$z? 11: 12 : 0xFFFE0000&$z? 0xFFFC0000&$z? 13: 14 : 0xFFFF0000&$z? 15: 16 : 0xFFFFFF80&$z? 0xFFFFF800&$z? 0xFFFFE000&$z? 0xFFFFC000&$z? 17: 18 : 0xFFFFF000&$z? 19: 20 : 0xFFFFFE00&$z? 0xFFFFFC00&$z? 21: 22 : 0xFFFFFF00&$z? 23: 24 : 0xFFFFFFF8&$z? 0xFFFFFFE0&$z? 0xFFFFFFC0&$z? 25: 26 : 0xFFFFFFF0&$z? 27: 28 : 0xFFFFFFFE&$z? 0xFFFFFFFC&$z? 29: 30 : 0xFFFFFFFF&$z? 31: 32;


    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.

How can I extract an email behind a JS button?
2 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 14, 2024 at 18:46

    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 wrote a very ugly piece of code, can someone help me correct it?
1 direct reply — Read more / Contribute
by glwa
on Mar 14, 2024 at 15:09
    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
Problem combining hashes
3 direct replies — Read more / Contribute
by jh
on Mar 14, 2024 at 12:20
    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);
    and I want to populate an array with all the keys in either. It works great if I do it as two steps:
    %combined = (%A, %Z); @all_keys = keys %combined;
    No problem. But I want to do it in one step:
    @all_keys = keys %{{ %A, %Z }};
    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:
    @all_keys = keys(%A, %Z);
    I get

    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:
    @all_keys = keys((%A, %Z)); @all_keys = keys(%temp::var = (%A, %Z)); @all_keys = keys(my %temp = (%A, %Z)); @all_keys = keys(do { %{{%A, %Z}} });
    ...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:
    @all_keys = keys(%temp::var = (%A)); @all_keys = keys(my %temp = (%A)); @all_keys = keys(do { %{{%A}} });
    What is going on?
Reason for this discrepancy with scalar?
3 direct replies — Read more / Contribute
by kikuchiyo
on Mar 13, 2024 at 16:31

    Consider the following program:

    perl -MDevel::Peek -le 'my @nonempty = (1); my @empty = (); print Dump +(scalar @nonempty); print Dump(scalar @empty)' SV = IV(0x563dfe2d16f0) at 0x563dfe2d1700 REFCNT = 1 FLAGS = (TEMP,IOK,pIOK) IV = 1 SV = PVNV(0x563dfe2d0200) at 0x563dfe2ce410 REFCNT = 2147483647 FLAGS = (PADTMP,IOK,NOK,POK,READONLY,PROTECT,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x7fb0f957d3c2 "0" CUR = 1 LEN = 0

    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.)

CGI.pm ignore URL GET query string parameters
1 direct reply — Read more / Contribute
by Danny
on Mar 13, 2024 at 15:13
    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?
DBI and JSON fields
2 direct replies — Read more / Contribute
by Bod
on Mar 12, 2024 at 08:28

    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.

    But I seem to have hit a more tricky issue...

    I started with this DBI query:

    $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'};
    The code doesn't die.

    But, if I run the same query directly against the same database:

    SELECT COUNT(*) FROM Person WHERE Account_idAccount = 35 AND JSON_EXIS +TS(custom, '$.test3')
    I get the result 1

    Is there a problem using the MariaDB JSON Data Type with DBI or have I missed something else obvious?


Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":


  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • 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).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found