in reply to 5.40 released

I'm one of those old-school people, so i followed ye olde https://cpan.org/src instructions as usual, e.g.

wget https://www.cpan.org/src/5.0/perl-5.40.0.tar.gz tar -xzf perl-5.40.0.tar.gz cd perl-5.40.0 ./Configure -des -Dprefix=/home/cavac/bin/CavacPerl-5.40.0 make make test make install

And, as usual, everything works out fine on Xubuntu 22.04 LTS with the latest OS updates(*). And now i can FINALLY write cleaner code:

#!/usr/bin/env perl use v5.40; use strict; # <-- technically not required, but makes it clear to the +reader we are using strict use warnings; # <-- technically not required (i think), but makes it c +lear to the reader we are using warnings # <- no *EXPERIMENTAL* stuff here for(my $i = 0; $i < 10; $i++) { # Note to reader: Yes, could have used a trinary here, but those a +re hard to read # and therefore forbidden in my codebase print "$i is "; if(!iseven($i)) { print "NOT "; } print "even\n"; } sub iseven($val) { if($val % 2 == 0) { return true; # !!!!! A NATIVE BOOLEAN!!!! } return false; # The *other*, more pessimistic, native boolean ;-) }


* Famous last words. Still installing all my codebases, but so far everything looks peachy.

Replies are listed 'Best First'.
Re^2: 5.40 released- native boolean- convert to [01] without any warning|error
by parv (Parson) on Jun 12, 2024 at 22:12 UTC
      Why would you want it to issue a warning in that context? Seems consistent with
      print $some[ 1 > 0 ], q[ ], $some[ 1 < 0 ];

        Because not to confuse|treat a boolean as integer as it happens in Lua, Ruby (someone else mentioned), Java, & Rust (code+compiler error for last 2 is below).

        A C++ compiler will not complain on implicit conversion (per casual search; a Clang linter warning exists: readability-implicit-bool-conversion).

        In context of Perl, was hoping that "native boolean" -- as mentioned earlier -- would actually be so and no more, to, perhaps, slowly (very) update Perl😐

Re^2: 5.40 released
by Anonymous Monk on Jun 12, 2024 at 19:27 UTC
    for(my $i = 0; $i < 10; $i++) { # Note to reader: Yes, could have used a trinary here, but those a +re hard to read # and therefore forbidden in my codebase print "$i is "; if(!iseven($i)) { print "NOT "; } print "even\n"; }

    The easiest code to read does not have to be read at all because the comments tell you what's happening. If one must write readable Perl then one should use its features instead writing something that looks like C. This is more perlish and way easier to read:

    for (0 .. 9) { print "$_ is "; print "NOT " if not iseven($_); print "even\n"; }

    Code should be optimized for efficient execution more than readability. Here Perl does both, since creating blocks is more expensive than postfix logic, especially inside loops. Peace

      He was simply showing some of the features of the 5.40.0 release, not trying to demonstrate some beautiful or optimized code, making such nitpicking seem pretty out of place and off topic.

        I semi-agree with anonymonk here - the Cish cruft got in the way of me seeing the point trying to be made. I saw C code and essentially skipped everything else. I find it really hard to imagine why you would want to write C in Perl as a preferred option. Why not just write C in C?

        However I agree with cavac that the implicit variable should be avoided, but not to the extent of eschewing map and grep etc.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        I guess you're right about being off topic. Sorry I was just trying to be informative. Was triggered by reading that trinary (sic) is hard to read after seeing a multiple parameter loop in perl that was supposed to be easy to read... 🤷

        $ternary = $can ? be_really_easy : to_read_btw;

      Implicit variables, e.g. $_, are banned from my project. If you use a variable, you have to name it. This makes it much clearer on first glance on what data you are working. And it makes it much easier to move around.

      I should note that i'm paid to develop commercial software, meaning i always have some junior developers come and go. The goal with my code design is to make it easy to convert them from other languages. And i often have to convert code to (or from) Perl to other languages. "The most perlish way" is nearly always a bad idea, whereas a C-like for loop is available in most languages.

        > Implicit variables, e.g. $_, are banned from my project.

        Does it mean map and grep are banned, too? As is sort?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Implicit variables, e.g. $_, are banned from my project. If you use a variable, you have to name it.

        I agree with gramps on this one; implicit variables should be avoided ... but not to the extent of eschewing map, grep, sort etc.

        I should note that i'm paid to develop commercial software, meaning i always have some junior developers come and go

        Senior developers come and go too. What happens today at your company when you are away on leave? What is the company's succession plan if you decide to leave for greener pastures?

        "The most perlish way" is nearly always a bad idea, whereas a C-like for loop is available in most languages.

        It may depend on the company and its size, but I disagree with this general approach. In large companies I've worked for, it was common to have C++ specialists and Perl specialists. I would expect to see Perl code written idiomatically. Ditto for C++. And I would pull a face at code review if I spotted a C-style for loop in Perl code. :-)

        Generally, I'm a fan of chromatic's emphasis on maintainability over "readability", described at Readability vs Maintainability References (update: note the classic "the determined Real Programmer can write FORTRAN programs in any language" and "thankfully there is not so much Fortranish Perl out there" quotes in hippo's reply ;-).

        👁️🍾👍🦟

        I use map a fair bit in instances like this:

          $sth_insert->execute ( map { $fields{ $_ } } qw/ITEMNO OPTFIELD AUDTUSER AUDTORG VALUE TYPE LENGTH DECIMALS ALLOWNULL VALIDATE SWSET/ ) or $log->logdie ( "Error with $insert_cmd: " . $sth_insert->errstr );
        In that very limited scope, I feel it's fairly clear what $_ is. Another example:
          my @good = grep { $_->{'result'} != 0 } @list; my @bad = grep { $_->{'result'} == 0 } @list;
        (And I'm sure there's a clever way to do that in one statement, and maybe someone smarter than me will suggest it.) Finally, this is also handy:
          for ( 0..2 ) { defined $item_image_url->[ $_ ] or last; $new_values{ "ITEMIMAGEUR$endings[ $_ ]" } = $item_image_url->[ $_ ] // ''; }
        That's about the largest scope that I use $_ for. After that, I tend to use $k (for key) when I'm iterating over a hash:
          foreach my $k ( keys %DBhandles ) { $DBhandles{ $k }{ sth }->finish; $DBhandles{ $k }{ dbh }->disconnect; }
        I get that having $_ appear in code can be a little odd when the scope is anything larger than Quite Small -- it stops you from grokking the code and makes you go back and figure out what the heck the value is, and where it came from.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.