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
compare two arrays if both are equal and have same elements irrespective of their position
5 direct replies — Read more / Contribute
by noviceuser
on Jun 16, 2021 at 13:20

    i have to compare two arrays to check if both are exactly same or not irrespective of which elements come at which position

    here @failedTestsuites is first array and @referenceTestsuites is second array, i have tried below code but it is not giving desired result. one of the case is: suppose @failedTestsuites array contains 'a','b','c' and @referenceTestsuites array contains 'a','b' then also it is printing both array as same. please help

    my $comp = Array::Compare->new; if ($comp->compare(\@failedTestsuites, \@referenceTestsuites)) { print "same arrays\n"; } else { print "different arrays\n"; }
how to ignore spaces, commas or new line of an array when comparing
1 direct reply — Read more / Contribute
by noviceuser
on Jun 16, 2021 at 04:01

    I am trying to compare two arrays which may have strings/elements separated by newline, spaces or comma, how can i ignore those special characters while comparing

    e.g: suppose @array1 has contents like below

    abc xyz

    and @array2 has contents like below

    abc xyz
    my $comp = Array::Compare->new; if ($comp->compare(\@array1, \@array2)) { print "array same\n"; } else { print "array not same\n"; }
stat() returning values in random order for files on s3 bucket? (SOLVED)
2 direct replies — Read more / Contribute
by nysus
on Jun 15, 2021 at 17:08

    I'm seeing some weird behavior. Got this line: my @stats = stat($f);

    $f is the path to a file sitting on an amazon s3 bucket. I am using s3fs to mount the bucket on my instance and the path to the file looks like a normal path, something like: /backups/backups_db/some_dir/file. The backups_db is a symlink to /s3bucket/backups_db At any rate, @stats contains values from the stat() function above but in totally random order. Here's a dump of three different files:

    [DEBUG] Rex.CLI: 206 $VAR1 = [ 'gid', 1000, 'size', 325342, 'mode', '0644', 'mtime', 1623769844, 'uid', 1000, 'atime', 0 ]; [DEBUG] Rex.CLI: 206 $VAR1 = [ 'uid', 1000, 'size', 325316, 'atime', 0, 'gid', 1000, 'mode', '0644', 'mtime', 1623771735 ]; [DEBUG] Rex.CLI: 206 $VAR1 = [ 'gid', 1000, 'mtime', 1623772143, 'mode', '0644', 'atime', 0, 'uid', 1000, 'size', 325346 ];

    I'm not sure if I'm just being an idiot on how to use the stat() function or if the stat() function does not work properly on a file on an s3 bucket using s3fs. If it's the latter, can this be fixed?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Reading CSV Function
3 direct replies — Read more / Contribute
by workInProgress12
on Jun 14, 2021 at 22:15

    Hey all, trying to make a comprehensive perl function that can be used in various flows for my work. The stuff that I'm struggling with is:

    $outputType – if left blank (‘’) assume array reference (‘aref’), only other possibility is hash reference (‘href’)

    $headingAref – if left blank (‘’) assume first line as the header. Otherwise pass in an array reference where each element in the array is the column heading in the order they appear in the file.

    $readLineFrom – if left blank (‘’) start at the first line, otherwise if a number defined start reading at that line of the file.

    $readLineTo – if left blank (‘’) read till EOF, otherwise if a number defined end reading at that line of the file.

    Trying to read the csv from a certain row to another is proving to be difficult. I tried to keep an index and use a nested for loop but this is still printing all my rows, not just the ones I want. Furthermore, I need to be able to add my own personal header according to $headerAref. I tried opening the file for reading and writing using "+<" but that causes my code to only print the header. I also need to duplicate all of this for a hash output type as well but I have not yet looked into this. Right now, my code is able to output the csv file as an array or arrays as requested but reads each row (not the range given) and can't add a header. Any help would be appreciated.

    #!/tool/pandora64/bin/perl5.8.8 #MODULES use strict; use warnings; use Pod::Usage; use Data::Dumper; use Getopt::Long; use File::Basename; use Cwd 'abs_path'; use Data::Dump 'dd'; use Data::Dumper qw(Dumper); use Text::CSV; #my $output = readCSV($pathToCSV, $columnSeperatorChar, $cellEncapsual +tingChar, $outputType, $headingAref, $readLineFrom, $readLineTo, $ign +oreFirstLineBool); my @headerArray = "a,b,c,d,e,f,g,h,i,j,k"; my $headerArrayRef = \@headerArray; my $pathToCSV = "input.csv"; my $columnSeperatorChar = ""; my $cellEncapsulatingChar = ""; my $outputType = ""; my $headingAref = "$headerArrayRef"; my $readLineFrom = 8; my $readLineTo = ""; my $ignoreFirstLineBool = 0; $columnSeperatorChar = "," if ($columnSeperatorChar eq ""); $cellEncapsulatingChar = "" if ($cellEncapsulatingChar eq ""); $outputType = 'aref' if ($outputType eq ""); my @output; my $rowCounter = 0; my @index; my $currentIndex = 0; open(my $file, "<", $pathToCSV) or die "File could not be opened $!\n" +; =begin comment if($headingAref ne ""){ my @headerArrayCopy = @$headingAref; print ($file "@headerArrayCopy\n"); #adds header } =end comment =cut while (my $line = <$file>){ chomp $line; $currentIndex++; if ($readLineTo eq ""){ $rowCounter++; $readLineTo = $rowCounter; } for ($currentIndex >= $readLineFrom and $currentIndex <= $readLine +To){ my @row = split ("$columnSeperatorChar", $line); foreach my $cell (@row){ $cell = "$cellEncapsulatingChar$cell$cellEncapsulatingChar +" } push @output, \@row; } } print Dumper(\@output);
Capturing query string parameter from Header response.
1 direct reply — Read more / Contribute
by chandantul
on Jun 14, 2021 at 01:14

    Hello Monks, I was trying to find a way to capture existing header response in my PSGI app but i was not able to access the same and i have used the catalyst::Request but failed to do so. Can you please let me know the way i can achieve the same? Please check my header response.

    Request URL: https://testchandan.com:5001/?code=E1XqzJxB-OMLscNAJtWZG1 +_wthVln5UfNIWwAS5YDU8&state=1234 Request Method: GET Status Code: 200 OK Remote Address: 10.10.1.6:5001 Referrer Policy: strict-origin-when-cross-origin Content-Length: 1252 Content-Type: text/html; charset=UTF-8 Date: Mon, 14 Jun 2021 04:59:57 GMT Server: HTTP::Server::PSGI Server: Perl Dancer2 0.301002 Set-Cookie: plack_session=ba1d0d240ba3ed6c6fca13c240c758a6c812200d; pa +th=/ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/av +if,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q +=0.9 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Connection: keep-alive Host: testchandan.com:5001 Sec-Fetch-Dest: document Sec-Fetch-Mode: navigate Sec-Fetch-Site: cross-site Sec-Fetch-User: ?1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537. +36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36 code: E1XqzJxB-OMLscNAJtWZG1_wthVln5UfNIWwAS5YDU8 state: 1234

    Please check below code that i was trying to capture the same but unable to do so

    package Op; use strict; use warnings; use LWP::UserAgent; use URI; my $ua = LWP::UserAgent->new(); my $uri2 = URI->new('https://testchandan.com:5001/dev'); my $response1 = $ua->get($uri2); my $responsecode = $response1->code() print $response1->headers_as_string if $response1->is_success;
The "%" operator and its documentation.
1 direct reply — Read more / Contribute
by syphilis
on Jun 13, 2021 at 21:40
    Hi,

    UPDATE: I've now filed a bug report about this.

    I've been chewing on this over the last couple of days, and it doesn't look right to me.
    It's an issue that arises only when NV-precision is greater than IV-precision.
    So, if ivsize is 8 bytes, then in order to experience the issue, you'll generally need to use a quadmath build (nvtype of __float28, NV-precision of 113 bits).
    With such a perl, I get:
    C:\_32>perl -wle "$m = (2**113) - 1; $n = 2; print $m % $n;" 0
    In contrast, POSIX::fmod returns "1" for the same operation.
    C:\_32>perl -MPOSIX -wle "$m = (2**113) - 1; $n = 2; print fmod($m, $n +);" 1
    Here's the section of perlop documentation that I'm looking at:
    Binary "%" is the modulo operator, which computes the division remaind +er of its first argument with respect to its second argument. Given integ +er operands $m and $n: If $n is positive, then "$m % $n" is $m minus the largest multiple of $n less than or equal to $m. If $n is negative, th +en "$m % $n" is $m minus the smallest multiple of $n that is not less tha +n $m (that is, the result will be less than or equal to zero). If the operands $m and $n are floating point values and the absolute value of $n (that is "abs($n)") is less than "(UV_MAX + 1)", only the integer portion of $m and $n will be used in the operation (Note: here "UV_MAX +" means the maximum of the unsigned integer type). If the absolute value of the right operand ("abs($n)") is greater than or equal to "(UV_MAX + 1)", "%" computes the floating-point remainder $r in the equation "($r = $m - $i*$n)" where $i is a certain integer that makes +$r have the same sign as the right operand $n (not as the left operand $m like C function "fmod()") and the absolute value less than that of $n. Note that when "use integer" is in scope, "%" gives you direct access +to the modulo operator as implemented by your C compiler. This operator i +s not as well defined for negative operands, but it will execute faster.
    AFAICT, the parts of that documentation that are pertinent to my given example are:
    If $n is positive, then "$m % $n" is $m minus the largest multiple of +$n less than or equal to $m. .... If the operands $m and $n are floating point values and the absolute v +alue of $n (that is "abs($n)") is less than "(UV_MAX + 1)", only the integer portion of $m and $n will be used in the operation (Note: here "UV_MAX +" means the maximum of the unsigned integer type)
    And to me that implies that the correct calculation for the given example is to do:
    C:\_32>perl -wle "$r = 10384593717069655257060992658440191.0 - (519229 +6858534827628530496329220095.0 * 2); print $r;" 1
    This agrees with the value produced by POSIX::fmod(), but disagrees with the value produced by the "%" operator.
    Do we agree that perl is buggy here ?

    If you have a perl with ivsize of 4, you can also demonstrate the same issue.
    With ivsize of 4, NV-precision of 113 bits:
    C:\_32>perl -wle "$m = (2**113) - 1; $n = 2; print $m % $n;" 0 C:\_32>perl -MPOSIX -wle "$m = (2**113) - 1; $n = 2; print fmod($m, $n +);" 1
    With ivsize of 4, NV-precision of 64 bits (long double):
    C:\>perl -wle "$m = (2**64) - 1; $n = 2; print $m % $n;" 0 C:\>perl -MPOSIX -wle "$m = (2**64) - 1; $n = 2; print fmod($m, $n);" 1
    And now to really muddy the waters !! One might be expecting the trend to continue when ivsize is 4, and NV-precision is 53-bits .... but not so:
    C:\_32>perl -wle "$m = (2**53) - 1; $n = 2; print $m % $n;" 1 C:\_32>perl -MPOSIX -wle "$m = (2**53) - 1; $n = 2; print fmod($m, $n) +;" 1
    This time the "%" operator decides to act in accordance with (my reading of) the perlop documentation.

    Cheers,
    Rob
When not to use taint mode
8 direct replies — Read more / Contribute
by Bod
on Jun 12, 2021 at 15:09

    A few times recently in the Monastery I have noticed mention of taint mode

    A long time ago I first came across taint mode and decided it is far too difficult to understand...I've since looked again and it doesn't appear anything like as mystical as it once did. That's what happens when one improves of course.

    I have never used taint mode so have no idea of the ease or otherwise of actually using it. It seems from this poll that most others don't use it either. So why not? Is it that taint mode is actually difficult to use or are there reasons to keep it switched off as it is by default?

    Do you have any advice on the topic?

IO::Async::PID works fine in SSCCE, fails randomly in real app, how to debug?
No replies — Read more | Post response
by vr
on Jun 12, 2021 at 09:25

    Edit. Please ignore. I forgot there's already "1 while waitpid(-1, WNOHANG) > 0;" added long time ago to obscure part of application to reap rarely spawned children, should re-write it now using e.g. a no-op IO::Async::PID's on_exit handler. Sorry.

    The OS is Windows, there's existing IO::Async-based, somewhat complex and clumsy app, which I want to extend. At some point in workflow, lists of file names arrive, in batches up to hundreds of items, but usually, say, 1-20. What I want to insert, is to process each file invoking external CLI tool, which takes, say, 5 to 60 seconds per file. It is to be done asynchronously by a few (2-5, I'll see when I get to observing real load) workers, and when whole current batch is ready, I want to get control back and proceed with workflow as it exists now.

    Because it's Windows, the recommended way with IO::Async::Process won't work, it wants real POSIX fork. "OK", I thought, I'll fire "system 1, @args" per file, and PID will be used to add IO::Async::PID watcher to the loop.

Replacing crypt() for password login via a digest - looking for stronger alternative
7 direct replies — Read more / Contribute
by davebaker
on Jun 11, 2021 at 15:14

    Yikes. Another 8-1/2 billion passwords have been leaked.

    Have a login system for my web app that has a couple hundred users. To log in, a user enters a password, the submitted password is run through my authentication script that uses crypt() and compares the result to a hashed string (i.e., a digest) stored in a text file on my server (because crypt() had created that digest when the user first created his or her account). If the crypt() result is the same as the digest, we have a match, so c'mon in.

    I've done enough research to see that such a system is a commonly-used one and has the advantage of not storing passwords in plain text, or even storing passwords at all, but that crypt() uses an encryption method that isn't very strong, at least nowadays.

    So I'm thinking there is a security vulnerability in the event a black hat were to obtain the plain text file by breaking into my server. Then the black hat runs a rainbow table against each user's digest to find a password that produces that same string. Then the black hat takes the user's password to the user's bank web site and uses that same password to empty the account, because so many people use the same password for multiple websites. It's not so much the user's information on my website that's confidential, it's the empty-the-bank-account scenario that's troubling.

    This issue always has been present with the use of a digest for purposes of password checking, but I'm thinking it becomes more of an issue when a couple billion more possible passwords are released that can be used to make the rainbow table more effective. Hence the risks of damages to my users has gone up, and my risk of getting sued has gone up. Do you agree?

    So I'm thinking that it would be good to replace:

    grant_website_access() if ( $password_entered_on_web_form eq crypt( $u +ser_specific_hash_stored_in_text_file_on_server, $password_entered_on +_web_form );

    with

    grant_website_access() if ( $password_entered_on_web_form eq something +_stronger_than_crypt( $user_specific_hash_stored_in_text_file_on_serv +er, $password_entered_on_web_form );

    An article on Password Storage with Salted Hashes recommends Crypt::SaltedHash, with an example that seems to use the module's methods to achieve the same effect as my code. The author also recommends the use of SHA-512 with Crypt::SaltedHash rather than the considerably less secure SHA-1 or MD-5.

    That's a good solution, it appears, to my perceived problem. I've checked threads back through 2012 here on PM and find none better, although Authen::Passphrase::BlowfishCrypt is intriguing.

How to count the vocabulary of an author?
5 direct replies — Read more / Contribute
by karlgoethebier
on Jun 11, 2021 at 07:12

    I have no serious idea for the moment. And done nothing so far. Background is that Kurt Schumacher claimed that Goethe had a vocabulary of about 29.000 words and Adenauer only had a vocabulary of about 500 words.

    Update: Thanks to all for the kind and inspiring replies. I guess Lingua::Stem is the way to go. I‘ll open another thread about tokenizing.

    «The Crux of the Biscuit is the Apostrophe»


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