Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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
Lexical variable not available
3 direct replies — Read more / Contribute
by choroba
on Mar 18, 2024 at 17:06
    Copied from one of the Perl Discords:

    By user "timka7060":

    Who can figure this out. I narrowed down my issue in some other code to this simple use case:
    # Can print $v only once!!! # Second call returns undef. # Some coderef optimization??? # Lexical variable goes undef on second call. # sub { my $v = 111; eval q( eval 'print $v, "\n"'; # 111 eval 'print $v, "\n"'; # undef ); }->();

    User "tyrrminal" replies with:

    If you enable warnings, you see a line Variable "$v" is not available at (eval 3) line 1. Googling that led me to a stack overflow response which seems to relate to your case (2nd answer, starting with See perldiag)

    timka7060's reaction:

    So why does the first eval output 111?
    (I'd figure either both lines should be 111 or both undef.)
    Btw, prior to 5.10, both lines would output 111: https://perlbanjo.com/b28dce8db2
    This is a simple fix, but only for a single variable: https://perlbanjo.com/13dbb8d48d

    My observation:

    sub { my $v = 111; eval q( eval 'print $v, "A\n"'; eval 'print $v, "B\n"'; print $v, "C\n"; ); }->();
    Now, none of either A or B sees $v, but C (obviously) does.

    What's going on here?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
MTA for Perl
3 direct replies — Read more / Contribute
by Bod
on Mar 18, 2024 at 16:51

    I need to install a Mail Transfer Agent that will be used with MIME::Lite

    I understand that the modern choices are Postfix or Exim. However, MIME::Lite says it uses sendmail which is pretty old isn't it?

    I have never looked at MTA's before so any advice would be appreciated. This is being installed on a RaspberryPi. Does the MTA only deal with outgoing mail or will it manage incoming mail as well? If not, what do I need for that?

Common subs and Global Variables
2 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 18, 2024 at 15:56
    In the course of writing a bunch of perl code for my firm, I noticed that some subs were used over an over and some variables were most naturally implemented as global variables.

    After some experimentation I hit upon a method of making this happen. Let "program.pl" be one of the perl programs which needs to use (a) one of these common subs and (b) needs to share some variables with this sub.

    Context: This is in Windows 10 (ugh) using Strawberry perl v5.20.2.

    I construct a file "includee.pm" which looks like this:

    use vars qw { $SHAREDVARIABLE1 @SHAREDVARIABLE2 %SHAREDVARIABLE3 ... } sub function { ... }
    Then in program.pl I put at the top this line:

    use includee
    Both program.pl and includee.pm are in the same folder, which is also the current folder when the program runs.

    This actually produces the desired effect: the shared variables are accessible in both progam.pl and includee.pm, and the sub function is callable from program.pl.

    The question is: is this legitimate? Am I depending on a quirk, which might go away in a future version of perl? In particular, will this method work in a linux context also?

Exact definition of DateCalc and Delta_Format functions
1 direct reply — Read more / Contribute
by zatlas1
on Mar 18, 2024 at 14:20
    I am looking for the exact definition of DateCalc and Delta_Format functions in Date::Manip and I seem not to be able to find those definitions. Could somebody please point me to those definitions. Thank you ZA
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
1 direct reply — 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
8 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 ;
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

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 chanting in the Monastery: (4)
As of 2024-03-19 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found