Perl language


Perl is a high-level, general-purpose, interpreted, dynamic programming language designed by Larry Wall.

Perl (Wikipedia)

Official site:
https://www.perl.org/



Perl introduction


https://perldoc.perl.org/5.32.0/perlintro.html



Tutorials


https://www.tutorialspoint.com/perl/index.htm



Examples:


Calculate square root of a number

We create a file with this code. E.g: square_big.pl
#!/usr/bin/perl

# Calculate square root of a number.

use strict;
use warnings;
use bignum;

# 500 digits maximum precision.
Math::BigFloat->precision(-500);

my $number = shift;
die "No number to calculate square root\n" unless (defined $number);

die "Number shall be greater or equal than zero\n" unless ($number >= 0);

# Hardcoded precision limit.
my $limit = 1e-490;

sub square_root {

	my $num = shift;

	my $low = 0;
	my $high = ($num < 1) ? $num: 1;

	my $av = ($low + $high) / 2;

	my $square;

	while ($high - $low > $limit) {

		$square = $av * $av;

		if ($square > $num) {
			$high = $av;
		} elsif ($square < $num) {
			$low = $av;
		} else {
			return $av;
		}

		$av = ($low + $high) / 2;
	}

	return $av;
}


print "Square root of $number: ", square_root($number), "\n";


To execute this script:
$ perl square_big.pl 2
Square root of 2: 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561854084226857



Calculate PI number

We create a file with this code. E.g: pi_big.pl
#!/usr/bin/perl -w

# Calculate PI number using this formula:
#   r[0] = 0;
#   r[i+1] = sqrt((1 + r[i]) / 2);
#   delta[i] = (2**(i+1)) * (1 - r[i]) * sqrt(1 - r[i]*r[i]);
#   pi = delta[0] + delta[1] + delta[2] + ... + delta[i] + ...  ; i = 0 .. inf

use strict;
use warnings;
use bignum;

Math::BigFloat->precision(-500);

my $digits = shift or 20;

my $limit = 1e-490;

sub square_root {

	my $num = shift;

	# print "num: $num\n";

	my $low = 0;
	my $high = ($num > 1) ? $num: 1;

	my $av = ($low + $high) / 2;

	my $square;

	while ($high - $low > $limit) {

		$square = $av * $av;

		if ($square > $num) {
			$high = $av;
		} elsif ($square < $num) {
			$low = $av;
		} else {
			return $av;
		}

		$av = ($low + $high) / 2;
	}

	return $av;
}


my $count = 0;
my $rr = 0;
my $rr_sq = 0;
my $acc = 0;
my $mult = 2;

while ($count < $digits) {

	$acc += ($mult * (1 - $rr) * square_root(1 - $rr_sq));
	$rr_sq = (1 + $rr) / 2;
	$rr = square_root($rr_sq);
	
	print "\ncount: $count -> acc: $acc\n";
	$count++;
	$mult *= 2;
}

print "PI: $acc\n";



To execute this script:
$ perl pi_big.pl 50
PI: 3.14159265358979323846264338327542627761557698854124379668821332323163585454318619321347588827072794848299290125513043752475748631743709756275331493480837846155277063608424036342205923256904780973128978200129229250763907729304707285930150362119621428986239487332176390225309519883197664464156069660363571538639339858639258251218865408882290823280739309476412316920700408387459099100919685855465403907867105565899417280273100454810773478888978992514659302910842961017524463089418768200086897001863102645