Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
How to use a private version of a published module?
2 direct replies — Read more / Contribute
on Jun 18, 2021 at 10:57
    So I am trying to publish a module that allows access to IG's API directly. This would allow simple listings of your spread bet positions. The module is written and works well for me. Under the hatch it uses the CPAN module Rest::Client for formatting requests.

    In order to test it it needs to be able to be tested without the use of my personal IG login details as these are private to me. The test programs use the unmodified module, with a special @INC path from which it picks up a special version of Rest::Client. This version hashes the parameters, and then reads a file based on the hash and returns identical json formatted data as if my account had been used.

    My aim is to test my module, not Rest::Client which is not my module.

    I included the special version of Rest::Client and in my module code as well as a Record::Rest::Client for creating the special data files read by my local Rest::Client module.

    All works well, however when I submit the module, it says I have no permission to publish a version of Rest::Client. I do not actually want to do that, its only used in the testing of my module.

    I seek some suggestions as to what is the best way to proceed in this situation.

Windows and backslashes and replacements oh my!
3 direct replies — Read more / Contribute
by stevieb
on Jun 18, 2021 at 10:28

    Hey there my fellow esteemed Monks!

    I've been having a long-running problem that I'm finally fed up enough with to ask for help.

    In my test suite for berrybrew, I perform a path substitution in a configuration file before the tests load. The specific command which is run out of a batch script looks like this:

    call perl -i.bak -ne "s/berrybrew(?!\\+test)/berrybrew\\test/; print" +test/data/config.json

    Essentially, that's supposed to change the path C:\berrybrew to C:\berrybrew\test.

    On some of my Windows 10 systems, it works just fine, and everything goes on its merry way. On other Win10 systems, the change results in a tab character being inserted, so the path looks like this: C:\berrybrew[TAB]est. If I insert four backslashes in the replacement (ie. berrybrew\\\\test, it works. However, after a git push then a git pull on the other systems that work with just the two backslashes, it breaks.

    What can I do to provide myself some consistency across systems? Is there some unicode trick or something I can use? I've tried replacing the forward slashes in the substitution to single-quotes (eg: s///; to s''';) to no avail.

    I'd really like to get this resolved so that the substitution works reliably across all systems.



Escaping or removing quotes
2 direct replies — Read more / Contribute
by htmanning
on Jun 17, 2021 at 17:52
    Monks, I've done this a thousand times before, but for some reason this isn't working. I'm trying to escape or preferably replace double quotes that are copied from a website. I'm doing like this but Perl is not recognizing the quotes at all.
    $string = 'copied from a web page looking like this: “Test.” '; $string =~ s/“//g; $string =~ s/”//g;
    Am I just having a brain fart moment? Perl seems to not even recognize the quotes and just lets them go through. I have ruled out simple things like the string name being wrong or something.
Issue with LWP loading client certificate
3 direct replies — Read more / Contribute
by ffrost
on Jun 17, 2021 at 16:04

    Objective: To connect from my machine to an external server using HTTPS. The connection will use the GET verb and the external server should return a message back to the client. As a security mechanism, the certificate for my client must be sent to the server as authentication.

    I'm using LWP to get the response from the external server. I've run into two problems I'm not sure of. First, I am unsure if the LWP program is actually loading the certificate, which is in DER format. Second, since the server is responding with a 403 error I don't think they are getting my certificate. Am I loading the certificate correctly to send from my Windows machine to their external IIS server?

    The DER certificate is valid and has not expired. It is not a self-signed certificate.

    use LWP::UserAgent; use Data::Dumper; use Cwd; $cdir = getcwd; $endpoint = ‘https://omit/commotest'; $ua = LWP::UserAgent->new; $ua->ssl_opts(SSL_cert_file => "$cdir\\der.cer"); $ua->ssl_opts(SSL_use_cert => '1'); $response = $ua->get($endpoint); if ($response->is_success) { print Dumper $response; } else { print "Error: " . $response->status_line, "\n"; }
reading through dated directories
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 17, 2021 at 14:13
    Dear Monks,

    I am reading though a large set of dated directories with a large number of dates where the number of <date> directories is much larger than the number of parent <dir> directories. There is only one date "dir" per parent in the future--the rest are in the past. I just have to find these future dates.

    dir1 / <date1> dir1 / <date2> dir2 / <date3> dir3 / <date4>

    I am currently using glob("*/*") to loop through all the dates to find all the ones in the future. My glob has become annoyingly slow and I'd like to speed it up. Any ideas how I can use readdir() to return an array of "dir / <date> " of only the future dates

    Thank you for your time--always appreciate the help & advice.


Mojo pp html_entities.txt
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jun 17, 2021 at 12:45

    How can I override at run time the directory where Mojo searches for the file "html_entities.txt"? The reason is that I am trying to use PAR pp to create an executable of an application using Mojo on Windows 10, but the exe fails with the following error:

    Unable to open html entities file (C:\Users\de\AppData\Local\Temp\par-6663\cache-9973dd41d00e8bee27c9630746780ae38da71709\inc\lib\Mojo\resources\html_entities.txt): No such file or directory at C:\Users\de\AppData\Local\Temp\par-6663\cache-9973dd41d00e8bee27c9630746780ae38da71709\inc\lib/Mojo/ line 14.

    I already tried to add to the file explicitly:

    -a "C:\Strawberry\perl\vendor\lib\Mojo\resources;Mojo/resources"

    with no effect. So probably copying and pasting the file into my data structure and instruct Mojo to search there could be a fix. Any suggestion?

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);

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-06-19 00:54 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (91 votes). Check out past polls.