Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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
9 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
Firefox warns that cookies generated by Catalyst will be rejected in future
1 direct reply — Read more / Contribute
by martell
on Jul 04, 2020 at 10:32

    Dear Monks,

    I'm using Catalyst::Plugin::Session::State::Cookie to have sessions within my pages. While developing I noticed that Firefox in its most recent version is complaining in the console that my cookies are misusing the sameSite attribute:

    Cookie “my_site_session” will be soon rejected because it has the “sam +eSite” attribute set to “none” or an invalid value, without the “secu +re” attribute. To know more about the “sameSite“ attribute, read http +s:// p +hotos

    In catalyst I have following code to configure my site and the Cookie Plugin:

    use Catalyst qw/ ConfigLoader Static::Simple Session Session::Store::FastMmap Session::State::Cookie Authentication Authorization::Roles /;

    If found that I can set the secure flag by adding to my config:

    __PACKAGE__->config('Plugin::Session' => { cookie_secure => 1, });

    This solves my immediate concern because now Firefox doesn't complain any more. (In reality I use the value "2" because on my development machine I don't use https .). But it doesn't feel like a fundamental good solution.

    Looking through the code of the module I don't see the variable "SameSite" explicitly set. Hence I suppose Firefox detects it as having the value "None". I'm not familiar with the innards of Catalyst and I don't know if the cookie is handled somewhere else in Catalyst before sending. But I was wondering if this module should not set the "SameSite" variable in a Cookie by default to "Lax" as it is the expected default.

    Any thoughts, insights on this observation before I report it as a bug on the module?

    Kind regards

Caching DB rows transparently and with SQL search
3 direct replies — Read more / Contribute
by bliako
on Jul 04, 2020 at 07:13

    Esteemed Monks, I need some advice on DB caching,

    A typical web-app, let's say using Mojolicious and DBIx::Class, has a lot of semi-static data stored in the DB. For example the list of registered users, the set of allowed tags, phone numbers of associates. This data does not change often (perhaps once a week/month) so why do I fetch it every time a user looks up another user's phone. (forget at the moment issues of privacy - all users are allowed to see all other users details). Another example is a business phone directory with 100 entries. Fairly static and very much in use. This data is not so big in size so I want to keep it in the memory of my app and serve this instead of fetching it from db each time a webpage with a dropdown menu of all phones is loaded.

    So, I thought on starting up the app, create a helper and store these semi-static data (fetched once and only from DB). Then serve this and never bother the DB again. That's fine but how about providing SQL-style search for this data? Because sometimes I will get requests from the client (e.g. when it loads a user's profile page) of /user/list?id=xyz in addition to /usr/list?id=all and also /usr/list?name=*abc*. These are all implemented as SQL searches in DBIx::Class and work fine but they are not cached.

    This is what I would like (and this is the 21st century and I want to have it): either tell DBIx::Class that specific table should be cached unconditionally until I say so. This transparency will be ideal for using existing code. OR, less preferred, store the static data in Perl and via a module have SQL searches on it, as if it was residing in a DB.

    thanks, bw bliako

    EDIT: I have asked a similar question at (which was part of a more general question)

$c->flash Not Dereferencing Data Objects
2 direct replies — 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
2 direct replies — 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
5 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.
Breathing life into the (Emacs) cperl-mode
2 direct replies — 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 pondering the Monastery: (2)
As of 2020-07-05 04:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found