1
submitted 1 week ago* (last edited 1 week ago) by [M] to c/perl5@lemmy.ml
 
 

I notice that LLMs have a strange way of writing perl.
(strange until you learn it !)

Chaining ternary operators
these seem to be used in places where i would have been more familiar with a simple if/then/else conditional. This one‑liner is useful when you need to assign a value based on a series of mutually exclusive tests without writing explicit if/elsif/else blocks.

Chaining ternary operators for compact conditionals

my $age = 27;       
      
my $category = $age < 13          ? 'child'        
                          : $age < 20          ? 'teen'     
                          : $age < 65          ? 'adult'      
                          :                            'senior';      
       
print "Category: $category\n";           
# Determine a short label for a set of unrelated inputs         
my $label = $age < 13            ? 'child'       
          : $color eq 'red'              ? 'stop'         
          : $count == 0                  ? 'empty'        
          : $mode eq 'debug'        ? 'verbose'         
          : $size > 1000                 ? 'big'         
          : 'default';          
  • When condition is true, the part after the question‑mark, becomes the value of the whole expression.
  • if true, The rest of the chain (other ternary tests) are never evaluated. also including the last return value is also skipped, in this case.
  • therefore, ordering matters.
  • finally, The final : supplies the fallback value, acting as the “else” case.
  • you must be able to read it easily, but it provides terse syntax.
  • Alternatives: if this isnt appropriate for your usecase, then alternatives are: given/when, hash lookup, or simple if/else .

an extra value can be appended to the right side to return a specific false value.
otherwise, it is implied that testing false will be handled/passed by/to the next ternary in the chain.

use strict;
use warnings;

my $score = 78;          # try different values

my $result =
    $score >= 90 ? 'A: Excellent' :
    ( $score < 90 ? 'Not an A, keep trying' :
      $score >= 80 ? 'B: Good' :
      ( $score < 80 ? 'Not a B, try harder' :
        $score >= 70 ? 'C: Satisfactory' :
        ( $score < 70 ? 'Not a C, improve' :
          $score >= 60 ? 'D: Pass' :
          ( $score < 60 ? 'F: Fail – below passing mark' :
            '??' ) ) ) );

print "$result\n";

note:
Operator precedence – The ternary operators (? and :) bind loosely, so you often need parentheses when mixing them with other operators. example: $result = $a > $b ? $a : ($c < $d ? $c : $d);

advanced theoretical use:

Chaining (nesting) ternary operators

Because a ternary expression itself returns a value, you can place another ternary where either the true or false part belongs:

cond1 ? expr1
      : cond2 ? expr2
               : cond3 ? expr3
                        : expr4;
2
 
 

cross-posted from: https://programming.dev/post/3619339

Hello!

I am pleased to announce a new version of my Perl One-Liners Guide ebook.

Perl has a feature rich regular expression engine, plenty of builtin modules and a thriving ecosystem. Another advantage is that Perl is more portable. This book will show examples for filtering and substitution features, field processing, using standard and third-party modules, multiple file processing, how to construct solutions that depend on multiple records, how to compare records and fields between two or more files, how to identify duplicates while maintaining input order and so on.

Links:

I would highly appreciate it if you'd let me know how you felt about this book. It could be anything from a simple thank you, pointing out a typo, mistakes in code snippets, which aspects of the book worked for you (or didn't!) and so on. Reader feedback is essential and especially so for self-published authors.

Happy learning :)

3
 
 

cross-posted from: https://lemmy.ml/post/4027414

TIL that I can use Perl's Benchmark module to time and compare the performance of different commands in an OS-agnostic way, ie as long as Perl is installed.

For example, to benchmark curl, wget and httpie you could simply run:

$ perl -MBenchmark=:all \
     -E '$cmd_wget    = sub { system("wget  https://google.com/ > /dev/null 2>&amp;1") };' \
     -E '$cmd_curl    = sub { system("curl  https://google.com/ > /dev/null 2>&amp;1") };' \
     -E '$cmd_httpie  = sub { system("https https://google.com/ > /dev/null 2>&amp;1") };' \
     -E '$timeresult  = timethese(15, { "wget" => $cmd_wget, "curl" => $cmd_curl, "httpie" => $cmd_httpie });' \
     -E 'cmpthese($timeresult)'

which on my old T530 produces:

Benchmark: timing 15 iterations of curl, httpie, wget...

      curl:  2 wallclock secs ( 0.00 usr  0.00 sys +  0.42 cusr  0.11 csys =  0.53 CPU) @ 28.30/s (n=15)
    httpie:  8 wallclock secs ( 0.00 usr  0.01 sys +  4.63 cusr  0.79 csys =  5.43 CPU) @  2.76/s (n=15)
      wget:  3 wallclock secs ( 0.00 usr  0.00 sys +  0.53 cusr  0.19 csys =  0.72 CPU) @ 20.83/s (n=15)
    
         Rate httpie   wget   curl
httpie 2.76/s     --   -87%   -90%
wget   20.8/s   654%     --   -26%
curl   28.3/s   925%    36%     --

Very handy indeed ❤

4
 
 

I did not find anything mentioning this anywhere, but it is such a basic feature of class based programming, that I can't imagine it is not there, somewhere, somehow.

class One { method do_it {say 1} }
class Two : isa(One) { method do_it { super()->do_it; say 2} } # or so...
5
perl v5.38.0 released (perldoc.perl.org)
submitted 3 years ago by to c/perl5@lemmy.ml
6
Perl Weekly Challenge #221 (theweeklychallenge.org)
submitted 3 years ago by to c/perl5@lemmy.ml
7
Perl Weekly #620 (perlweekly.com)
submitted 3 years ago* by to c/perl5@lemmy.ml
8
 
 

Perl Weekly #619

9
 
 

v6 looks like nice and my Raku skills are definitely not at the level that I would need the (albeit very nice) MoarVM stack. So is there like a list of things that have not yet been implemented in the v6-perlito module? It's also nice that it works in different languages, javascript is very optimized and Go can be bytecompiled, so if my stuff is slow with MoarVM, I could always try those others out.