Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
Force Getopt::Long to Require ONLY Equals Sign Between Option Name and Value
3 direct replies — Read more / Contribute
by roho
on Oct 18, 2019 at 16:36
    Here is what I am trying to do. I have a command line option that is defined in Getopt::Long as having an optional string value as follows:

    use Getopt::Long; GetOptions( "ot:s" => \$ot ) or die("Error in command line arguments\n +");

    The problem occurs when -ot (without a value) is followed by either another option or a program argument. The -ot option will take the next word as its value.

    I know I can signal the end of options by using -- on the command line, but for backward compatibility before this program used Getopt::Long (the program previously used "/usr/bin/perl -s" to process command line options), I would like to tell Getopt::Long to always require an equals sign before a value so that -ot (without a value) can be followed by a program argument without the invervening -- in the command line.

    Here is what I am aiming for:

    Scenario #1: -ot=123 arg1 arg2 ...

    This works fine. Variable $ot='123'

    Scenario #2: -ot arg1 arg2 ...

    I need $ot to be set to the empty string '' instead of 'arg1', without the need of an intervening -- on the command line (hence the need to force Getopt::Long to require an equals sign between the option name and its value).

    Again, my goal is backward compatibility with the way this program was called before adding Getopt::Long, because this program is very central to the system and is literally called in thousands of places. TIA for any suggestions.

    "It's not how hard you work, it's how much you get done."

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.

New Meditations
If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
5 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 cooling their heels in the Monastery: (4)
As of 2019-10-19 23:13 GMT
Find Nodes?
    Voting Booth?