Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

The Monastery Gates

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

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

Quests
poll ideas quest 2021
Starts at: Jan 01, 2021 at 00:00
Ends at: Dec 31, 2021 at 23:59
Current Status: Active
10 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
New "miniconf" scheduled for 18th of November
on Aug 24, 2021 at 10:20
0 replies by hippo

    As announced in this dev.to post there is planned to be a one-day online "miniconf" for Perl and Raku in November. It will be held via Zoom and Thibault Duponchelle has posted the call for papers.

    The talks can be about toolchain, devops, cloud, sysadmin, core, design patterns, web or whatever linked to Perl or Raku.

    🦛

The ephemeral miniconf
on Aug 24, 2021 at 10:16
0 replies by contra-sh
    The ephemeral miniconf is a Perl and Raku virtual miniconf that will take place online the 18 november 2021. Follow the link (announcement/call for papers blog post) for details :)
Supplications
How to convert hash keys to utf8
4 direct replies — Read more / Contribute
by mpersico
on Sep 21, 2021 at 22:11
    I am working with Inline::Python. In order to pass string data into Python, I am calling Data::Structure::Util::utf8_on() on all the data I am passing into any function call that is Python. It seems to work well except for the hash keys. Python complains: "TypeError: keys must be str, int, float, bool or None, not bytes at line 39". Turns out that in that module (sample here), only the values are set to utf8, not the keys.

    Is there a way to set the keys to utf8 in XS? I was hoping for a HeKEY macro, but doing some poking around, I only see keys being retrieved, not set.

    Failing that, would a simple:

    my %newhash; for (keys %oldhash) { $newhash{utf8_on($_)} = $newhash{$_}; } call_pyfunc(\%newhash);
    seem to be the right thing to do?

    Thank you.

Preventing Use of uninitialized value
6 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 20, 2021 at 15:35
    Hi there Monks!

    Is there a way to prevent "Warnings" near the "ne", Use of uninitialized value in string ne at...?
    In this line :
    ... $alldata->{ cont } ne $_->{ cont } ...

    ... return join '', map { my $data = join ' ', grep $_, @{ $_ }{ qw( year color age ) }; ( $alldata->{ cont } ne $_->{ cont } ) ? qq|<a href="index.pl?cont=$_->{ cont }">ALL FOUND $_->{ cont }</a> +<br>$data<br><span>$_->{ year }</span></li>| : (); } @{ $general->{ '111111' } }; ...

    Thanks for looking!
conditional catch-blocks 'try {} catch(COND) { }'
4 direct replies — Read more / Contribute
by LanX
on Sep 19, 2021 at 16:30
    Hi

    this is part of a longer meditation, but I want to keep it short:

    I want to be able to catch error-objects thrown with die and I want to have an easy syntax:

    the JS model is reproducible 1-to-1 with Try::Tiny it's even easier because Perl's $_ is automatically set to JS's e

    from try...catch#conditional_catch-blocks

    try { myroutine(); // may throw three types of exceptions } catch (e) { if (e instanceof TypeError) { // statements to handle TypeError exceptions } else if (e instanceof RangeError) { // statements to handle RangeError exceptions } else if (e instanceof EvalError) { // statements to handle EvalError exceptions } else { // statements to handle any unspecified exceptions logMyErrors(e); // pass exception object to error handler } }

    But in most cases the final else will just do die $_ to propagate the error to higher call levels, hence it's boilerplate.

    Python has a model for this by providing a condition after the 'catch', if non is matched the error is raised again.

    from https://pythonbasics.org/try-except/

    try: # your code here except FileNotFoundError: # handle exception except IsADirectoryError: # handle exception except: # * # all other types of exceptions print('Should reach here')

    (* the last except must be removed to automatically raise the error again)

    So ideally one could define in Perl a prototype for catch (COND) { CODE } where the (COND) part is optional. Alas that's not possible in Perl, even with mandatory (COND) (otherwise experiments with syntax extension were easy)

    Three workarounds come into mind

    1. a bail out function only {} (or maybe handle {} )

    try { # your code here } catch { only { FileNotFoundError }; # # handle exception } catch { only { IsADirectoryError }; # handle exception } catch { # * # all other types of exceptions }; print('Should reach here')

    (* again, if the simple catch is removed the error is automatically thrown again die $_ )

    ( the COND is either a boolean expression or a constant resp. object representing an error-class. Since an error-class is blessed into a type like "ErrorClass" this can be tested by only ... hence a shorthand for $_->isa(FileNotFoundError) ... not sure if Python has the same flexibility ;)

    2. extend catch (&;&) {...} with optional second sub {}

    try { # your code here } catch { FileNotFoundError } sub { # handle exception }, catch { IsADirectoryError } sub { # handle exception }, catch { # * # all other types of exceptions }; print('Should reach here')

    UPDATE: added commas, plz see here why.

    3. as a variant of 2. use an "underscore" sub _ as syntactic sugar to chain code-blocks

    with sub _ (&;@){ return @_ }

    try { # your code here } catch { FileNotFoundError }_ { # handle exception } catch { IsADirectoryError }_ { # handle exception } catch { # * # all other types of exceptions }; print('Should reach here')

    This variant could help implementing a future built-in syntax catch () {} by parsing {...}_ as (...)

    Like that module authors wanting to be backwards compatible to older versions could keep writing {...}_ without loosing the full performance of newer versions.

    And new syntax for compound statements could be experimentally implemented and tested.

    finally

    I wanted to keep it short and didn't show much implementation detail. I'm more interested in comments regarding the interface... or probably I'm missing a good CPAN module already?

    comments? suggestions?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2021-09-24 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?