Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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. Post a new question!

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.

User Questions
Ternary Quizical behaviour?
6 direct replies — Read more / Contribute
by bliako
on Jul 10, 2020 at 07:35

    I have a possibly bad habit to compact boring parts of my code like this:

    if( exists($hash->{akey}) && defined($m=$hash->{akey}) && ($m==1) ){ $ +y = $m; ... } # don't trust $m here

    But in this case it has unexpected results. This is the 1st part where a hash is constructed based on whether a key in another hash exists:

    use strict; use warnings; use Data::Dumper; my %tests = ( 'a' => 10, 'b' => 20, ); my $m; # this seems to assign $m once and never bother to check again my %hash = ( 'b' => exists($tests{'b'}) && defined($m=$tests{'b'}) ? $m : 0, 'a' => exists($tests{'b'}) && defined($m=$tests{'a'}) ? $m : 0, ); print Dumper(\%hash); # this works as expected $m = 10; my %hash2 = ( '1' => $m++, '2' => $m++, '3' => $m++ ); print Dumper(\%hash2);
    $VAR1 = { 'a' => 10, 'b' => 10 }; $VAR1 = { '1' => 10, '2' => 11, '3' => 12 };

    Does anyone have an explanation? And is my habit bad?

1 direct reply — Read more / Contribute
by The_Dj
on Jul 10, 2020 at 00:21
    Greetings Monks!

    Does anyone have any experience in getting Perl to talk to EWS using ADFS authentication?

    My company's Microsoft exchange tech people are friendly but API programming is well beyond their expertise.

    We are migrating from Exchange server to Office365 and I need to migrate my automated mail-based systems. (send and receive)

    Thanks, all.
Net::DNS::Resolver using IPv6 transport in nameservers
1 direct reply — Read more / Contribute
by elsifsheep
on Jul 09, 2020 at 12:48
    I'm trying to use IPv6 addresses in Net::DNS::Resolver and it's not working. Help?
    %tld_v4_masters = ( "server1" => "", "server2" => "", "server3" => "", "server4" => "", ); %tld_v6_masters = ( "server1" => "2620:10a:aaaa::1", "server1" => "2620:10a:aaaa::2", "server1" => "2620:10a:aaaa::3", "server1" => "2620:10a:aaaa::4", ); ###################################################################### +#################### ### SUB: &verifymasters; - Verify and Determine which zone masters to +compare myself against ###################################################################### +#################### sub verifymasters { print "Running verifymasters...\n"; foreach $tryzm (values %$prizms) { print "[DEBUG(verifymasters)] Verifying ZM $tryzm with test candid +ate zone $testcandidate...\n" if ($debug); $res = new Net::DNS::Resolver; $res->tcp_timeout (1) ; $res->retry (1) ; $res->retrans (1) ; $query = $res->nameservers($tryzm); $query = $res->query($testcandidate, "SOA"); my $serial=($query->answer)[0]->serial, if ($query); if (length($serial) > 2) { print "[DEBUG(verifymasters)] Marking ZM $tryzm as GOOD!\n" if ( +$debug); $goodzms++; } else { print "[DEBUG(verifymasters)] Marking ZM $tryzm as BAD!\n" if ($ +debug); } } if ($goodzms < 4) { foreach $tryzm (values %$seczms) { print "[DEBUG(verifymasters)] Verifying ZM $tryzm with test cand +idate zone $testcandidate...\n" if ($debug); $res = new Net::DNS::Resolver; $res->tcp_timeout (1) ; $res->retry (1) ; $res->retrans (1) ; $query = $res->nameservers($tryzm); $query = $res->query($testcandidate, "SOA"); my $serial=($query->answer)[0]->serial, if ($query); if (length($serial) > 2) { print "[DEBUG(verifymasters)] Marking ZM $tryzm as GOOD!\n" if + ($debug); $goodzms++; } else { print "[DEBUG(verifymasters)] Marking ZM $tryzm as BAD!\n" if +($debug); } } } }
    I end up with the IPv4 checks being marked as GOOD, an the IPv6 tests being marked as BAD.
Perl::Critic says don't modify $_ in list functions and other things
8 direct replies — Read more / Contribute
by Lady_Aleena
on Jul 08, 2020 at 21:29

    Hello all. I have been playing around with Per::Critic on the command line and found to my dismay that some of my modules do not pass gentle. I realize that I can ignore the recommendations of Perl critic, but I would love for my modules to pass "gentle" before I begin ignoring things.

    The most common issue is that I modify $_ in list functions. The following is a convenient and short one liner to put the lines of files into a list. I thought doing it this way was nice and slim.

    my @list = map { chomp($_); $_ } <$fh>; my @uc_list = map { chomp $_; [uc $_] } <$lc_fh>; # used only onc +e my @split_list = map { chomp $_; [ split(/\|/, $_) ] } <$piped_fh>; +# used only once

    In one subroutine, I have this three times.

    Also, my idify subroutine is just modifying $_.

    And my Fancy::Map modifies $_ too.

    If this is something ignored usually, I will ignore it, but I would like to know how to make it better. Should I just assign $_ to a variable and then use the variable?

    Another thing that I am confused by is why the expression form of eval is discouraged?

    The rest of the gentle issues are me being lazy with conditionals. While writing I was thinking my $var = "foo" if 'some condition', but Perl critic does not like it, but it is fixed easily.

    NOTE: Please see my update. Most are fixed, but the eval problem remains.

    My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.8.8 on web host.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Why eval $version?
3 direct replies — Read more / Contribute
by Aldebaran
on Jul 08, 2020 at 18:22

    I'm still processing what I experienced at TPC. I've had time to catch up on some of the youtube videos from the ones I missed. Sometime in all the zooming, I thought I heard one of the presenters say

    that's why we eval version

    I thought it was Sawyer X, who says a lot of things parenthetically, but I went over his talk for a third time and didn't hear it again. I didn't understand what he meant, so I could well have misheard it or just dreamed up the recollection wholesale.

    A couple days later, I'm going through the guts of a test script for local::lib and I see this, beginning line 16 of xt/bootstrap.t

    sub check_version { my ($perl, $module) = @_; my @inc = `$perl -le "print for \@INC"`; chomp @inc; (my $file = "$") =~ s{::}{/}g; ($file) = grep -e, map { "$_/$file" } @inc; return undef unless $file; my $version = MM->parse_version($file); eval $version; }

    Q1) What purpose does this line serve?

     eval $version;

    Q2) Is this a "string eval?"

    Q3) Do dangers lurk in its use, cf. Uri Guttman's 2019 TPC talk on 'eval'?

    Q4) Does anyone else remember the "that's why we eval version" comment, or is it just me?

    Thanks for your comment,

Detect boundaries within .png's - and cropping
6 direct replies — Read more / Contribute
by Stickybit
on Jul 08, 2020 at 02:30

    Hi everyone

    Got at specific problem, that I need some hints on. :-)

    I got a bunch of fixed size .png images with logos. Those logos are centered on the images, and empty space on the left/right of the logo, is transparent background. I need to crop those images on the fly, so that empty space on the left/right of the logo is removed.

    Any hints on how to solve that one? :-)

permission denied on file write only when running browser on application server
3 direct replies — Read more / Contribute
by raccardi
on Jul 07, 2020 at 13:56

    I have a simple test script that attempts to create a file and to append an existing file. This is a web-based script hosted by IIS.

    When I call this script by putting its URL into a browser running on my PC, the tests pass: one file is created, the other file is appended to. However, if I run a browser on the application server and access the script using a "localhost" URL, I get a permission denied error on both file operations.

    If I run the script from the command line on the server, either as an elevated (Run as Administrator) command line or not, the script is able to write to both files

    The script is running under IIS's CGI module, not FastCGI, with the Impersonate User option enabled. The site uses Windows Authentication. In both cases, regardless of which browser I use to run the script, I see that the perl.exe process is running as my domain user. I can't figure out why I get different behavior when access the script from a browser running on the server versus a browser running on my own PC. Has anyone ever seen anything like this?

    I used the Errno library to get some more info about the permission denied error when opening the files for write/append:

    13 Permission denied
    5 Access is denied

    By the way, my specific Windows user account has explicit access to the folder I'm trying to write to.

    As an experiment, I turned off Impersonate User on IIS's CGI module. The perl interpreter runs as DefaultAppPool and does have permission to write to the files.

REST::Client + Request Body
2 direct replies — Read more / Contribute
by ravigupta1
on Jul 07, 2020 at 07:23

    I am trying to connect Quest Foglight using REST::Client over https. I tried in PostMan tool and it is working over there but it accept authToken in Request Body.

    Appreciate if someone could help me to understand how to pass "authToken" in PERL code as Request Body. Below is my code FYR. This is not working as I am new to PERL and don't know how to pass authToken as request body.

    Use REST::Client; my $client = REST::Client->new(); $req = '{"authToken":"<My Auth Token Value>"}'; # Above value should be passed as request Body. As per Postman Tool. $url = "https://<my Quest Server>/api/v1/security/login"; $client->POST($url,$req); print($client->responseCode()); exit();
Perl and MongoDB
3 direct replies — Read more / Contribute
by Theodore
on Jul 06, 2020 at 09:21
    As the MongoDB module will soon be EOL, what the Monastery wisdom says about using MongoDB from Perl?

    I inherited an in-house (rather critical and very well written) Perl application which uses MongoDB as a back end. Replacing this application with another one is not an option for now.

    Should I start looking for another DB to completely replace MongoDB (which is a rather daunting task)? If yes, any suggestions?

    Should i try to keep the application working using the last version (2.2.0) of the driver?

    Any other suggestions?

defined and value is 0
3 direct replies — Read more / Contribute
by redtux
on Jul 06, 2020 at 07:57
    Hi I have a sub routine which reads in the result of thousands of open3 calls (ffprobe for info) and parses the results. Everything works fine except for uninitialized warnings for about 400 out of around 33k entries Trying to get the the values that are missing is proving problematic
    foreach my $file (keys %hash){ foreach my $opt (keys %{$hash{$file}}){ if (!(defined $opt,$hash{$file}->{$opt})){ say '9809 ', join '|',$file,$opt,$hash{$file}->{$opt}; } $hash{$file}->{$opt}=~s/[^[:ascii:]]//gxms; my $lined=join ("\t",$file,$opt,$hash{$file}->{$opt})."\n"; }
    This gives initialized warnings on the last two lines if the value is blank. However I cant seem to get the problems lines to check. The defined check at the top prints everything that is either blank or 0. However 0 is a fully valid value, not undef. If I test for true (if ($hash{$file}->{$opt}){}), everything with a zero gets removed. Is there any way around this? (I'm probably missing something basic here) thanks Mike

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2020-07-10 21:29 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found