Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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
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
5 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»

AnyEvent::HTTPD -> Extra Callback after response?
2 direct replies — Read more / Contribute
by sectokia
on Jun 10, 2021 at 21:51
    Hi Monks,

    I cannot work out why in this code the callback sub is called twice. The response goes to the client after the timer expires the first time, but then it triggers all over again. Code:

    use strict; use warnings; use AnyEvent::HTTPD; my $h = AnyEvent::HTTPD->new(port => 8123);; my $timer; $h->reg_cb ( '/test' => sub { my ($httpd,$req) = @_; $req->respond({ content => ['text/plain', sub { my ($data_cb) = @_; return unless $data_cb; print "Setting timer...\n"; $timer = AnyEvent->timer(after => 5, cb => sub { print "Sending response...\n"; $data_cb->("Hello"); $data_cb->(); }); }] }); }, ); my $c = AnyEvent::condvar; $c->recv();
    Output:
    Setting timer... Sending response... Setting timer... Sending response...
    I was expecting the sub to only be called once. But it ends up being called a second time? Is there any way to avoid this? Thanks!
a look at Sub::Genius with the debugger, and use v in 2021
1 direct reply — Read more / Contribute
by Aldebaran
on Jun 10, 2021 at 20:51

    I was looking at my normal youtube feed when Leon Timmermans came up with a presentation about Raku. (Certainly interesting, and I have questions for him, but not now.) I soon realized that the zoom convention was happening, and that I could get it in essentially real time on youtube. I have them on as I go about my business, and B Estrade's talk comes on. I went about cleaning the kitchen at the start, but once it started into graph theory, I got very interested, and gave it my full attention. As things started cooking, the moderator chimed in that five minutes remained, and the presenter was left to rush through slides, to my, and seemingly, his dismay. He didn't get to running code, but I was interested enough to see what I could dig up. I feel like there was a second half to the talk that I would gladly hear. I tried to fill in the gaps by working some code, and I would like to post sources, output and questions.

    Thanks to presenters. I've seen several and might need half a year to see them all. I appreciate your service. Thanks also for comments,

Help! dmake error Win10
6 direct replies — Read more / Contribute
by Sanjay
on Jun 10, 2021 at 08:15

    dmake error while trying to install a module. Windows 10 Home. ActiveState Perl 5.26. Screen ...

    C:\ids\nm3\perl\ssan3-2.6.01>perl Makefile.PL Checking if your kit is complete... Looks good Generating a dmake-style Makefile Writing Makefile for ssan3 Writing MYMETA.yml and MYMETA.json C:\ids\nm3\perl\ssan3-2.6.01>dmake cp ssan3.pm blib\lib\ssan3.pm AutoSplitting blib\lib\ssan3.pm (blib\lib\auto\ssan3) Running Mkbootstrap for ssan3 () "C:\Perl\bin\perl.exe" -MExtUtils::Command -e chmod -- 644 "ssan3.bs" "C:\Perl\bin\perl.exe" -MExtUtils::Command::MM -e cp_nonempty -- ssan3 +.bs blib\arch\auto\ssan3\ssan3.bs 644 "C:\Perl\bin\perl.exe" "C:\Perl\lib\ExtUtils\xsubpp" -typemap C:\Perl +\lib\ExtUtils\typemap ssan3.xs > ssan3.xsc "C:\Perl\bin\perl.exe" -MExtUtils::Command -e mv -- ssan3.xsc ssan3.c C:\Perl\site\bin\gcc.exe -c -IC:\ids\nm3/h -s -O2 -DWIN32 -DWIN64 + -DCONSERVATIVE -DPERL_TEXTMODE_SCRIPTS -DUSE_SITECUSTOMIZE -DPERL_IM +PLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fwrapv -fno-strict-aliasing -mms- +bitfields -s -O2 -DVERSION=\"2.6.01\" -DXS_VERSION=\"2.6. +01\" "-IC:\Perl\lib\CORE" ssan3.c "C:\Perl\bin\perl.exe" -MExtUtils::Mksymlists \ -e "Mksymlists('NAME'=>\"ssan3\", 'DLBASE' => 'ssan3', 'DL_FUNCS' + => { }, 'FUNCLIST' => [], 'IMPORTS' => { }, 'DL_VARS' => []);" C:\Perl\site\bin\g++.exe ssan3.def -o blib\arch\auto\ssan3\ssan3.dll - +mdll -s -static-libgcc -static-libstdc++ -L"C:\Perl\lib\CORE" -L"C:\M +inGW\x86_64-w64-mingw32\lib" ssan3.o "C:\Perl\lib\CORE\libperl526.a +" "C:\ids\nm3\lib\stssan3cl.lib" "-lmoldname" "-lkernel32" "-luser32" + "-lgdi32" "-lwinspool" "-lcomdlg32" "-ladvapi32" "-lshell32" "-lole3 +2" "-loleaut32" "-lnetapi32" "-luuid" "-lws2_32" "-lmpr" "-lwinmm" "- +lversion" "-lodbc32" "-lodbccp32" "-lcomctl32" -Wl,--enable-auto-imag +e-base ssan3.o:ssan3.c:(.text+0x263): undefined reference to `ezx113' ssan3.o:ssan3.c:(.text+0x95f): undefined reference to `ezx112' ssan3.o:ssan3.c:(.text+0x1003): undefined reference to `ezx111' ssan3.o:ssan3.c:(.text+0x1482): undefined reference to `ezx110' ssan3.o:ssan3.c:(.text+0x1984): undefined reference to `ezx108' ssan3.o:ssan3.c:(.text+0x2001): undefined reference to `ezx107' ssan3.o:ssan3.c:(.text+0x2533): undefined reference to `ezx105' ssan3.o:ssan3.c:(.text+0x29f2): undefined reference to `ezx104' ssan3.o:ssan3.c:(.text+0x2f00): undefined reference to `ezx103' ssan3.o:ssan3.c:(.text+0x33ed): undefined reference to `ezx102' ssan3.o:ssan3.c:(.text+0x3a1c): undefined reference to `ezx101' ssan3.o:ssan3.c:(.text+0x3fe3): undefined reference to `ezx100' ssan3.o:ssan3.c:(.text+0x4460): undefined reference to `ezx099' ssan3.o:ssan3.c:(.text+0x4c59): undefined reference to `ezx005' ssan3.o:ssan3.c:(.text+0x54f0): undefined reference to `ezx026' ssan3.o:ssan3.c:(.text+0x5b78): undefined reference to `ezx061' ssan3.o:ssan3.c:(.text+0x60c7): undefined reference to `ezx060' ssan3.o:ssan3.c:(.text+0x66b3): undefined reference to `ezx022' ssan3.o:ssan3.c:(.text+0x6c23): undefined reference to `ezx021' ssan3.o:ssan3.c:(.text+0x6f9a): undefined reference to `ezx002' ssan3.o:ssan3.c:(.text+0x716b): undefined reference to `ezx001' ssan3.o:ssan3.c:(.text+0x75d9): undefined reference to `ezx109' ssan3.o:ssan3.c:(.text+0x7b4d): undefined reference to `ezx106' ssan3.o:ssan3.c:(.text+0x8340): undefined reference to `ezx004' ssan3.o:ssan3.c:(.text+0x8b93): undefined reference to `ezx025' ssan3.o:ssan3.c:(.text+0x9450): undefined reference to `ezx003' ssan3.o:ssan3.c:(.text+0x9ca2): undefined reference to `ezx024' ssan3.o:ssan3.c:(.text+0xa44b): undefined reference to `ezx023' c:/perl/site/lib/auto/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.6.3/.. +/../../../x86_64-w64-mingw32/bin/ld.exe: ssan3.o: bad reloc address 0 +xae0 in section `.rdata' collect2: ld returned 1 exit status dmake.exe: Error code 129, while making 'blib\arch\auto\ssan3\ssan3.d +ll' C:\ids\nm3\perl\ssan3-2.6.01>

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 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
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-06-15 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (72 votes). Check out past polls.

    Notices?