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
[OT] github: testing PRs
2 direct replies — Read more / Contribute
by syphilis
on May 11, 2021 at 00:56

    I assume this must be a simple procedure.
    I want to obtain the same perl source code that CI ran in its automated testing of https://github.com/Perl/perl5/pull/18783.
    What is the simplest way for me to obtain that source ?

    Update: I probably should mention that I do already have a fully functional git utility.

Need advice on AD authentication confirguration in Dancer2 app.
1 direct reply — Read more / Contribute
by chandantul
on May 10, 2021 at 23:39

    Hello Smart Monks, I will need your help in AD authentication configuration in app.psgi file. I was able to conifugure simple authentication but not able to configure AD authentication. Please check my below code that is not not leeting me start the app.psgi.

    # use this block if you don't need middleware, and only have a single +target Dancer app to run here use webapp; use Plack::Builder; use Authen::Simple::ActiveDirectory; my $ad = Authen::Simple::ActiveDirectory->new( host => 'ldaps//ADDEV.dev.abc.com', principal => 'abc.com' ); builder { enable "Auth::Basic", authenticator => $ad; webapp->to_app; }; my($username, $password, $env) = @_; if ( $ad->authenticate( $username, $password ) ) { # successfull authentication } else { die "Wrong ID and Password\n" }

    Please check below error and help me to identify the issue if possible

    :Simple::ActiveDirectory::authenticate was an 'undef', which is not on +e of the allowed types: scalar at C:/Strawberry/perl/site/lib/Authen/Simple/Adapter.pm line 42, <DAT +A> line 755. Authen::Simple::Adapter::authenticate(undef, undef, undef) cal +led at C:\Users\Documents\Perl\webapp\bin\app.psgi line 26 require C:\Users\Documents\Perl\webapp\bin\app.psgi called at +(eval 6) line 3 eval 'package Plack::Sandbox::C_3a_5cUsers_5c503004671_5cDocum +ents_5cPerl_5cSynchrony_2dPerl_5cwebapp_5cbin_5capp_2epsgi; { my $app = do $_file; if ( !$app && ( my $error = $@ || $! )) { die $error; } $app; } ' called at C:/Strawberry/perl/site/lib/Plack/Util.pm line 120 Plack::Util::_load_sandbox("C:\\Users\\Documents\\Perl\\webapp +\\bin\\a"...) called at C:/Strawberry/perl/site/lib/Plack/Util.pm lin +e 136 Plack::Util::load_psgi("app.psgi") called at C:/Strawberry/per +l/site/lib/Plack/Runner.pm line 181 Plack::Runner::__ANON__() called at C:/Strawberry/perl/site/li +b/Plack/Runner.pm line 24 Plack::Runner::__ANON__() called at C:/Strawberry/perl/site/li +b/Plack/Runner.pm line 24 Plack::Runner::__ANON__() called at C:/Strawberry/perl/site/li +b/Plack/Runner.pm line 24 Plack::Runner::__ANON__() called at C:/Strawberry/perl/site/li +b/Plack/Runner.pm line 24 Plack::Runner::__ANON__() called at C:/Strawberry/perl/site/li +b/Plack/Loader.pm line 55 Plack::Loader::preload_app(Plack::Loader=HASH(0x65aa50), CODE( +0x2ca34c0)) called at C:/Strawberry/perl/site/lib/Plack/Runner.pm lin +e 276 Plack::Runner::run(Plack::Runner=HASH(0x11aa248)) called at C: +\Strawberry\perl\site\bin/plackup line 7
I can't subtract one DateTime object from another to get a DateTime::Duration in days
3 direct replies — Read more / Contribute
by Cody Fendant
on May 10, 2021 at 18:17

    Oh the shame: my date objects weren't actually differing by more than one day because I'm an idiot and assumed that my inputs were what I thought they were! Thanks commenters for forcing me to go back and look at exactly what was in the objects. I leave this here as a memorial, or it can be reaped.

    This is a bug in an application I'm working on.

    The code creates two DateTime objects and subtracts one from the other to get a DateTime::Duration object.

    However the DateTime::Duration object can't return a difference in number of days, which is what I need.

    This is actually expected behaviour:

    my $dur = DateTime::Duration->new( years => 1, months => 15 ); $dur->in_units('years'); # 2 $dur->in_units('months'); # 27 $dur->in_units( 'years', 'months' ); # (2, 3) $dur->in_units( 'weeks', 'days' ); # (0, 0) !
    The last example demonstrates that there will not be any conversion between units which don't have a fixed conversion rate.

    I guess my question is, how do I get the difference in days between two DateTime objects?

    Updated: The current code just does

    my $diff = $dt1 - $dt2;
    Where both DateTime objects are specific fixed dates, like one is January 1, 2021 at 10 AM and the other is today at 11 AM. Surely there's a 'fixed conversion rate' in days between those? Note: They were essentially the same day!

    I can understand that if I create a duration object with years => 1, months => 15 that's not something where we can be sure of the number of days, but that's not what's happening in my code.

Combinations with variable length
6 direct replies — Read more / Contribute
by TryingTheBest
on May 10, 2021 at 16:54

    Hi everyone.

    I am working with a combination script that is giving me problems.

    This is as below:

    I have two static numbers: 1 and 2.

    And also I have a non-static number that represent the size of the combinations.

    Example: 3 (combinations of 3 characters).

    I need to perform ALL combinations using the static number, but considering the non-static number.

    In the example with "3" as non-static number, this will be:

    1 1 1
    1 1 2
    1 2 1
    1 2 2
    2 1 1
    2 1 2
    2 2 1
    2 2 2

    ( I think I don't forget any combination :P )

    Someone can help me with the code, please? I tried using use Algorithm::Combinatorics qw( combinations ); but I could not solve it.

    Thanks all!

Perl5.26 installing Tk module manually
3 direct replies — Read more / Contribute
by vinoth.ree
on May 10, 2021 at 04:04

    I have installed perl 5.26 on windows10 and trying to install Tk module manually, since my application uses Tk module, but getting below error.

    C:\Users\VinothG\Desktop\Tk-804.036>perl Makefile.PL Cannot find 'C:\TEMP\ActiveState--------please-run-the-install-script- +-------\lib/CORE/perl.h' have you installed C:\Perl64\bin\perl.exe? Compilation failed in require at Makefile.PL line 37. BEGIN failed--compilation aborted at Makefile.PL line 39.

    Could someone help me to fix this error, Am I doing something wrong here?

    All is well. I learn by answering your questions...
Counting characters within regexp
5 direct replies — Read more / Contribute
by Bod
on May 08, 2021 at 18:15

    Good evening fellow Monks

    I've just been writing a bit of code to partially hide an email address such that the owner of the address would recognise it but anyone else would find it difficult, at least to work out. I tried a simple substitution but hit a problem. How can I determine how many characters there are and replace them with that same number of full stops or asterisks or whatever?

    I have come up with a three line solution but it seems like the sort of problem that should be able to be done with a regexp substitution

    use strict; use warnings; my @email = ( 'someone@example.co.uk', 'andrew.test@some.company.co.uk', 'jo@abc.com', ); foreach my $em(@email) { print "$em - "; my ($name, $comp) = split /@/, $em; $em =~ /^(\w[\w|\.]).*@(\w\w).*\.(\w+)$/; $em = $1 . '.' x (length($name) - 2) . "\@$2" . '.' x (length($com +p) - length($3) - 2) . $3; print "$em\n"; }
    This produces this result:
    C:\Users\joolz\Perl>perl regtest.pl someone@example.co.uk - so.....@ex.........uk andrew.test@some.company.co.uk - an.........@so..............uk jo@abc.com - jo@ab..com
    This is one of those problems that seems relatively trivial until you actually come to do it! It also suffers as a difficult one to search for as Google wants to tell me how to match a range of lengths {x,y} or count the number times one string occurs in another.

    Is there a nice, simple, succinct way to do this?

    I guess the nicest solution would change the number of characters properly displayed as a function of the overall length!

Attempting to use $_ in "sub" inside "map"
1 direct reply — Read more / Contribute
by NetWallah
on May 08, 2021 at 00:36
    I'm trying to automate building a dispatch table - or at least, to minimize keystrokes.

    Please suggest ways to achieve the desired result:

    use strict; use warnings; my %dispatch = ( normal => sub{"$_[0] Normal dispatch"}, map( {$_ => sub{"$_[0] Sub returns $_"} } qw| Uno Dos tres|), ); print $dispatch{"normal"}->(0),"\n"; # 0 Normal dispatch (as expected) + print $dispatch{"Uno"}->(1),"\n"; # WANT: "1 Sub returns Uno" print $dispatch{"Dos"}->(2),"\n"; # WANT: "2 Sub returns Dos"
    This currently prints:
    0 Normal dispatch Use of uninitialized value $_ in concatenation (.) or string at .\test +dispatch.pl line 5. 1 Sub returns Use of uninitialized value $_ in concatenation (.) or string at .\test +dispatch.pl line 5. 2 Sub returns
    Also - is there a NAME for what I am attempting ? "Early evaluation of $_" ?

                    "Avoid strange women and temporary variables."

PDF::API2 external objects
1 direct reply — Read more / Contribute
by dd-b
on May 07, 2021 at 23:40

    It appears that external objects (specifically, image files placed on the various pages) are not actually read until the pdf->save() call at the end. This may be problematic, in that I'm playing with a script that may generate a rather LARGE PDF containing more than 10,000 images.

    I'm wondering if anything like the $pdf->finishobjects(@obj) call might help; but the documentation doesn't really tell me very much about it!

    It does look like this module lets you insert pages in any order, and write on pages in any order, so maybe in fact nothing is really done until the end. It might not matter; I'm not actually that worried about the temp directory capacity on modern systems, and a BOTE calculation suggests the sum of the image files, at the size I'm currently proto-typing at, is a very few gigabytes. (Temp files are written out as a necessary step in re-scaling the files to sizes suitable to their appearance in the PDF; yes, using GD you can import an image file, re-scale it in memory, and put that directly into PDF::API2, without going through any temporary files, and it even works...but I was kind of shocked to discover that that was TWO ORDERS OF MAGNITUDE slower than using Image::Imlib2 to read, rescale, and write the image to a temp file and then reading it using the PDF::API2 method.)

    (As a side note--I'm kind of shocked how far I've gotten with this since starting to commit code this morning. PDF::API2, despite some documentation issues, which are frequently commented on, seems to be fairly easy to get somewhere with.)

Data::Dumper output
3 direct replies — Read more / Contribute
by Bod
on May 07, 2021 at 19:33

    Esteemed Monks,

    The LinkedIn API is returning an error Illegal character VCHAR=( and I am trying to resolve where the bracket is coming from as no brackets appear in the request headers or JSON body content. However, $request is ultimately passed to LWP::UserAgent->request. So I have tried:

    print Dumper $request; $VAR1 = 'HTTP::Request=HASH(0x1c23960) ';
    So I am wondering if this is where the bracket is coming from. Unlikely, but it has to be coming from somewhere...

    Seeing as Dumper is giving me a hash, I tried to dereference it like this:

    print Dumper %$request; $VAR1 = '4/8 ';
    I do not understand this.
    What does 4/8 mean? I cannot find any clues in the Data::Dumper documentation but I am struggling to understand it properly.

    Any advice on how I interpret 4/8 or how I can further work out where the bracket is coming from that is generating the error from the LinkedIn API?

4 direct replies — Read more / Contribute
by fmcroft92
on May 07, 2021 at 15:40

    Hi, Iím very new to Perl and programming generally. I have a real world problem whereby I want to convert a CSV file into an XML file. The structure of the xml file I need to create can be found here https://www.gov.uk/government/publications/co-ordinated-admissions-2021-series-17-files.

    Would anyone be able to give me any guidance on how I could go about this and where to start?

    Thank you.

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":