This is PerlMonks "Mobile"

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

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.

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.

Post a new question!

User Questions
Unexpected line breaks in substitution results
2 direct replies — Read more / Contribute
by paschacroutt
on Apr 10, 2024 at 10:22
    Hi monks,

    What follows is my very first adventure in perl world, so please be merciful.

    I wrote the following code to modify a bunch of pandoc-generated DokuWiki formatted files in order to convert some expressions to DokuWiki internal links. That went through many iterations and I am now somewhat satisfied with the result except for one mystery I am unable to pierce through.
    It does not crash my code, nor does it sends any warning, but I can't explain this result.

    Here's my problem:

    In this substitution part of the code: .(defined($4) ? $4 =~ tr[\/*][]dr : '.') line 44, I check if the value of $4 is defined and if it is, I apply a tr to it to remove italic (//) or bold (**) marks if they are present. If $4 is undefined (meaning there are no dot, comma or semicolon after the last word of the expression), the conditional operator sends a dot to end the substitution.

    So either this data entry:

    **Voir :** proton, solution hydrogénée//.//
    or that one:
    **Voir :** proton, solution hydrogénée.
    Give this result:
    **Voir :** [[glossaire:entrees:p:proton|proton]], [[glossaire:entrees: +s:solution_hydrogenee|solution hydrogénée]].
    Which is exactly what is needed.
    But if there is no final dot or comma after solution hydrogénée:
    **Voir :** proton, solution hydrogénée
    What I get is:
    **Voir :** [[glossaire:entrees:p:proton|proton]], [[glossaire:entrees: +s:solution_hydrogenee |solution hydrogénée ]].
    With line breaks after solution_hydrogene and hydrogénée
    Which is ok but I was expecting:
    **Voir :** [[glossaire:entrees:p:proton|proton]], [[glossaire:entrees: +s:solution_hydrogenee|solution hydrogénée]].
    No line breaks.
    And I don't understand why and how those line breaks get inserted.
    Actually, those breaks don't really matter, as DokuWiki format being some kind of lesser markdown, it just gives the same html output with or without them, but it worries me as a sign of my incomplete understanding of my own code.

    It may have something to do with the /x modifier I suspect.

    The actual code follows

    Thanks for reading through !

replacement for Auth::GoogleAuth
1 direct reply — Read more / Contribute
by ForgotPasswordAgain
on Apr 09, 2024 at 12:24
    We currently use qr_code of Auth::GoogleAuth to generate QR codes for Google Authenticator, but recently found out that the underlying Google Image Charts was deprecated in 2012... (apparently it's periodically shut off to "notify" users of it going away). Anyone know of an easy replacement or have any recommendations?
Parallel::ForkManager install fails
5 direct replies — Read more / Contribute
by cormanaz
on Apr 07, 2024 at 16:30
    When I try to install Parallel::ForkManager on Windoze 10 it fails:
    Test Summary Report ------------------- t/start_child.t (Wstat: 1280 Tests: 0 Failed: 0) Non-zero exit status: 5 Parse errors: Bad plan. You planned 2 tests but ran 0. t/waitpid-conflict.t (Wstat: 0 Tests: 2 Failed: 0) TODO passed: 1-2 t/waitpid-waitonechild.t (Wstat: 0 Tests: 3 Failed: 0) TODO passed: 2-3 Files=12, Tests=26, 74 wallclock secs ( 0.05 usr + 0.02 sys = 0.06 C +PU) Result: FAIL Failed 1/12 test programs. 0/26 subtests failed. dmake.exe: Error code 255, while making 'test_dynamic' -> FAIL Installing Parallel::ForkManager failed. See C:\Users\boss\.cp +anm\work\1712513913.232104\build.log for details. Retry with --force +to force install it. Expiring 2 work directories.
    The docs list no test results of the current version on Windoze. Is there anything I can do to fix this?
converting time zone CET Greenwitch
2 direct replies — Read more / Contribute
by Takamoto
on Apr 07, 2024 at 09:27

    Hi

    I am doing some timezone conversions, and I am realizing that this has some cavetas I am not understanding. In this particular case I am trying to convert from CET to Greenwitch. This is one hour difference, yet this script gives me two. It has probably to do with summer/wintertime? Which is the best approach to have precise conversions?

    use DateTime; use DateTime::Format::Strptime; my $db_update_time = '2024-04-07 15:03:27'; my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %T', locale => 'en_US', time_zone => 'Europe/Berlin', on_error => 'croak', ); my $datetime = $parser->parse_datetime($db_update_time); $datetime->set_time_zone('Greenwich'); $formatted_time = $datetime->strftime('%Y-%m-%d %T'); print $formatted_time; #output: 2024-04-07 13:03:27
Server Time
2 direct replies — Read more / Contribute
by Bod
on Apr 07, 2024 at 06:56

    We've started the process of moving hosting server to a cheap VPS - just non-essential (read "hobby") sites for now whilst we get to grips with it and I learn how to manage a server...

    All is going relatively well so far, but I've found an anomaly with time settings. Can anyone explain what is going on?

    The 'old' server observes DST whereas the 'new' server doesn't. If I run timedatectl on the 'new' server it tells me it is set to UTC. I don't have access to run the same command on the 'old' server.

    Here in the UK we are now on BST (GMT+1) so things that are time dependent (like Google Calendar feeds) that have been moved over are all 1 hour out. If I get the time from the MariaDB database SELECT NOW() I get GMT, as expected.

    However, I have a bit of test code left in a page that only I use. It's a bit of JavaScript document.write(document.lastModified); which shows the time in BST. Doen't that time get passed in the HTTP headers from the Perl generated web page? Perl is, of course, also reporting GMT.

    The obvious solution seems to be to change the server time from UTC to GMT.
    Will that then observe UK DST?
    Are there any reasons not to change the server to GMT bearing in mind that the entire codebase was written on a server that observes DST?

    Update: - the "obvious" option to set would be 'Europe/London' but that is not included in timedatectl list-timezones

Parallel:ForkManager how to pass back a list
3 direct replies — Read more / Contribute
by cormanaz
on Apr 05, 2024 at 12:12
    I have code where a forked process is generating a list in $data and passing that to the callback like in this simplified version:
    use strict; use feature ':5.10'; use Parallel::ForkManager; our $pm = Parallel::ForkManager->new(8); $pm -> run_on_finish ( # called BEFORE the first call to start() sub { my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_ +structure_reference) = @_; # retrieve data structure from child if (defined($data_structure_reference)) { # children are not +forced to send anything my $string = ${$data_structure_reference}; # child passe +d a string reference if ($string) { #how to process $string? }; } else { # problems occurring during storage or retrieval will +throw a warning say qq|No message received from child process $pid!|; } } ); LOOP: foreach my $instance (0..100) { $pm->start and next LOOP; # do the fork my $data; foreach my $i (0..50) { push(@$data,int(rand(10)); } $pm->finish(0,\$data); # exit the child process } $pm->wait_all_children;
    The docs say you can use this method to return a data structure, but they refer to the thing returned as a string. Is $string really a string or is it an object?
Does the experimental builtin::stringify() do anything that scalar() doesn't ?
2 direct replies — Read more / Contribute
by syphilis
on Apr 05, 2024 at 01:11
    Hi,

    I was looking at https://github.com/Perl/perl5/issues/22118 and, on perl-5.39.9 devel release, went looking for the difference between calling builtin::stringify() and scalar().
    This somewhat simplistic little script failed to find any difference:
    use strict; use warnings; use experimental qw(builtin); my @x = qw(one two three); my $_1st = builtin::stringify @x; my $_2nd = scalar @x; print $_1st, "\n", $_2nd, "\n\n"; $_1st = builtin::stringify "@x"; $_2nd = scalar "@x"; print $_1st, "\n", $_2nd, "\n\n"; print builtin::stringify @x, "\n"; print scalar @x, "\n\n"; print builtin::stringify "@x", "\n"; print scalar "@x", "\n\n"; # UPDATE: remove duplicate test #print builtin::stringify @x, "\n"; #print scalar @x, "\n\n";
    Presumably, there are circumstances where builtin::stringify() and scalar() are not the same thing .... right ???

    Cheers,
    Rob
Perl autovivifyies object property when using exists
5 direct replies — Read more / Contribute
by nysus
on Apr 03, 2024 at 13:33

    Got this bit of code in a test:

    say (exists $fs4->{_structure}{'/Users/dotfiles/perl5/my_modules'}{BLA +H}{Crap}); # BLAH and its parent do not exist d $fs4; # dumps the object

    The dump of the object now shows this:

    ### Dump from: 01-FSO_basic.t line: 35 $VAR1 = { '/Users/dotfiles/perl5/my_modules' => { 'BLAH' => {} },

    I was pretty surprised to see the path and BLAH in the output as keys. So why is 'BLAH' key getting autovivified? Second, how I work around this?

    Update: This one-liner reproduces the problem for me:

    perl -MData::Dump -E ' my $fs4 = {'_structure' => {}}; exists $fs4->{_ +structure}{BLAH}{TEST}{Crap}; dd $fs4->{_structure} '

    Outputs:  { BLAH => { TEST => {} } }

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

What is your favourite Perl-based forum software?
3 direct replies — Read more / Contribute
by hippo
on Apr 03, 2024 at 09:08

    There is a site I manage which is a good candidate for a forum. Here's what I'm after:

    • Written in Perl (of course)
    • Threaded discussions
    • Backed by an open source DB or similarly open storage mechanism
    • At least some sort of moderation system

    Hopefully that's not too much to ask for. If you know of (or even better, use) such an application please reply with the details.

    And yes, I do realise I'm asking this on precisely the type of platform I'm looking for but the gods and pmdevs constantly moan about how unmaintainable it is so perhaps it isn't a great option from a backend point of view. It is lovely to use as a normie, though. :-)


    🦛

How can I make a string unique (quicker than my approach at least)
8 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 01, 2024 at 17:15
    Dear Monks, I have several thousands of lines that have a format like the following:
    ID1<TAB>name1-name2-name3-name4....-nameX ID2<TAB>name1-name2-name3-name4....-nameX ID3<TAB>name1-name2-name3-name4....-nameX ...

    What I want to achieve is to keep each of the IDs as they appear and ONLY the unique names that appear on the second column. For instance, imagine:
    ID1<TAB>nick-john-helena ID2<TAB>george-andreas-lisa-anna-matthew-andreas-lisa ID3<TAB>olivia-niels-peter-lars-niels-lars-olivia-olivia ...

    my output should be:
    ID1<TAB>nick-john-helena ID2<TAB>george-andreas-lisa-anna-matthew ID3<TAB>olivia-niels-peter-lars

    What I am looking is to see if someone has a quick solution to this. My approach would be to read each line, store the ID and the line of names, split the names using - as delimiter, put all names into an temp array on the fly and then make this array unique and print the unique elements. Any more clever solution perhaps?

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":