Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Seekers of Perl Wisdom

( #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
Errors uncaught by CGI::Carp
2 direct replies — Read more / Contribute
by Bod
on Oct 13, 2021 at 16:46

    This is a follow on from the second part of the question I asked in CGI::Carp fatalsToFile
    The collective wisdom of The Monastery solved the first issue but not the seems they are unrelated!

    I am getting 500 errors intermittently from most, if not all of the pages of a website. The errors seems to be clumped together over time. In other words, once an error happens it seems likely that it will happen again quite soon. Whereas at other times we go quite a while without errors.

    Every script has use CGI::Carp qw(fatalsToBrowser); at the start of the script.
    The bit I am most confused about is that my understanding is that, with fatalsToBrowser the only two ways to get a 500 error is that the script has the wrong permissions or that the script doesn't return valid HTTP headers. As this is an intermittent problem, it is neither of these.

    This is the code of the modules that are common :

    package Site::HTML; use strict; use warnings; use DBI; #use DBD::mysql; use Exporter; use Template; use MIME::Lite; use Digest::SHA qw(sha256_base64); use cPanelUserConfig; use Site::Variables; use Bod::CRM; our @ISA = qw(Exporter); our @EXPORT = qw(%data %file %cookie $dbh $current_user); our $site = $Site::Variables::site; our $template = Template->new( INCLUDE_PATH => $Site::Variables::template_path, PRE_CHOMP => 0, POST_CHOMP => 1, ); our (%data, %file, %cookie, $dbh); our $current_user = 0; my $crm = Bod::CRM->new($Site::Variables::env_crm_env); # Untaint ENV{PATH} for MIME::Lite my $path = $ENV{'PATH'}; $ENV{'PATH'} = undef; foreach my $p(split /:/, $path) { if ($p =~ m!^(/(usr|bin).*)!) { $ENV{'PATH'} .= ':' if $ENV{'PATH'}; $ENV{'PATH'} .= $1; } }
    package Site::Wayfinder; use strict; use warnings; use cPanelUserConfig; use Digest::SHA qw(sha512_base64); use Facebook::Graph; use GD; use Bod::CRM; use Site::HTML; use Site::Point; my $crm = Bod::CRM->new(Site::HTML->crm_env); sub new { my $class = shift; my %attrs = @_; $attrs{'error'} = ''; $attrs{'fb'} = Facebook::Graph->new( app_id => '12345678', secret => 'abcdefghijklmnop', postback => "https://$ENV{'HTTP_HOST'}/?command=authori +ze_facebook", ); return bless \%attrs, $class; }
    I have commented out use DBD::mysql; because on one occasion I got some output from CGI::Carp that showed an error on that line. I have not been able to replicate this error. Not specifying a DBD driver forces DBI::DBD to use the driver in it's config which is setup to connect to the MariaDB database.

    My only hunch is that, because errors seem more concentrated at certain times, it is connected with the number of concurrent users. This in turn makes me wonder if is is Facebook::Graph that is the problem as it is deprecated. We only use Facebook for authentication. Users can create an account using Facebook rather than creating a password or can sign into an existing account using Facebook provided the email addresses are the same. This works under normal circumstances.

    It isn't just web browsers that are getting the error. Facebook is complaining that our privacy policy page is giving errors and Stripe gets occasion webhook errors.

    I am unable to replicate the problem on our test site - it only seems to happen on the live production site. Therefore I am limited on how much I can test or what I can tweak!

    Any ideas what I can do to try and get some understanding of what is going on?

    UPDATE: - to test my hunch that it is to do with multiple users, I have tried to cross referencer 500 errors with the data collected by Google Analytics and Hotjar. But the errors are not written to the Apache error log so this is proving difficult. I have started having Google Analytics open when I access the site so that I can look at the real time data when I next see an error.

iterating over time with DateTime to obtain values with Astro::Coords
5 direct replies — Read more / Contribute
by Aldebaran
on Oct 12, 2021 at 20:53

    Been a while since I have posted as OP with the Perlpocalypse and all. I took a tour "out there" to see what else was going on. I worked a bit with python, but, as a guy who started comp sci in the punchcard era, I can't look at fixed form source and call it "progress." (Do they have pythontidy like we have perltidy?)

    I also took a gander at Usenet. It was nice to see some old names, but it has the same cooties of spammers and pornographers that mutate faster than one's killfile. comp.unix.programmer was still viable, as well as comp.lang.fortran, but I couldn't figure out the proprietary client the provider wanted me to use, so that fizzled out. (That's a nice way of saying I got locked out of my own account with failed attempts.)

    I've had several scripts that I've been fiddling with in the meantime. This one came up in the context of Moon phase on historical events. I present attempts to iterate over the closed interval of a lunar cycle within the methodology of DateTime. It's not complete, but I've got output I don't understand, so I want to put it up for review. Let me just get source out there first:

    $et is to be the second at which the moon and venus conjoin, ie, have the same right ascension. The problem is that I check the data with John Walker's Fourmilab ephemeris and use the option for univeral time. The time defined in perl thus:

    my $isostr = '2021-10-09T11:13:57';

    is inputed without the T, and with the radio button activated, thus:

    2021-10-09 11:13:57

    But that's not our value to check, which should be $et, namely:

    2021-10-09 20:25:46

    It would seem that the moon is well past Venus by the margin of almost 2 hours. I tried to spitball if it might be using a different idea of coincidence with non-point masses. The full moon is about a half a degree. It's certainly not half full, and the dark edge hits venus first. In the prelims, I calculated that angle the moon moves through in a day to be 12.2 degrees. A quarter of a degree takes the moon about a half hour. Our answer seems to be at least 3 times out of this range as fourmilab has the conjunction between this interval, I:

    2021-10-09 18:30:00

    And this time:

    2021-10-09 18:40:00

    Q1) Do these bodies conjoin at $et or in the interval I? How does perl represent the moon in Astro::Coords? (For example, is the right ascension that of the center?}

    I didn't expect $phases[0] to be negative. Given that it was defined by

    my @phases = phasehunt($dt);

    Q2) How do I take $dt and $phases[0] and get epoch time?

    Q3) How do I get the duration of the program run to display correctly? I thought I followed Dave Rolsky's example.

    I wanted to be able to calculate the duration of the transit of the moon from venus to jupiter before it happened.

    Thanks for comments,

Seeking help with HTTPS form upload.
3 direct replies — Read more / Contribute
by FeistyLemur
on Oct 12, 2021 at 18:16

    >Greetings, I am attempting to reconstruct a post (using WWW::Mechanize and LWP::UserAgent so far) in order to upload a file and navigating some things I'm unfamiliar with. All my reading into examples has led me to the following code. I've truncated to the relevant section. I am having no issues with my first request to Authenticate and populate my $auth_key value with a previous request.

    #!/usr/bin/perl -w use strict; use IO::Socket::SSL qw(); use LWP::UserAgent; my $url_fw_upload=' +xxxxxxxxxxxxx'; my $ua=LWP::UserAgent->new(ssl_opts => { SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, verify_hostname => 0, }); $response=$ua->post($url_fw_upload, Authorization=>"Bearer $auth_key", Content_Type=>'form-data', Content => [ "file" =>['firmware.img' => '/home/user/firmware.img'], ], );

    So the thing which is new to me is "fwLoad?X-Progress-ID", which is a Javascript generated UUID for file upload progress. I've used the same UUID that I grabbed from a browser debug session, within that browser debug I also have after the Request headers and before the Request Payload "Query String Parameters" with "X-Progress-ID: xxxxxxxxxxxxx". Since I have no Javascript capability here I'm trying to get around this and not sure if I even can without full Javascript functionality.

    The results are always the same, 400 bad request. Any suggestions to point me in the right direction would be welcome as I've come up blank in my searches so far.

Akamai authentication for IMAP.
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 12, 2021 at 13:36

    Hi Monks,

    How Akamai authentication can be implemented using IMAP::Client (maybe) which would authentication both IMAP creds and Akamai creds (two-factor authentication),so that mails would be processed as expected.
    Currently we have $imap->user and $imap->password methods used to authenticate user, how to add additional layer of authentication ?

    Any help regarding this would be appreciated.
    Thank you.

XLSX::Chart::Pie -> Exploding
2 direct replies — Read more / Contribute
by sectokia
on Oct 11, 2021 at 20:41


    Using Excel::Writer::XLSX::Chart::Pie, does anyone know how to explode a pie chart (to create a gap between the pie slices, ala excels setting: 'Format Data Series' -> 'Pie Explosion')? I had a look through the docs but couldn't see anything.


DNS lookup and specify DNS server to use
2 direct replies — Read more / Contribute
by g_speran
on Oct 11, 2021 at 12:32
    currently I am using gethostbyname and gethostbyaddr. However I am looking for a way to specify the specific DNS server to validate against much like the Windows
    "nslookup IP dns server"
    format and dont believe that this can be done using the gethostbyname and/or gethostbyaddr. Any recommendations on how to achieve this?
Bareword issue with Perl 5.34
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 11, 2021 at 10:52

    Hello monks

    Loading the following does not produce any issue with Perl 5.28.

    use strict; use warnings; $Win32::OLE::CP = CP_UTF8;

    It produces issues however with Perl 5.34:

    Bareword "CP_UTF8" not allowed while "strict subs" in use at l +ine 3. Execution of aborted due to compilation errors.

    Changing $Win32::OLE::CP = CP_UTF8; to $Win32::OLE::CP = 'CP_UTF8'; avoid Perl complaining, however the feature is no more loaded. What is the best approach to solve this?

count backrefenence regex
4 direct replies — Read more / Contribute
by tmolosh
on Oct 10, 2021 at 16:15

    hi all - i have a workaround for this but want to understand why my 1st approach did not work. This relates to reporting on "direct repeats" in DNA|RNA sequences.

    here is a command line example to generate a 1,000,000 sequence and try to find direct repeats of length > 3

    perl -e '@a=qw(g a t c g a t c g a t c); for(1..1000000){$x=int(1000*r +and());$s.=$a[$x]} while($s=~/(?<repeat>\w{3,})\w*\g{repeat}/g){ $+{r +epeat};$c=()=$s=~/$+{repeat}/g;print "$+{repeat} : $c\n"}' agtcg : 0 agtcg : 0 agtcg : 0 ^C

    if i remove the //g, it reports a count of 1 but no substring

    perl -e '@a=qw(g a t c g a t c g a t c); for(1..1000000){$x=int(1000*r +and());$s.=$a[$x]} while($s=~/(?<repeat>\w{3,})\w*\g{repeat}/g){ $+{r +epeat};$c=()=$s=~/$+{repeat}/;print "$+{repeat} : $c\n"}' : 1 : 1 : 1 : 1

    if i 1st assign $+{repeat} to a variable, i get the substring, but without //g, the count is still (as expected since it is not global)

    perl -e '@a=qw(g a t c g a t c g a t c); for(1..1000000){$x=int(1000*r +and());$s.=$a[$x]} while($s=~/(?<repeat>\w{3,})\w*\g{repeat}/g){ $m=$ ++{repeat};$c=()=$s=~/$m/;print "$m : $c\n"}' gcacgt : 1 cga : 1 tggg : 1 ttg : 1 tta : 1 cct : 1

    adding //g back gets back to the non-functional state:

    perl -e '@a=qw(g a t c g a t c g a t c); for(1..1000000){$x=int(1000*r +and());$s.=$a[$x]} while($s=~/(?<repeat>\w{3,})\w*\g{repeat}/g){ $m=$ ++{repeat};$c=()=$s=~/$m/g;print "$m : $c\n"}' gctgca : 0 gctgca : 0 gctgca : 0 ^C

    any explanation so i can better understand regexs would be greatly appreciated


OO Pattern Container x Elements and Method Chaining
4 direct replies — Read more / Contribute
by LanX
on Oct 08, 2021 at 11:07
    Update: added a more intuitive example here

    Lets say we have two classes Container and Element

    and you can

    • add Elements to a Container
    • extract them
    • apply method chaining
    my $cont = Container->new(); my $elem = Element->new('name'); $cont->add_elem($elem); # method chaining $cont1->get_elem('name')->do_something(); # identical to $elem1 = $cont1->get_elem('name'); $elem1->do_something(); # *

    For this to work does $elem2 obviously need to know that it belongs to $container.

    So it's N->1 : N Element -> 1 Container

    and $elem1 == $elem

    with updated property $elem->{member_of}=$cont Now after using this for a long time new requirements arise and Elements need to belong to multiple Containers.

    So now it's N->M : N Element -> M Container

    Clearly the old model with $elem2 == $elem doesn't work anymore because

    $elem2 = $cont2->get_elem('name'); can't be member_of two different containers to allow method chaining

    I don't think that $elem1 and $elem2 should belong to class Element either, but to a "wrapper" class ContainerElement referencing $elem, i.e. $elem1->{master}=$elem

    Then I could think of many solutions, some involving inheritance, some AUTOLOAD to make sure that

    $elem1->do_something(...) always does $elem1->{master}->do_something(..)

    without hardcoding all methods.

    I don't wanna reinvent the wheel and I'm already starting to worry too much about performance so here the question ...

    What are the usual OO-Patterns to solve this? :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery


    *) OK Sorry, do_something() is doing something with the relation $cont <- $elem like set_weight($cont,$elem)

Is there a Perl API (XS) for ->can ?
2 direct replies — Read more / Contribute
on Oct 08, 2021 at 02:17
    Hi, in one of my XS modules I want to do the equivalent of PKG->can($method). It feels like this would be in the perl api, but keyword searching for 'can' is somewhat futile. Is there any way to run this operation more efficiently than literally building a call to method 'can'?

Add your question
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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2021-10-17 08:42 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (71 votes). Check out past polls.