Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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. 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
Encoding all HTML entities except < and >
1 direct reply — Read more / Contribute
by TrixieTang
on Oct 15, 2019 at 15:09

    How would one go about encoding all HTML entities in a string except for < and >? I've tried using HTML::Entities, but it ends up encoding < and > and breaking the HTML. I've also tried tinkering with the unsafe_characters parameter in HTML::Entities but I still can't seem to get it to allow < and >.

Error in coverage module
2 direct replies — Read more / Contribute
by ovedpo15
on Oct 15, 2019 at 11:14
    I'm trying to run the Devel::Cover module on a testing tool. I get the following error:
    unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. Possible unintended interpolation of @runs in string at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Possible unintended interpolation of @runs in string at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Bareword found where operator expected at /usr/pkgs/perl/5.14.1/lib64 +/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 836, near "$fi +le =~ q|/Devel" (Might be a runaway multi-line // string starting on line 808) Global symbol "@runs" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$run" requires explicit package name at /usr/pkgs/perl +/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 8 +08. Global symbol "@runs" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$st" requires explicit package name at /usr/pkgs/perl/ +5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 80 +8. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$run" requires explicit package name at /usr/pkgs/perl +/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 8 +08. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$st" requires explicit package name at /usr/pkgs/perl/ +5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 80 +8. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$count" requires explicit package name at /usr/pkgs/pe +rl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line + 808. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$run" requires explicit package name at /usr/pkgs/perl +/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 8 +08. Global symbol "$count" requires explicit package name at /usr/pkgs/pe +rl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line + 808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$f" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$count" requires explicit package name at /usr/pkgs/pe +rl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line + 808. Global symbol "$digest" requires explicit package name at /usr/pkgs/p +erl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm lin +e 808. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$digest" requires explicit package name at /usr/pkgs/p +erl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm lin +e 808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. /usr/pkgs/perl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cove +r/DB.pm has too many errors. Compilation failed in require at /usr/pkgs/perl/5.14.1/lib64/5.14.1/D +evel--Cover-master/lib/Devel/Cover.pm line 21. BEGIN failed--compilation aborted at /usr/pkgs/perl/5.14.1/lib64/5.14 +.1/Devel--Cover-master/lib/Devel/Cover.pm line 21. Compilation failed in require. BEGIN failed--compilation aborted.
    I guess it has to do with the following comment:
    # Sometimes the start value is undefined. It's not yet clear why, + but it # probably has something to do with the code under test forking. +We'll # just try to cope with that here.
    From the module (link: https://github.com/pjcj/Devel--Cover/commit/39ee220b1c04c442e1ab9077fea7309e2ebc9769). Have someone already had this issue and knows how to solve it?
Getting 403 from cpan.org when installing packages
1 direct reply — Read more / Contribute
by TieUpYourCamel
on Oct 14, 2019 at 16:22
    I'm attempting to install HTML::Template with cpanm in perlbrew... cpan.org is responding with a 403.
    myuser@spangled:~$ which cpanm /home/myuser/perl5/perlbrew/bin/cpanm myuser@spangled:~$ cpanm install HTML::Template --> Working on install Fetching http://www.cpan.org/authors/id/D/DA/DAGOLDEN/install-0.01.tar +.gz ... OK Configuring install-0.01 ... OK Building and testing install-0.01 ... OK Successfully installed install-0.01 --> Working on HTML::Template Fetching http://www.cpan.org/authors/id/S/SA/SAMTREGAR/HTML-Template-2 +.97.tar.gz ... OK Configuring HTML-Template-2.97 ... OK ==> Found dependencies: Test::Pod, CGI --> Working on Test::Pod Fetching http://www.cpan.org/authors/id/E/ET/ETHER/Test-Pod-1.52.tar.g +z ... OK Configuring Test-Pod-1.52 ... OK Building and testing Test-Pod-1.52 ... OK Successfully installed Test-Pod-1.52 --> Working on CGI Fetching http://www.cpan.org/authors/id/L/LE/LEEJO/CGI-4.44.tar.gz ... + OK gzip: stdin: unexpected end of file /bin/tar: Child returned status 1 /bin/tar: Error is not recoverable: exiting now ! Failed to unpack CGI-4.44.tar.gz: no directory ! Failed to fetch distribution CGI-4.44 ! Installing the dependencies failed: Module 'CGI' is not installed ! Bailing out the installation for HTML-Template-2.97. 2 distributions installed myuser@spangled:~$ wget http://www.cpan.org/authors/id/L/LE/LEEJO/CGI- +4.44.tar.gz --2019-10-14 16:03:35-- http://www.cpan.org/authors/id/L/LE/LEEJO/CGI +-4.44.tar.gz Resolving www.cpan.org (www.cpan.org)... 151.101.194.217, 151.101.130. +217, 151.101.66.217, ... Connecting to www.cpan.org (www.cpan.org)|151.101.194.217|:80... conne +cted. HTTP request sent, awaiting response... 403 FORBIDDEN 2019-10-14 16:03:36 ERROR 403: FORBIDDEN. myuser@spangled:~$
    I've tried this on different days (also tried it last Friday) and on different platforms with different perls. I always get the same result. It can't download the tar file, so it can't unzip it. I can download and unzip the file just fine on my Chrome/Windows setup. Just not from cpanm in perlbrew.
RSA.xs:194: OpenSSL error: no start line
1 direct reply — Read more / Contribute
by cinergi
on Oct 14, 2019 at 14:29

    Hello,

    I'm trying to debug the following error in a perl script that manipulates X509 certificates.

    RSA.xs:194: OpenSSL error: no start line

    My research suggests this is usually due to the certificate files not having the correct text encoding for the platform (UTF-8 for Windows in my case). However, I can read the certificates just fine using "openssl x509 -text -inform PEM -in <certfile>" from the command line, so this doesn't seem to be the case. What other causes does this error have?

    In case it doesn't show, I'm a total perl newbie. Just trying to get a script running (I did not write it).

    Thanks!

    cinergi

Issue with @INC
5 direct replies — Read more / Contribute
by sidmuchrock
on Oct 14, 2019 at 12:26

    So I was a perl 4 person for years, played in 5, scared of 6. Went to do something smarter than the old way today with dates. Old way is I would have created a @months=('January', etc.) Not even a qw as I'm that old. Anyway decided I'd go a bit more modern and use Date::Manip.pm but its not there. Okay add it. Wait strict isn't there?! My @INC doesn't seem to be playing right. Thoughts? Lemme add more danger. Strict was there earlier today as I used cpan to add DateTime. but it wouldn't install unless I used sudo cspan DateTime. It/I borked my @INC somehow. Wise Monks, what you got?

    [SiDMBP2:~] urinine% perl dog2.pl Can't locate Date/Manip.pm: Permission denied at dog2.pl line 1. BEGIN failed--compilation aborted at dog2.pl line 1. [SiDMBP2:~] urinine% locate Manip.pm [SiDMBP2:~] urinine% cpan Manip.pm Can't locate strict.pm: Permission denied at /usr/bin/cpan5.18 line +7. BEGIN failed--compilation aborted at /usr/bin/cpan5.18 line 7. [SiDMBP2:~] urinine% locate strict.pm /System/Library/Perl/5.18/strict.pm [SiDMBP2:~] urinine% perl -e 'print(join("\n", @INC))' /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.4 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .
print source golf
5 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 14, 2019 at 12:02
    Dear Monks, I was golfing around with ways to make perl print its own source and came up with something odd. I would expect the following program to print the first line of itself but it prints the entire file. Why does it do that?
    #!/usr/bin/perl use strict; use warnings; print do{@ARGV=$0;<>};
    My path thru this course:
    print do{local(@ARGV,$/)=$0;<>}; # the usual idiom print do{@ARGV=$0;local$/;<>}; print do{@ARGV=$0;$/='';<>}; # bad print do{open$_,"<$0";<$_>}; print do{@ARGV=$0;*/;<>}; # wtf print do{@ARGV=$0;<>}; # wow
Parallel::ForkManager right approach
3 direct replies — Read more / Contribute
by Takamoto
on Oct 13, 2019 at 11:05

    This is my first attempt to process things in parallel with Parallel::ForkManager on a server. I have several subrutines to collect data through APIs. I want to perform it in parallel and then merge the results. This is my script, not elegant of course, but it runs. As I do not see a huge difference in performance (time) in running things in parallel with this script or running the single subrutines one after the other (the script let me save ~1/3 of the time), just wanted to ask for your wisdom about my script

    use Parallel::ForkManager; my $max_procs = 6; my @names = qw( 0 2 3 4 5 0 ); my @DataStructure; my $pm = Parallel::ForkManager->new($max_procs, @ARGV); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_struct +ure_reference) = @_; my @results= @$data_structure_reference; if (@results){ push (@DataStructure, @results); } }); foreach my $child ( 0 .. $#names ) { my $pid = $pm->start($names[$child]) and next; my @results; if ($child eq 1){ @results=getResultsAPI_1(); } elsif ($child eq 2){ @results=getResultsAPI_2(); } elsif ($child eq 3){ @results=getResultsAPI_3(); } elsif ($child eq 4){ @results=getResultsAPI_4(); } elsif ($child eq 5){ @results=getResultsAPI_5(); } elsif ($child eq 6){ @results=getResultsAPI_6(); } $pm->finish($child, \@results); } $pm->wait_all_children;
Unit test of script fails unless Test::Warnings is used?!
1 direct reply — Read more / Contribute
by wanna_code_perl
on Oct 12, 2019 at 20:38

    Fellow Monks!

    I have a script in my latest distribution that I am adding unit tests for. I've gone with the simple caller approach:

    bin/script:

    #!/usr/bin/perl use 5.010; use strict; use warnings; main(@ARGV) unless caller; sub main { die 'main() was run' }

    This script exits with no warnings and normal status when require'd via perl -Ibin -e 'require "script";', but die()s as expected when run directly from the commandline.

    t/bin/load.t:

    #!perl use 5.010; use strict; use lib qw(bin); use Test::More; #use Test::Warnings ':all'; # <-- BEGIN { eval { require 'script' }; BAIL_OUT("bin/script did not load: $@") if $@; } done_testing;

    When run via make test, I get bin/load.t ........ skipped: (no reason given) and 255 exit status. When run via prove bin/load.t, I get the same:

    t/bin/00-run.t (Wstat: 65280 Tests: 0 Failed: 0) Non-zero exit status: 255

    I don't know why it's failing. Interestingly, it only failed when I started removing extraneous use lines from bin/load.t. Removing Test::Warnings was the one that stopped it from working! If the use Test::Warnings ':all'; line is uncommented, the test script succeeds.

    The BEGIN block seemingly makes no difference. The results are the same if the code is run outside of BEGIN { ... }. Also, if I add a die to the top of bin/script it (correctly) fails with the BAIL_OUT() message from bin/load.t.

    So the abnormal exit has me confused. Why is it failing, and how do I fix it? I guess the success under Test::Warnings should be a clue, but I would expect the tests to be more sensitive to warnings when Test::Warnings is included (due to the extra test it adds), not less sensitive.

Select Tcp Client
4 direct replies — Read more / Contribute
by PerlNewbie94
on Oct 12, 2019 at 16:44
    Hey Guys, I am working with a script which needs to handle multiple tcp clients. I have learned that this can be solved by using IO::Select. I have tested my server script with nc command in Linux and it is working ok. My issue is that i need to take input from user. But my client doesn't receive any information from the server till i have pressed enter on the client. My question is shouldn't $read_handler->can_read() get a value everytime there is incoming data from server? If a value is sent by server then client should be receiving it immediately? P.S. i am new to Perl and socket programming. Any kind of help would be greatly appreciated.
    use IO::Socket::INET; use IO::Select; # auto-flush on socket $| = 1; my $server_ip; my $server_port; #my $r,$w; @ARGV == 2 or die "IP and Portnumber needed"; ($server_ip,$server_port)=@ARGV; # create a connecting socket my $socket = new IO::Socket::INET ( PeerHost => $server_ip, PeerPort => $server_port, Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; my $read_handler = IO::Select->new(); $read_handler->add($socket); my $send_buffer; while(1) { @sockets_ready =$read_handler->can_read(); foreach my $read(@sockets_ready) { my $buffer; $buffer = <$socket>; print "$buffer\n"; } @write_ready=$read_handler->can_write(); foreach my $message(@write_ready) { my $send_buffer=<STDIN>; $message->send($send_buffer); } }
    This is my server code in case.
    #!/usr/bin/perl -w use strict; use warnings; use IO::Socket; use IO::Select; # auto-flush on socket $| = 1; my $server_ip; my $server_port; @ARGV == 2 or die "IP and Portnumber needed"; ($server_ip,$server_port)=@ARGV; # creating a listening socket my $tcp_socket = new IO::Socket::INET( LocalHost => $server_ip, LocalPort => $server_port, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "cannot create socket $!\n" unless $tcp_socket; print "server waiting for client connection on port $server_port\n"; #Create readhandlers with select my $read_select = IO::Select->new(); $read_select->add($tcp_socket); #version send to server my $Version ="Hello <VERSION>\n"; #hash for client my %clients; while(1) { my $new_readable; my $nick; ($new_readable)=IO::Select->select($read_select,undef,undef,0); foreach my $read(@$new_readable) { if($read==$tcp_socket) { my $new_connection = $read->accept(); $new_connection->send($Version); $read_select->add($new_connection); } else { my $buf; my $msg; $buf=<$read>; if($buf) { my @sockets = $read_select->can_write(); #returns a array + of handl$ foreach my $sck(@sockets) { $sck->send("$buf\n"); } } else { $read_select->remove($read); } } } } $tcp_socket->close();
MongoDB ObjectID
1 direct reply — Read more / Contribute
by betacentauri
on Oct 12, 2019 at 13:36

    O monks,

    I can't for the life of me figure out the proper way to prepare HTML references to MongoDB documents. I'm using Dancer2 with MongoDB 3.4.15, and Template Toolkit. I'm fairly new to these three monsters, and it shows.

    I can select and display documents from a collection in an HTML table. When a table row is clicked, I want to show a modal window filled with the document data, then possibly edit some data, then possibly save it back.

    My problem lies in how to build the table. I envision every row provisioned with an onclick script which receives the document _id and uses it as a key to bring the document from the database via AJAX for the modal to be filled. Now, how to go about presenting _id, which is an ObjectID, to Template Toolkit as valid HTML content for the script to consume it? As per Data::Dumper, each _id field data is '_id' => bless( {'oid' => '...funny characters...' }, 'BSON::OID' ). Understandably, I can't seem to make any other than HASHrefs to reach my HTML.

    Should I serialize every ObjectID on the way to HTML, then deserialize it to get an ObjectID again? How to achieve this? If I were to extract oid, then convert it to some visible form, could this be done into the template? I've tried with a $project stage in the aggregation pipeline but I don't understand how or where to convert oids to strings.

    Am I misdirected? Is there a simple way to do this? Thank you in advance.


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.
  • 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 making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2019-10-16 07:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?