Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
Apache becomes unresponsive running mod_perl
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 15, 2018 at 10:57
    Please help.

    Apache becomes unresponsive when it encounters an error (as reported in the error logs with an associated 500 HTTP error in the access log). I'm running Apache 2.2.29 in a windows environment with mod_perl (Apache/2.2.29 (Win32) mod_apreq2-20090110/2.8.0 mod_perl/2.0.7 Perl/v5.16.3 ). When a request results in an error all subsequent requests fail. The Apache service is still up and running but requires a restart for requests to begin to process again. I've begun by trying to fix any code that would result in an error but it's difficult to account for them all. Could mod_perl be somehow caching error responses? Is there a way to have Apache recover by itself? (This is an old legacy system which I'm trying to support). Any help would be most appreciated.

    Here is a sample of one of the errors encountered:

    [error] [client] encountered object 'true', but neither allo +w_blessed, convert_blessed nor allow_tags settings are enabled (or TO +_JSON/FREEZE method missing) at C:/ line 1234.\n

    Discipulus added code tags to the error

SOAP::Lite and default namespaces
2 direct replies — Read more / Contribute
by DreamT
on Jan 15, 2018 at 06:17
    (Here comes YET ANOTHER SOAP::Lite question ;) )
    By default(?), SOAP::Lite produces a couple of namespaces, "soap:encodingStyle" and "xmlns:soapenc". Is it possible to remove them? I know how to alter them and how to add new namespaces, but not how to remove existing ones.
    (Have tried to find an answer in one of the existing threads, but without luck).
Reading Variables in Excel Spreadsheet
3 direct replies — Read more / Contribute
by merrymonk
on Jan 15, 2018 at 04:06
    I have just come across something that I have never seen before in an Excel spreadsheet.

    When I selected a cell I expected to see the cell reference (e.g. B30) in the box in the top left hand corner of the spreadsheet.
    However, I now can see a ‘variable’ name (for example height). (This variable name is used in a formula in another cell in the spreadsheet - this is not important to me but is an example of how it is used in Excel).

    I need to read the value of the variable. I can do this by using the cell reference which is still available. However, this means that the each data item has to be in a fixed position on the spreadsheet.
    How can I use Perl to access this data using the variable name rather than the cell reference?
    I guess I will need to know
    1. How to open the spreadsheet;
    2. How to search for the data associated with variable name.

    Also it would be good to know how to write data to a cell and give it a variable name.

    As this is something I only have seen recently, I wondered if whatever the method is, applies to only certain version of Excel.
Detecting for HTTP pages code changes
3 direct replies — Read more / Contribute
by dotowwxo
on Jan 14, 2018 at 21:30
    Hello Monks,

    Currently, I have a script that curls for HTTP code (200,404 etc.) from a list of websites and check for HTTP code error 404, if the webpage returns HTTP code error 404, it will call another script to send out an email. However, the flaw in this script is that the script is constantly running 24/7, each cycle of the script is about 15 minutes. Every 15 minutes, if the website is still down (HTTP code 404), it will send out email. This means that I receive an email every 15 minutes if the webpage is still down. However, this is not what I want as I only want to receive 1 email when the webpage switches from a 200 webpage to a 404 webpage. Is there a way where I can enhance this and reduce the 404 error?

    Due to confidential issues, I cannot disclose the script, however, this is a short example in my script that I used to check for HTTP code check on the script:
    my $HTTPCode =`curl -s -w "%{http_code}" -o /dev/null https://$THIS_UR +L 2>&1`; #this is the line i used to retrieve the http_code if($HTTP Code == 404){ #Send email }
    What I want to achieve is instead of checking for HTTP code error 404, is there a way to detect if there's a page change from a 200 webpage to 404 webpage, send email. And if the webpage is already in a 404 state, do not send email.. I know this is a very vague question because I cannot provide my script to all of you.. but any suggestion in theory is good too. Thank you in advance
Auto Re-Connect Websocket Net::Async::WebSocket::Client
No replies — Read more | Post response
by awohld
on Jan 14, 2018 at 20:44
    Hi, here is working websocket code using Net::Async::WebSocket::Client and occasionally the websocket will drop. The websocket API heartbeat feed sends a JSON object every second. If there is no data for two or three seconds it is okay to reconnect the session assuming no other way to detect the socket is down.

    What would be the proper way to go about detecting a dropped/closed web socket and auto-reconnecting?

    I think the answer has something to do with Future but I don't understand how it would go together.

    #!/usr/bin/perl use warnings; use strict; use IO::Async::Loop; use Net::Async::WebSocket::Client; use JSON::XS qw( decode_json encode_json ); use Data::Dumper; my $client = Net::Async::WebSocket::Client->new( on_text_frame => sub { my ( $self, $json ) = @_; my $data = decode_json $json; # this is how we detect a heartbeat #return if $data->{type} eq 'heartbeat'; print Dumper $data; }, ); my $loop = IO::Async::Loop->new; $loop->add( $client ); $client->connect( url => "wss://", )->then( sub { $client->send_text_frame( encode_json { type => "subscribe", product_ids => [ "ETH-USD", ], channels => [ 'heartbeat', ] } ); } )->get; $loop->run;
Capturing groups where the ending is optional
3 direct replies — Read more / Contribute
by cwm9
on Jan 14, 2018 at 18:34
    I'm trying to split a line into two groups. The first group is a complete unknown, except that it will not contain a specific string. The second group starts with that same string and includes everything after it which may or may not be present. Example: suppose 'right' is the specific string in question. Here's an example of the desired input vs output:
    left \1=left \2= right \1= \2=right rightabc \1= \2=rightabc leftright \1=left \2=right leftrightabc \1=left \2=rightabc
    Here's what I've tried so far. This version is overly greedy -- it won't give up eating group 2 into group 1.
    s/(.*)(right)?/\1 <> \2/ echo "left"|perl -ne 'print if s/(.*)(right)?/\1 <> \2/' + left <> echo "right"|perl -ne 'print if s/(.*)(right)?/\1 <> \2/' + right <> echo "leftright"|perl -ne 'print if s/(.*)(right)?/\1 <> \2/' + leftright <>
    This version splits properly, but /2 is the same as /1 when there is no /2:
    s/(.*(?=right.*)|(.*))/\ echo "left"|perl -ne 'print if s/(.*(?=right.*)|(.*))/\1 <> \2/' + left <> left echo "right"|perl -ne 'print if s/(.*(?=right.*)|(.*))/\1 <> \2/' + <> right echo "leftright"|perl -ne 'print if s/(.*(?=right.*)|(.*))/\1 <> \2/' + left <> right
Excel::Writer::XLSX; Multi-worksheets formulas disappear on loading
1 direct reply — Read more / Contribute
by Bananorpion
on Jan 14, 2018 at 18:01

    Hello PerlMonks

    After a year far from Perl, I've been drawn to it once again, and for the first time in a while, I've stumbled upon something I can't google my way out.

    Here's the context: the current script I use for work creates a csv file, which is subsequently pasted into a worksheet, in an xlsx file. This works fine, but this week-end I decided to improve it, and to create the Excel file from scratch via the script, removing the human manipulation inbetween. It almost works, barring one detail: some formulas (Every formula referring to cells in another worksheet.) disappear when Microsoft Excel opens the file, with a not-really-helpful error message. The formulas are correct : they are exactly the same I used manually before, and if I input them myself in the newly created file, everything works.

    As it may be obvious, I can't fully assert this is a Perl problem, but this is the track I want to follow first.

    Below is a shortest-I-could version of the code I'm using (With random data and no formats, but the exact same structure regarding the problematic formula), which generates a file with the same error message on loading.

    #!/usr/bin/env perl use v5.010; use warnings; use strict; use Excel::Writer::XLSX; my $out = 'testfile'; my $excel = Excel::Writer::XLSX->new("$out.xlsx"); my $README = $excel->add_worksheet('README'); my $log_table = $excel->add_worksheet($out); # Log table ___________________ $log_table->write_row('A1', ['ColA', 'ColB', 'ColC', 'ColD', 'ColE']); sub randChar { @_ = qw/A B C D E/; return $_[(int rand 4)]; } my $current_line = 2; for (0 .. 9) { $log_table->write_row("A$current_line", [randChar(), randChar(), r +andChar(), randChar(), lc randChar()]); $current_line++; } $log_table->autofilter("A1:K$current_line"); # README ___________________ $current_line = 1; for (qw/a b c d e/) { $README->write_row("A$current_line", [$_, "=NB.SI('$out'!E:E;A$cur +rent_line)"]); $current_line++; } $excel->close();

    Has anyone ever encountered the same error and found how to fix it? I've been browsing the Excel doc, then the Excel::Writer::XLSX module doc, but I can't find anything related, and I prefer to ask for opinions before exploring in-depth how the module creates the files.. (There is no hurry, but I don't like leaving a script unfinished.)

    (If relevant, this has only be tested in Windows 10 OS, since it's supposed to eventually run on a Windows server, I haven't tried it in a Linux OS yet.)

    Thanks in advance for any help. \o/

Should I go back to basics of keep moving on?
6 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 14, 2018 at 16:28

    Hi Monks,

    Happy new year to you all.

    I had tried my hand at pearl a few months ago. I was using "Learning Perl" and had managed to write some scripts that helped me automate part of my work at office. Then I had to focus on other things at work, so I forgot all about Perl. This year, on a whim, I decided to get back to it.Here is what I have tried as of now. Not that this is difficult, but still, to truly test myself, I did not search anything on google, just started typing the script, kept modifying it and kept plodding on.

    Ex1: Vaguely recalling, "Learning Perl" had an exercise where I had to add elements to a list through a sub routine, and print them out. I modified the exercise so that the script would greet a number and add it to other numbers in the list. If this is the first number, it should be greeted stating something like "Hi, you are the first number". For the other numbers, it should show how many numbers we have in the list, then count them up, and show the sum. Initially I started with just adding the numbers in a list, and it took me some time to recollect how to do it. The other things mentioned above were gradually added, just to test my knowledge retention.

    user@linux-mint ~/perl_practice $ more use strict; use warnings; sub greet_add { my $sum = 0; my @seen; foreach my $num (@_) { $sum += $num; print "Hi $num, you are the first one!!\n" if !@seen; print "Hi $num, Please join (@seen). " if @seen; push @seen, $num; print "You ", scalar @seen , " fine folks add up to: $sum\n" i +f scalar @seen > 1; } print "Total is: $sum\n"; } my @list = (1..10); greet_add(@list); user@linux-mint ~/perl_practice $ perl Hi 1, you are the first one!! Hi 2, Please join (1). You 2 fine folks add up to: 3 Hi 3, Please join (1 2). You 3 fine folks add up to: 6 Hi 4, Please join (1 2 3). You 4 fine folks add up to: 10 Hi 5, Please join (1 2 3 4). You 5 fine folks add up to: 15 Hi 6, Please join (1 2 3 4 5). You 6 fine folks add up to: 21 Hi 7, Please join (1 2 3 4 5 6). You 7 fine folks add up to: 28 Hi 8, Please join (1 2 3 4 5 6 7). You 8 fine folks add up to: 36 Hi 9, Please join (1 2 3 4 5 6 7 8). You 9 fine folks add up to: 45 Hi 10, Please join (1 2 3 4 5 6 7 8 9). You 10 fine folks add up to: 5 +5 Total is: 55 user@linux-mint ~/perl_practice $

    I also tried to recall some basic map and grep stuff:

    user@linux-mint ~/perl_practice $ more use strict; use warnings; my @tripled = map { $_ * 3 } my @list = (1..10); print "\@list = @list\n\@tripled = @tripled\n"; my @div_by_two= grep { $_ % 2 == 0 } @tripled; print "\@div_by_two = @div_by_two\n"; user@linux-mint ~/perl_practice $ perl @list = 1 2 3 4 5 6 7 8 9 10 @tripled = 3 6 9 12 15 18 21 24 27 30 @div_by_two = 6 12 18 24 30 user@linux-mint ~/perl_practice $

    I am not sure if at this point, I am good enough to start with some intermediate perl. I was planning to start right off with the books - "Intermediate Perl" and "Modern Perl". But with what I know, should I study the basics more and then move ahead? Sort of confused here folks, would appreciate your feedback

Print the output results of matching line horizontally
6 direct replies — Read more / Contribute
by sreek3502
on Jan 14, 2018 at 10:32

    Hello Experts,

    I have "results.bat" file gives the output as below.

    Student Name = Harry Student Code = student_id_1 Exam Status = PASSED ------------------------------------- Student Name = Mike Student Code = student_id_2 Exam Status = PASSED ------------------------------------- Student Name = Tom Student Code = student_id_3 Exam Status = PASSED -------------------------------------

    I have written the below code to print the "Student Name & Exam status". But, all i need to print the result in horizontal as below. Please advise.

    Expecting result:

    use strict; use warnings; my ($sname,$result); my $cmd ="results.bat"; my @lines = `$cmd`; #print "@lines\n"; chomp (@lines); foreach my $line(@lines){ chomp ($line); if ($line =~ m/Student\sName\s+=\s(.*)/i){ chomp ($1); $sname =$1; print "$sname \n"; } if ($line =~ m/Exam\sStatus\s+=\s(.*)/i){ chomp ($1); $result =$1; print "$result \n"; } }

    2018-01-16 Athanasius added code and paragraph tags

sorting an array with decimal points
9 direct replies — Read more / Contribute
by levW
on Jan 14, 2018 at 02:51

    HI, need to sort the array with members that look like: Patch_1.0 Patch_2.0 Patch_3.1 Patch_5.0 Patch_4.2 Patch_6.0 Patch_7.0 Patch_8.0 Patch_9.3 Patch_10.2
    Want to get rid of string "Patch_" and compare only by the decimal point numbers.
    The following code does not sort it in perfect order, because of the decimal point :

    sort  { substr($a, 6,2) <=> substr($b, 6,2)} @array

    Would appreciate any ideas.thanks

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2018-01-19 02:07 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (215 votes). Check out past polls.