http://qs321.pair.com?node_id=479

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
Using perl script to convert .html file to tab delimited file
2 direct replies — Read more / Contribute
by ssaahh
on Oct 01, 2020 at 05:56

    This is not a post about some debugging of code I have zero knowledge of perl.Recently I wanted to use a particular dictionary on my kindle ereader and I downloaded it from internet but the problem is that it is in .epub format which is not supported by kindle ereaders.But the dictionary is in 'dictionary format' and not a normal book .epub format.Whem I converted it to .mobi format by calibre it converted that dictionary .epub format to normal book .mobi format which means that that .mobi file is recognised by kindle as normal book and not dictionary. But I came across one post that dealt with the problem I have but it uses 2 script(one script using perl and other using pyhton) and I have not experience of programming.

    url to that post:- https://www.mobileread.com/forums/showthread.php?p=2562381#post2562381

    So,assuming that you have once went through that post I need help with how to actually use that dicthtml2tab.pl file and if you can help with this problem that would be very helpful

[OT] Next steps in using DigitalOcean to host a website running Mojolicious
4 direct replies — Read more / Contribute
by Aldebaran
on Oct 01, 2020 at 01:47

    I've never had a VPS before a fortnight ago, so I have yet to figure out much of the functionality. Also not sure how much of my question is vendor-specific. I've gotten so far as to get my domain out out the registrar's (ionos') purgatory and have the nameservers pointing to DO. Then, I sit there looking at the records page and wonder if I have more to do there...or not. (can't share the page.)

    It has a table with these values along the top:

    A AAAA CNAME MX TXT NS SRV CAA

    This is the explanation for A:

    Use @ to create the record at the root of the domain or enter a hostname to create it elsewhere. A records are for IPv4 addresses only and tell a request where your domain should direct to.

    Do I fiddle with this menu, creating a 'record', or do I get what I need by creating-apache-virtual-hosts-on-ubuntu? I've done this before to my home machine, but to put it bluntly, do I need to create a virtual host on a virtual host?

    I've been looking at the host command to see if it tells me anything relevant:

    $ host -t soa www.merrillpjensen.com www.merrillpjensen.com has SOA record ns1.digitalocean.com. hostmaster +.www.merrillpjensen.com. 1601009638 10800 3600 604800 1800

    The meaning of Start of Authority data seems to deal with varying measures of time. (1601009638 doesn't make sense to me as the first four numbers are to represent YYYY, and nothing happened in 1601 except that life was nasty, brutish, and short.)

    $ host -v www.merrillpjensen.com Trying "www.merrillpjensen.com" ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53474 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.merrillpjensen.com. IN A Received 40 bytes from 127.0.0.53#53 in 15 ms Trying "www.merrillpjensen.com" ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56934 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.merrillpjensen.com. IN AAAA Received 40 bytes from 127.0.0.53#53 in 14 ms Trying "www.merrillpjensen.com" ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57522 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.merrillpjensen.com. IN MX Received 40 bytes from 127.0.0.53#53 in 15 ms $

    My goal for this thread is to get "hello mojo world" to display. I'd like to know whether I'm doing the right thing before I do the next thing.

    Thank you for your comment,

Nested reference to an array
5 direct replies — Read more / Contribute
by Perlian
on Oct 01, 2020 at 00:12

    Dear friends, one of the things i can't wrap my mind around with perl is this: I have parsed a JSON document like the example below

    { "data": { "dividends": { "rows": [{ "exOrEffDate": "10/08/2020", "type": "CASH", "amount": "$0.52", "declarationDate": "09/25/2020", "recordDate": "10/12/2020", "paymentDate": "11/02/2020" }, { ... } ] } } }
    So basically a JSON document with an array nestded somewhere. Now if i write:
    my $Rows = $Data->{data}->{dividends}->{rows}; my @Rows = @$Rows;
    @Rows contains an array of hash-references, but if i try to dereference it in one line: my @Rows = @$Data->{data}->{dividends}->{rows}; the perl interpreter errors out with »Not an ARRAY reference«. Yes, true, $Data is a reference to a hash, but $Data->{data}->{dividends}->{rows} is a reference to an array! Please send me some wisdom! Best regards from West Virginia, Frank/2

Error with Chart::GGPlot
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 30, 2020 at 11:13

    Hello,

    I've found the Chart::GGPlot library that looks great, but unfortunatey I cannot manage to use it because I get an error.

    This is the sample code and the error.

    Thank you if you can provide me with some indication.

    Roberto

    use Chart::GGPlot qw(:all); use PDL; my $df = Data::Frame->new( columns => [ x => pdl(1,2,3,8,7), y => pdl(260,34,64,23,6) ] ); my $p = ggplot( data => $df, mapping => aes(x => 'ws', y => 'wd') )->geom_point( alpha => 0.05 ); $p->show()

    This is giving me the error:

    Error in eval_tidy('ws', ...) : Bareword "ws" not allowed while "stric +t subs" in use at make_plot_test.pl line 10. at /usr/local/share/perl/5.26.1/Eval/Quosure.pm line 76. at /usr/local/share/perl/5.26.1/Data/Frame/Partial/Eval.pm line 41.
Net::OpenSSH mater mode is not working
2 direct replies — Read more / Contribute
by leostereo
on Sep 30, 2020 at 10:02

    Hi guys.

    Im using Net::OpenSSH to connect multiple times to same host , exec some commands and grab it output.

    Problem is that I can not make module to work with master mode.

    I have read the documentation but can not get it to work.

    This is how Im creating object:

    my $ssh = Net::OpenSSH->new( $source, $login ? ( user => $login ) : (), $password ? ( password => $password ) : (), #external_master => 1, port => $port, timeout => 60, strict_mode => 0, kill_ssh_on_timeout => 1, ctl_dir => "/tmp/.libnet-openssh-perl", #ctl_path => "reusable_path", master_opts => [-o => "StrictHostKeyChecking=no"], ssh_cmd => $ssh_cmd );

    If uncommenting external_master line I get Invalid or bad combination of options ('master_opts')

    If uncommenting ctrl_path, I get "ControlSocket reusable_path already exists, disabling multiplexing",

    Dont know what to try ... I thought module needs to find existing path so it can reuse but no.

    Any help wouls be wellcome.

    Leandro

Influencing the Gconvert macro
1 direct reply — Read more / Contribute
by syphilis
on Sep 30, 2020 at 08:16
    Hi,

    This has been (partially, sort of) cross-posted at https://github.com/Perl/perl5/issues/18170.

    According to the perl source's uconfig.h:
    /* Gconvert: * This preprocessor macro is defined to convert a floating point * number to a string without a . This * emulates the behavior of sprintf("%g"), but is sometimes much mo +re * efficient. If gconvert() is not available, but gcvt() drops the * trailing decimal point, then gcvt() is used. If all else fails, * a macro using sprintf("%g") is used. Arguments for the Gconvert * macro are: value, number of digits, whether trailing zeros shoul +d * be retained, and the output buffer. * The usual values are: * d_Gconvert='gconvert((x),(n),(t),(b))' * d_Gconvert='gcvt((x),(n),(b))' * d_Gconvert='sprintf((b),"%.*g",(n),(x))' * The last two assume trailing zeros should not be kept. */
    Unfortunately, on Ubuntu-18.04, the Gconvert macro uses gcvt() - ie the second option.
    It's unfortunate because, on Ubuntu-18.04, it produces (for perls whose nvtype is 'double):
    $ perl -le 'printf "%.54g\n", 0.3;' 0.29999999999999999
    Contrast that with(eg) Windows and freebsd-12.0, both of which correctly output:
    0.299999999999999988897769753748434595763683319091796875
    The issue is that I want the Gconvert macro on my Ubuntu-18.04 perls (when nvtype is double) to use sprintf() instead of gcvt().
    That way, I'll get the correct output, as opposed to the truncated garbage that is currently being displayed.

    The question is: "How do I realize that objective ?"
    Can I do it via a configure argument when building perl?
    Or do I need to do it by hacking the perl source ?

    It's a fairly simple hack, if that's what's required.
    But I'd prefer to do it via a configure argument, if that's possible.

    Cheers,
    Rob

    PS
    I also wonder if people think that this current behaviour on Ubuntu-18.04 should be fixed.
    I think it should ... but I shy away from pushing the case because I have this notion that the vast (and I mean VAST) majority of perl practitioners really do not care about it, and would be quite happy for the current behaviour to continue.
How to use AnyEvent::Handle to read data from a websocket?
1 direct reply — Read more / Contribute
by pudda
on Sep 29, 2020 at 08:41

    Hi!

    I'm new with perl, so please have patience if I misunderstood anything, or got something wrong.

    What I'm trying to do is connect to a websocket, read the data the ws is sending and do some stuff with it. My code is connecting to the ws but when I try to read and print in the console the data readed it shows nothing. Can you guys help me?

    here's my code:

    #!/usr/bin/env perl use strict; use warnings; use lib 'lib'; use Data::Dumper; use AnyEvent; use AnyEvent::Socket; use AnyEvent::Handle; $|++; my $cv = AnyEvent->condvar; my $ws_handle; $ws_handle = AnyEvent::Handle->new( connect => [ 'localhost', '8082' ], keepalive => 1, on_eof => sub { print ("! Server disconnected"); }, on_read => sub { my ($handle) = @_; my $buf = delete $handle->{rbuf}; print "teste"; }, on_connect => sub { my ($handle, $host, $port, $retry) = @_; print "Server connected\n"; # this works }, on_connect_error => sub { my ($handle, $message) = @_; print $message; }, ); $cv->recv;
Question about regex.
6 direct replies — Read more / Contribute
by that_perl_guy
on Sep 28, 2020 at 22:10

    Hi Monks,

    I will be extremely thankful if the monks here could help me understanding regex in Perl.

    Suppose I have a text file that has these lines:

    This is line one. Line Two is this. Third line starts here. This is line four. This is line five. This is line six. This is the seventh line. This is line eight.

    If the record contains the word "third" our "four", I want it to print the whole record, meaning the stuff between the empty lines, not just lines with those words in it. But I am not able to write it correctly. Here is what I have tried:

    use strict; use warnings; open my $fh, "+<", "testlines.txt"; while (<$fh>) { if ($_=~ /(third | four)/si) { chomp; local $/ = "\n\n"; print "line is: $_\n"; } }

    And it prints:

    >perl regex.pl line is: Third line starts here. line is: This is line four.

    But what I want is:

    This is line one. Line Two is this. Third line starts here. This is line four

    Where am I going wrong? Please guide. Please note, this is just an example. Depending on the data supplied the other lines in the file may contain some different words.

    Not sure if this matters, but I'm on Windows 10 with Strawberry Perl version 5.32.

DBIx::Class : match integer-cols having specific bits set
1 direct reply — Read more / Contribute
by bliako
on Sep 28, 2020 at 14:26

    Enlightened Brothers,

    I am having bit (!) trouble with telling DBIx::Class to match records where a specific column of integer type (actually BIGINT) has specific bit(s) set. The equivalent SQL is something like: select * from TABLE where (roles & 4) = 1 (Edit: the RHS of this where expression, i.e.: =1 was written rather hastily and it does not make sense for checking if the 2nd LSbit is set. roles=4 is much clearer and probably faster. Instead,  (roles&4) = 4, (roles&4) > 0. (roles&4)=0 make more sense in my particular scenario)

    Since we are on the topic, I have not yet understood how to use the column-name in a search with DBIx::Class. For example how to DBIx::Class this: select * from TABLE where (roles & 4) = (roles & 2) ?

    I can also settle for someone telling me how to pass a custom WHERE-in-SQL using DBIx::Class::ResultSet::search(), if possible.

    Oh! MySQL latest

    bw, bliako

substrings that consist of repeating characters
14 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 27, 2020 at 13:30

    Hi. I am studying regular expressions and wanted to write a script that searches a DNA string for the longest substrings that consist of repeating letters. For example: CCCCC or GGG or AAAA etc. I managed to do that, but i am not very happy with the end resuslt. I was hoping to get most of the work done with a regex, in that regard i have failed. Furthermore there are statements in the while loop that look doubtful, and the idea of using an array to store the substring along with its length might not be good. Any advice is welcome. Thank you.

    use strict; use warnings; my $string = "AAATTTAGTTCTTAAGGCTGACATCGGTTTACGTCAGCGTTACCCCCCAAGTTATT +GGGGACTTT"; my @substrings; while($string =~ /([ACTG])(\1+)/g){ my $comb = $1.$2; my $len = length($1) + length($2); push @substrings, [$comb,$len]; } my @sorted = sort {$b->[1] <=> $a->[1]} @substrings; foreach my $substring (@sorted){ foreach my $element (@$substring){ print "$element "; } print "\n"; }

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


  • 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.