Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
weird bug with IO::Prompter, prompt spells out the word 'prompt'
2 direct replies — Read more / Contribute
by daxim
on Oct 17, 2019 at 11:01
    This works:

    › perl -MIO::Prompt=prompt -e'prompt -menu => [6..9]'

    This does not (type letters and/or Enter key when prompted):

    › perl -MIO::Prompter=prompt -e'prompt -menu => [6..9]'

    Can you repro?

Can a regular expression include an arbitrary string variable?
5 direct replies — Read more / Contribute
by anautismobserver
on Oct 16, 2019 at 21:39

    I want to search an array for an arbitrary sting variable. All the examples I've found use regular expressions. Is there a way to devise a regular expression that includes an arbitrary string variable (unknown in advance)?

    For example:

    my #string_to_find = 'hidden'; my @array_to_search = ('Foo', 'whathiddenever', 'Bar', '12hidden456');
Can't remove ANSI markup
2 direct replies — Read more / Contribute
by glendeni
on Oct 16, 2019 at 21:35

    I output some text to a terminal by inserting colors via strings ala my $red = "\033[31m"; I would like to sometimes remove that markup for printing to a normal file. But all of the following fail to remove the markup:

    ( my $nocolorsline = $line ) =~ s/\033\[[\d;]*[a-zA-Z]//g; ( my $nocolorsline = $line ) =~ s/\e\[[\d;]*[a-zA-Z]//g; ( my $nocolorsline = $line ) =~ s/\Z\[\d;]*[a-zA-Z]//g; ( my $nocolorsline = $line ) =~ s/\x1b\[[\d;]*[a-zA-Z]//g;
    What am I missing ?
    LATER - I was missing that I was changing the wrong line :-( So the above do work. Sorry for the inconvenience. Can't see how to delete this post.
Is there a term for this?
9 direct replies — Read more / Contribute
by mw
on Oct 16, 2019 at 10:28

    Dwellers of the Monastery, allow me to de-lurk, partly from a desire to commune with fellow Monks, partly from a regular frustration in on-line discussions.

    Sometimes, one must run Perl scripts on machines that one does not own, has no administrative rights on, and most significantly, one is not allowed to install any software on. In general, one is lucky if /usr/bin/perl -v returns something containing a 5. A prime example is an inventory script. One copies it to one's home directory, one runs it, and one removes it. One might be grateful that even this is allowed.

    Not all of these restrictions are even unreasonable. One does not want to upgrade a seemingly innocuous package, only to find that this upgrade breaks something important. One wishes to tread lightly, at least this one does.

    For this reason, I have on occasion been forced to write modules for which a beautiful module already exists on CPAN. I have had to produce my very own XML parser, because XML::LibXML was not installed everywhere, and I was forbidden from installing it, or anything else. People pointing out how wonderful XML::LibXML is, and that I am stupidly re-inventing a perfectly good wheel, only succeed in rubbing salt into the wound and bring me no closer to a solution.

    Does this resonate with anyone in this group? If so, is there a concise term for this kind of situation? For instance:

    • Hi, I'd like to know how to fork off several subprocesses in a controlled manner, but fork() isn't returning what I expect it to.
    • There's a great module for that on CPAN! Why don't you use that? It'll save you so much hassle!
    • I know. Can't. I'm in a nature reserve. Take nothing but logs, leave nothing but wtmp entries.

    Or something in that spirit.

[implementation specific to windows] writing a proper batch file for terminal start-up
3 direct replies — Read more / Contribute
by Aldebaran
on Oct 15, 2019 at 18:11

    I have been wanting to post more about scrabble-esque games, but I have an implementation problem that I am still struggling with. I've installed Strawberry perl, as well as git for windows, as well as a batch file from git that is to start Strawberry perl correctly which, unfortunately, I lack a reference for. The one I am going to post here has been altered from the original so as to be more verbose, at least until I get this ironed out.

    As I keep working with this script, I keep commenting out the things that I don't need. After grinding this out for a few weeks, more of it is commented out than not:

    I would try to use my script for generating the tags for a write-up and would get this:

    C:\Users\tblaz\Documents\evelyn>perl INC is C:/Strawberry/perl/site/lib C:/Strawberry/perl/vendor/lib C:/St +rawberry/perl/lib Can't locate in @INC (you may need to install the Win32 modul +e) (@INC contains: C:/Strawberry/perl/site/lib C:/Strawberry/perl/ven +dor/lib C:/Strawberry/perl/lib) at C:/Strawberry/perl/lib/ line + 605. BEGIN failed--compilation aborted.

    Then I noticed that one of the directories that had this was given my the directory in PERL5LIB. Indeed, when I comment the line out that resets this, then the script works. So I have results, yay, ...but, I would like to pause to ask whether I am doing something stupid that I will regret.

    Is PERL5LIB set to null for a reason? Am I setting up my system to be playing a tug-of-war? I have 3 different perls on this system. Should they have a single common place for new modules? In the past (distant), it's seemed like the mingw install starts to fight with strawberry perl and eventually wins, leaving the strawberry perl install outmoded and dusty.

    Is being the directory in PERL5LIB how this value entered @INC? Also, am I doing anything useful with the following commands?

    C:\Strawberry>set TERM=dumb C:\Strawberry>set PERL_JSON_BACKEND=JSON::XS C:\Strawberry>set PERL_YAML_BACKEND=YAML C:\Strawberry>rem avoid collisions with other perl stuff on your syste +m C:\Strawberry>rem set PERL5LIB= C:\Strawberry>set PERL5OPT= C:\Strawberry>set PERL_MM_OPT= C:\Strawberry>set PERL_MB_OPT=

    I hope to get back into less implementation-specific issues soon.

    Thanks for your comment,

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 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 Can't locate Date/ Permission denied at line 1. BEGIN failed--compilation aborted at line 1. [SiDMBP2:~] urinine% locate [SiDMBP2:~] urinine% cpan Can't locate Permission denied at /usr/bin/cpan5.18 line +7. BEGIN failed--compilation aborted at /usr/bin/cpan5.18 line 7. [SiDMBP2:~] urinine% locate /System/Library/Perl/5.18/ [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:


    #!/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.


    #!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.

Can't find application class in @INC when starting Mojolicious-generated app
1 direct reply — Read more / Contribute
by djevox
on Oct 12, 2019 at 09:56

    I'm completely new to Perl, so please be gentle. I generated a mojolicious app with the command mojo generate app first-app. It generated the app structure that should allow a running example when running the dev server with morbo ./script/first-app.

    The file structure looks like this:

    My error when running morbo ./script/first-app in the root of the project directory is this error:

    Can't load application from file "/home/djnorrisdev/Documents/mojo-pra +ctice/first-app/script/first-app": Can't find application class "firs +t-app" in @INC. (/home/djnorrisdev/Documents/mojo-practice/first-app/ +lib /home/djnorrisdev/perl5/perlbrew/perls/perl-5.30.0/lib/site_perl/ +5.30.0/x86_64-linux /home/djnorrisdev/perl5/perlbrew/perls/perl-5.30. +0/lib/site_perl/5.30.0 /home/djnorrisdev/perl5/perlbrew/perls/perl-5. +30.0/lib/5.30.0/x86_64-linux /home/djnorrisdev/perl5/perlbrew/perls/p +erl-5.30.0/lib/5.30.0) Compilation failed in require at (eval 72) line 1

    I tried using the full path for morbo (as mentioned in a 6 year old SO post), but that gives the same error as above. That command was this: /home/djnorrisdev/perl5/perlbrew/pls/perl-5.30.0/bin/morbo ./script/first-app

    Running morbo script/first-app gives the same error.

    I'm guessing anyone familiar with a full mojolicious app would be familiar with the script file, but here's the contents of script/first-app:
    #!/usr/bin/env perl use strict; use warnings; use Mojo::File 'curfile'; use lib curfile->dirname->sibling('lib')->to_string; use Mojolicious::Commands; # Start command line interface for application Mojolicious::Commands->start_app('first-app');

    Considering this is a mojolicious-generated app, I would assume it should not get an @INC error and run with morbo without issue. Does anyone have insight into this?

    Edit: I forgot my env info. I'm on ubuntu 18.04LTS and am using Perl 5.30.0. I do have perlbrew installed also, and all my editing is done in vscode.

New Meditations
If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
4 direct replies — Read more / Contribute
by haukex
on Oct 14, 2019 at 10:06

    Disclaimer: This post is entirely hypothetical. No decisions regarding Perl 5 have been made, or to my knowledge even been seriously discussed. It's entirely possible that Perl 5 will stay Perl 5 forever, or that the "5" will simply be dropped from the numbering (which would mean the next release of Perl is Perl 32), or something else happens.

    Now that Perl 6 has been renamed to Raku, that theoretically leaves open the possibility of Perl 5 upgrading to Perl 7. I've thought about what features would warrant a major version number change, and that's what I'm writing about here. My list is probably not complete, and I might update it if I think of more.

    First off, in my opinion, Perl should continue its tradition of being mostly backwards-compatible from release to release. If you want radical changes to the language, see Raku (and define your own slang) ;-)

    • Subroutine signatures need to come out of experimental
    • Smart matching should either be revamped, or removed entirely (Update: and if the latter, keep given as a topicalizer whose block can return a value)
    • Various other experiments should come out of experimental - e.g. refaliasing and declared_refs are IMHO pretty useful.
    • I'm not sure how this would be best implemented, but a "batteries included" Perl release would be nice. For example, one that includes Moo and other extremely common modules such as DBI. We might look to Strawberry Perl for an initial list.

    I imagine that, similar to Perl 6, a Perl 7 binary might be called perl7, with files being called .p7 or .pl7, .pm7, etc. Using this interpreter or this file extension would be the same as a use v7; (and turn on the corresponding feature bundle, etc.).

    Again, these are just hypothetical and unfinished thoughts of mine :-)

    Update: I accidentally created this post too early, sorry for all the additional edits. I'm done for now and further significant edits will be marked as such.

New Monk Discussion
Let's be the albatross
2 direct replies — Read more / Contribute
by stevieb
on Oct 15, 2019 at 17:04

    Remove yourself from political thought, frustration from things gone by strict adherence to protocol and the overall 'set in my ways, so fsck everything else'.

    Perlmonks to me is a safe place. A place where I've been able to ask questions or talk about numerous programming languages, mathematical equations, statistical analysis and things I never figured a fellow Perl programmer would understand.

    Thing is, the Perl programmers here did understand.

    I have been welcomed here for all of my technical life essentially, and I've done my best to reciprocate that back. I have respect and admiration for those who are for, and opposed to what I think. That's the premise of PM to me.

    Everyone appears to want to divide PM into this or that. We don't do politics here; we are not divisive by nature, and we shouldn't behave as such.

    As well-versed, experienced (possibly Open Source) programmers (some of us with Perl as our star language), inclusiveness is key. Whether that means allowing 'other' languages here or not is not for me to say. I will say though that I was accepted here as something, so were you, so if you're asking questions or answering them: "Where were you in life when you ran into Perlmonks. What did you acquire from the site? What did you learn from the people there?"

    Pushing off those who ask questions unrelated is our business, and we've done it for years politely. Demanding and advocating a select subset of programmers based on language isn't our business.


Inclusion of Raku on PerlMonks
6 direct replies — Read more / Contribute
by haukex
on Oct 14, 2019 at 09:52

    The document The Path to Raku includes this:

    Sites such as PerlMonks appear to be really Perl (aka Perl 5) focused, and could possible make that clear in their description, or change their description to specifically include Raku.

    Although it's true that most of the nodes posted on PM are about Perl 5, several of the gods have made it clear on several occasions that Perl 6 / Raku posts are welcome. So this node is just to point out that references to "Raku" should probably be included in the PerlMonks description, although I'm not yet sure what the best way to do so is.

    Also, I'm not sure if it's feasible to go back and change existing posts, but perhaps we should standardize a [raku] tag for post titles.

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 taking refuge in the Monastery: (5)
As of 2019-10-18 19:36 GMT
Find Nodes?
    Voting Booth?