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 #27 Create a 3-dimensional array

Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.



my $array3d = [
    [ [ 1, 0, 1 ],
      [ 0, 0, 0 ],
      [ 1, 0, 1 ] ],
    [ [ 0, 0, 0 ],
      [ 0, 2, 0 ],
      [ 0, 0, 0 ] ],
    [ [ 3, 0, 3, ],
      [ 0, 0, 0, ],
      [ 3, 0, 3, ] ]
];

Idiom #127 Source code inclusion

Import the source code for the function foo body from a file "foobody.txt".



sub foo {
    do './foobody.txt';
}

Idiom #200 Return hypotenuse

Compute the hypotenuse h of the triangle where the sides adjacent to the square angle have lengths x and y.



my $h = hypot $x, $y;

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 #80 Truncate floating point number to integer

Declare integer y and initialize it with the value of floating point number x . Ignore non-integer digits of x .
Make sure to truncate towards zero: a negative x must yield the closest greater integer (not lesser).


my $y = int($x);

Idiom #67 Binomial coefficient "n choose k"

Calculate binom(n, k) = n! / (k! * (n-k)!). Use an integer type able to handle huge numbers.


sub binom {
   my ($n, $k) = @_;
   my $fact = sub {
      my $n = shift;
      return $n<2 ? 1 : $n * $fact->($n-1);
   };
   return $fact->($n) / ($fact->($k) * ($fact->($n-$k)));
}

Idiom #117 Get list size

Set n to the number of elements of the list x.


my $N = @x;

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

For example, these implementations would need a demo URL:

Idiom #209 Type with automatic deep deallocation

Declare a type t which contains a string s and an integer array n with variable size, and allocate a variable v of type t. Allocate v.s and v.n and set them to the values "Hello, world!" for s and [1,4,9,16,25], respectively. Deallocate v, automatically deallocating v.s and v.n (no memory leaks).


class T {
    has 's', is => 'ro', isa => Str;
    has 'n', is => 'ro', isa => ArrayRef[Int];
}

{
    my $v = T->new(s => 'Hello, world!', n => [1,4,9,16,25]);
    # deallocation happens at closing brace, see explanation
}

Idiom #3 Create a procedure

Like a function which doesn't return any value, thus has only side effects (e.g. Print to standard output)


sub some_procedure {
    print 'some side effect';
    return;
}

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items


while (my ($i, $x) = each @items) {
    print "array[$i] = $x\n";
}

Missing implementations

🗘

A Perl implementation is missing for 12 idioms out of 344 idioms.

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


Idiom #341 Find substring last position

Set i to the position of the last occurrence of the string y inside the string x, if exists.

Specify if i should be regarded as a character index or as a byte index.

Explain the behavior when y is not contained in x.


Idiom #342 Leap year?

Determine if the current year is a leap year.


Idiom #345 Convert string to big integer

Create the integer value i initialized from its string representation s (in radix 10)

Use an integer type that can hold huge values. Explain what happens if s cannot be parsed.