Logo

Programming-Idioms

Community backlog for Perl

Expert validation

It is extremely valuable to curate the Perl contents. Would you like to review these implementations?

If they can be improved, please help yourself. If you know a better way, please create a distinct entry. If they're broken, please flag them.

🗘

Idiom #319 Generator function

Write a function g that behaves like an iterator.
Explain if it can be used directly in a for loop.



sub _upto (&) { return $_[0]; }  # prototype & is for a function param
sub upto {
    my ($start, $end) = @_;

    my $n = $start;

    return _upto {
        return
            wantarray ? $start .. $end    :
            $n > $end ? ($n = $start, ()) :
            $n++
            ;
        };
}
my $it = upto(3, 5);
foreach ( $it->() ) { print ' ' . $_ }

Idiom #131 Successive conditions

Execute f1 if condition c1 is true, or else f2 if condition c2 is true, or else f3 if condition c3 is true.
Don't evaluate a condition when a previous condition was true.



if ($c1)    { $f1 }
elsif ($c2) { $f2 }
elsif ($c3) { $f3 }

Idiom #186 Exit program cleanly

Exit a program, indicating no error to the OS



exit;

For many more Perl snippets to curate, see the full list.

Docs & demos

🗘
45% of the Perl snippets don't have an external link to a documentation page yet. For example, these implementations would need a doc URL:

Idiom #33 Atomically read and update variable

Assign to the variable x the new value f(x), making sure that no other thread may modify x between the read and the write.


my $x :shared;
$x = 0;

sub my_task {
   my $id = shift;
   for (1 .. 5) {
      sleep 2*rand();
      { # lock scope
         lock($x);
         print "thread $id found $x\n";
         $x = $id;
         sleep 2*rand();
      }
   }
}

threads->create('my_task', $_) for 1 .. 3;
sleep 5 while threads->list(threads::running);

Idiom #179 Get center of a rectangle

Return the center c of the rectangle with coördinates(x1,y1,x2,y2)


my @c = (($x1 + $x2) / 2, ($y1 + $y2) / 2);

Idiom #70 Use clock as random generator seed

Get the current datetime and provide it as a seed to a random generator. The generator sequence will be different at each run.


srand time;

75% of the Perl snippets don't have a runnable demo yet.

For example, these implementations would need a demo URL:

Idiom #159 Trie

Define a Trie data structure, where entries have an associated value.
(Not all nodes are entries)


my $trie = Data::Trie->new;

Idiom #312 Test for list equality

Set b to true if the lists p and q have the same size and the same elements, false otherwise.


$b = Compare( \@p, \@q );

Idiom #246 Count distinct elements

Set c to the number of distinct elements in the list items.


my $c = scalar(uniq @items);

Missing implementations

🗘

A Perl implementation is missing for 38 idioms out of 372 idioms.

You may help by writing a Perl snippet for these idioms:


Idiom #358 Format a value in unit notation

Create the end-user text, s, specifying the value a, in units of x.

For example, "0 results", "1 result", or "1,000 results".


Idiom #365 Convert an angle to a direction

Convert a degree, x, of 360, to a Compass direction, y.

For example, `123.4 deg` is "South-east".


Idiom #368 Parse a millisecond value

Parse the millisecond value a, to a collection of duration values, m.

For example, `1,000,000 ms` is `{min=16, sec=40}`.

https://en.wikipedia.org/wiki/Gregorian_calendar
https://en.wikipedia.org/wiki/Time