Code Snippets Perl

Short texts Perl 🧑‍💻 Code snippets Perl ✍️ Reading lists Perl 👀

Perl threads http requests perform

use threads; use Carp; use LWP::UserAgent; use HTTP::Request::Common qw(GET POST); use JSON::Parse ‘parse_json’; our $host = ‘https://dev.d.app/api/v1/’; print “\nSTART!\n”; my $t = {}; $t->{$_} = threads->new( \&thget, $_ ) foreach (1..32); $t->{$_}->join() foreach( sort keys %$t ); print “\nFINISHED!\n”; sub ua { my $ua = LWP::UserAgent->new; $ua->default_header( ‘Accept’ => ‘application/json’ ); $ua->default_header( ‘Content-Type’ => ‘application/json’ […]

Simple utility for starting command in separate process.

#!/usr/bin/env perl use strict; use warnings; use Getopt::Long; my $stdin = “/dev/null”; my $stdout = “/dev/null”; my $stderr = “/dev/null”; sub print_help { my $message = \$stdin, ‘stdout|o=s’ => \$stdout, ‘stderr|e=s’ => \$stderr ) or print_help and exit 1; my $pid = fork; unless (defined $pid) { die “Failed to fork:\n$!\n”; } if ($pid != […]

Tail-recursive factorial function with bignums in perl

# Tail recursive factorial with bignums # Capable of handling any number. # Large numbers take a long time to compute, use wisely # # Author: Ernest Deak # License: BSD 3-Clause use strict; use warnings FATAL => ‘all’; use bignum; use Test::More tests => 4; sub factorial{ my $n = shift; my $a = […]