Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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.

poll ideas quest 2020
Starts at: Jan 01, 2020 at 00:00
Ends at: Dec 31, 2020 at 23:59
Current Status: Active
8 replies by pollsters
    First, read How do I create a Poll?. Then suggest your poll here. Complete ideas are more likely to be used.

    Note that links may be used in choices but not in the title.

Perl News
Announcing Perl 7
on Jun 24, 2020 at 15:14
16 replies by marto
Reminder: The Perl and Raku Conference in the Cloud - this week!
on Jun 22, 2020 at 04:36
2 replies by haukex
$c->flash Not Dereferencing Data Objects
1 direct reply — Read more / Contribute
by phildeman
on Jul 02, 2020 at 15:48

    Hi All,

    I encountered something unusual with Perl 5.14.2.

    I am using DBIx::Class to query my database. The data object is returned to Root to be passed to a View.
    I pass the data object to the View via $c->flash. However, in the View it has a problem dereferencing the
    data object.

    For example, I query a person's name and address and store it in bio_object. Then I pass it to $c->flash like so:


    my $bio_obj = $model->get_bio_info( $id ); $c->flash->{ 'bio' } = $bio_obj; $c->res-redirect( 'myviewpage' );

    In myviewpage, using Mason in HTML page:

    <p>My name is <% $bio->name %><br /> My residence is at <% $bio->address %> <%init> my $bio = $c->flash->{ 'bio' } if $c->flash->{ 'bio' }; </%init>

    The view error message states it cannot find the method 'name' or 'address'. However, I created a test script
    that calls the same query method, "get_bio_info". I display the results without issues, using a print statement.

    Has anyone experienced this before?

Multiple numeric not or compare in if statement
7 direct replies — Read more / Contribute
by RedJeep
on Jul 02, 2020 at 00:41

    Ok, this should be very basic. For the life of me I can't figure it out. I searched and searched. Maybe you can help?

    In one of my programs I found this wrong way

    if ($sender_transaction_type !~ /3|21/)

    The above code is wrong, is bad, works sometimes. I know this is wrong. Trying to figure out the right way. I wrote the little test script below.

    In the below script the multi-compare Option Equals works fine. But the multi-compare Option Not Equal does not work. Why? How to fix?

    For example, set $a = 77 and the Option Not Equal does not work.

    Thanks for the help!

    use strict; $a = 8; #Try using 7 and see how second loop does not evaluate correct +ly ## Examples # $a = 44 evaluates ok for both options # $a = 33 evaluates ok for both options # $a = 3 does not evalute correctly on Option Not Equal # $a = 77 does not evalute correctly on Option Not Equal # $a = 8 does not evalute correctly on Option Not Equal ## Option Equal if (($a == 3) || ($a == 77) || ($a == 8)) { print "$a must be a 3 77 or 8\n"; } else { print "$a is not a 3 77 or 8\n"; } print "\n"; ## Option Not Equal if (($a != 3) || ($a != 77) || ($a != 8)) { print "$a is not a 3 77 or 8\n"; } else { print "$a is a 3 77 or 8\n"; }
Where to put data that goes with a module?
3 direct replies — Read more / Contribute
by Lady_Aleena
on Jul 02, 2020 at 00:21

    I have been documenting my Random modules in a readme for GitHub when I came across some that have external data. Two of them have text files that I open, chomp, and make into an array; nothing special. However, the data for those two are in my general data directory. If I people want to use these modules (and if I ever get them properly written), I will need to put the data closer to them. Now, I just don't know where to put them. (The other modules with external data need hashes made, and that is a bigger problem.)

    You might be wondering why I did not put the data directly into the module. It is because two of the lists are over 100 items long and the other two are over 10 with longer strings.

    I am also looking to move these Random modules away from using any of my Util modules. My Util modules are messes and for my personal use. (So many PODs to update with their dependencies, hopefully without using any of those Util modules one day.)

    I know it may be a matter of taste, but where you would put data files relative to the module that uses them?

    My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.8.8 on web host.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Is there a problem with using barewords as filehandles ?
7 direct replies — Read more / Contribute
by syphilis
on Jul 01, 2020 at 05:39

    In following the kerfuffle on p5p about the planned release of perl 7, I've noticed a repeated claim that use of barewords as filehandles will be disallowed in perl 7.
    What's so wrong with doing open RD, '<', 'file.txt' ?

    Is there some official perl documentation about any such issue ?

Perl: Extracting the value from --rsync-path=PROGRAM key/value pair
1 direct reply — Read more / Contribute
by nysus
on Jun 30, 2020 at 14:12

    I want to extract the value from a string that (I assume) could look a mix of any of the following formats:

    --rsync-path = 'blah blah' # might be spaces before/af +ter equal sign --rsync-path=/usr/bin/rsync # no quotes around value (a +ssuming this is allowed by rsync) --rsync-path="blah blah \"blah" # double quotes, with possi +ble escaped quotes --rsync-path='blah blah \'blah' # single quotes, with possi +ble escaped quotes --rsync-path='blah blah' --another-option # additional options might +follow --another-option --rsync-path='blah blah' # additional options might +precede (and follow) # any other tricky alternatives I'm forgetting?

    So basically, I want to simulate how bash extracts the value but with perl.

    I could probably create some regexes for this but I'm quite sure some obscure scenario will be left out not to mention the possibility of badly malformed user input. So is there any module out there that might make extracting this value more of a no-brainer?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Why does this non-greedy match not work?
2 direct replies — Read more / Contribute
by Special_K
on Jun 29, 2020 at 13:07

    Suppose I have the following in a text file "fbb_test":


    Now suppose I have the following script:

    #!/tool/bin/perl -w use strict; my $file = "/tmp/fbb_test"; open(FILE, $file) || die("ERROR: Unable to open $file for read, exitin +g...\n"); while (<FILE>) { chomp($_); if ($_ =~ /\/(.+?)$/) { printf("captured $1\n"); } } close(FILE);

    The script is returning "captured foo/bar/baz". Given that I specified the non-greedy operator (?), I would have expected the result to be "baz", as the non-greedy operator would have matched as few characters as possible between a forward slash and the end of the line. What am I missing here?

Reliably parsing an integer
4 direct replies — Read more / Contribute
by rdiez
on Jun 29, 2020 at 10:04

    Hi all:

    I am writing the best Perl script since sliced bread:

    But I have other, equally super scripts around. Top of the pops.

    Anyway, I do not really trust my pesky users. Some of them have been known to pass some rubbish in command-line option --resume-from-line, instead of an integer. You know, something like --resume-from-line='from yesterday, you know what I mean'. The script is also reading a file which contains file sizes, and those sizes should be integers too. But who knows what the script might encounter there.

    So I decided to do some validation, like you do in C++ or in Java: read as string, convert it to integer, and if that fails, tell the user: "Invalid integer. Sod off."

    I was also hoping that, if the number is a real integer, Perl will run faster. In any case, I need to write those values to a file, so the output must look like a valid integer too.

    Alas, I am finding it difficult to do a simple integer validation in Perl. The first thing I did is to write routine has_non_digits() in order to discard anything that is obviously not an integer, see the script I mentioned above. But it looks like that is not enough. If you try the following test code:

    my $str = "99999999999999999999";
    print "What the string is: $str\n";
    my $strAsInteger = int( $str );
    print "Value of \$strAsInteger: $strAsInteger\n";
    printf "How printf sees it: %u\n", $strAsInteger;

    You will get this result:

    What the string is: 99999999999999999999
    Value of $strAsInteger: 1e+20
    How printf sees it: 18446744073709551615

    The results are puzzling. Note that there is no sign of a hint, a warning, or anything helpful there.

    Now, let's assume for a moment that anybody of us gives a damn about writing perfect Perl scripts. What would be the best way to parse an integer?

    Because my script is not doing any arithmetic, I would like to accept integers up to the maximum integer limit. This particular script is fussy and only wants to run on Perl versions compiled with 64-bit integer support, but I have other scripts that are not so choosy, so the integer size might be 32 bits. In any case, the maximum value is not a round base-10 value like "9999".

    In the name of all users that have the privilege of running my fine scripts, I thank you all.

Async Net::AMQP::RabbitMQ
3 direct replies — Read more / Contribute
by dcpetrov
on Jun 29, 2020 at 04:56
    Hey monks, I am little bit confused of all the RabbitMQ interfaces available for Perl. I'm currently using Net::RabbitFoot which has it last update back in 2011 and Net::RabbitFoot performance compared to Net::AMQP::RabbitMQ seems to be 4 times slower. So I am trying to migrate my code to Net::AMQP::RabbitMQ which uses librammitmq underneath. What I am currently struggling with, is how to run periodic task, since $mq->recv() is blocking. With Net::RabbitFoot I currently do something similar:
    my $ae = AnyEvent->timer ( after => 5, interval => 60, cb => \&logStats, ); my $rf = Net::RabbitFoot->new()->load_xml_spec()->connect( host => 'localhost', port => 5672, user => 'guest', pass => 'guest', vhost => '/', timeout => 1, ); ... open channel and define on_consume handler ... AnyEvent->condvar->recv;
    So I am wondering how can I achieve the same using Net::AMQP::RabbitMQ. Thanks.
Simple Perl NMS Polling Engine
3 direct replies — Read more / Contribute
by mmoorreett
on Jun 26, 2020 at 22:41

    Hi Monks

    I'm looking for some high level/framework advice on how to best develop a solution to build a *simple* network polling engine. I'm not looking to re-invent the wheel or want to implement a full blown NMS, just build something thats light weight and fast/efficient to plug a gap we have at the moment. Since being tasked with this, I have researched various frameworks I have not used before for multitasking/parallelism. E.g, threads, forks, event loop frameworks, such as POE, AnyEvent, IO:Async. I've used Perl periodically for various applications but not for something like this. I've started playing with the mentioned modules/frameworks and developed some samples, but I am unsure what is the best one. At a high level I need to:

    Periodically query an external data source for device data

    ICMP Ping poll on a configurable schedule (5 minute), potentially a couple of thousand devices, in parallel, return results for further processing

    Separate to ICMP poll, collect SNMP data on a configurable schedule (5 minute), potentially a couple of thousand devices, in parallel, return results for further processing

    Each poll thread/cycle needs a "TTL", that is, each poll thread must complete by X seconds, (not talking about an ICMP/SNMP timeout)

    Needs to run on Windows and Nix perl.

    Not sure what the best approach to take is. Having looked at a couple of open source NMS solutions i found built in perl, threads/forks were used, but these are over a decade old so I'm not sure if the event loop frameworks were considered or suitable.

    Looking to get some guidance on the most appropriate path to take so I don't need to re-invent the wheel, again :).

    Thanks in advance.

Working with old code
9 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 26, 2020 at 12:31
    I have to work with old Perl code grown organically, written by others over the period of time, with 1000s of lines, and sometime with single function over 100 lines. I often think, to convert it into organized fashion with small subroutines and clear logical flows, so that I can maintain it better going forward. I am afraid to compromise business logic. What solutions are recommended ? Thanks.
Breathing life into the (Emacs) cperl-mode
1 direct reply — Read more / Contribute
by haj
on Jul 02, 2020 at 11:29
    This also appeared on

    If you are an Emacs user, you might know or even use cperl-mode. I am using it, more or less since my first days with Perl. Back then, newsgroups were a thing, and Ilya Zakharevich recommended it occasionally. In older times cperl-mode was shipped with Perl, today it is part of Emacs.

    If you use cperl-mode, you might also have had a look at the code and noticed that it hasn't seen much love in the last decade or so. Perl, on the other hand, evolves. Version 5.32, for example, brings a new infix operator, and some future version might bring Cor. Wouldn't it be nice if cperl-mode understood these new keywords?

    I'm on my way to get familiar with emacs-lisp, ERT, and other stuff to see what I can do. Ideas, contributions, comments, bug requests and criticism is welcome - There's a GitHub repository to get started.

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 perusing the Monastery: (6)
As of 2020-07-03 09:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found