Skip to content

Instantly share code, notes, and snippets.

View rsperl's full-sized avatar

Richard rsperl

  • North Carolina, United States
View GitHub Profile
@rsperl
rsperl / list_installed_modules.pl
Last active August 4, 2021 20:01
list installed modules #perl
#!/usr/bin/env perl
#
# You can also do this (but you don't get the versions):
#
# perl -MExtUtils::Installed -e '$e = ExtUtils::Installed->new(); print join("\n", $e->modules())'
#
use strict;
@rsperl
rsperl / get_ldap_connection.pl
Last active August 4, 2021 20:01
get ldap connection base #perl #ldap
#!/usr/bin/env perl
# Getting the base from an ActiveDirectory connection
use Net::LDAP;
my $ldap = ...; # connect to ldap
my $dse = $ldap->root_dse();
my @contexts = $dse->get_value('namingContexts');
my $base;
@rsperl
rsperl / create_cpan_file.pl
Last active August 4, 2021 20:02
create cpan file using latest version of modoules #perl
#!/usr/bin/env perl
use CPAN;
my $file = shift @ARGV;
open my $fh, $file or die "Can't open $file: $!\n";
my @modules = <$fh>;
chomp @modules;
close $fh;
my $n = scalar @modules;
@rsperl
rsperl / count_occurrences.pl
Last active August 4, 2021 20:02
counting the number of occurrences of a string inside another string #perl
#!/usr/bin/env perl
use strict;
use Data::Dumper;
use Test::More;
diag "\n>>> Run as 'prove -v $0'<<<\n";
my $doc =<<EOF;
@rsperl
rsperl / clone_ds.pl
Last active August 4, 2021 20:02
clone a data structure #perl
#!/usr/bin/env perl
use strict;
use Test::More;
use Storable qw(dclone);
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
@rsperl
rsperl / capture_stdout_stderr.pl
Last active August 4, 2021 20:02
capture stderr and stdout #perl
#!/usr/bin/env perl
use strict;
use Capture::Tiny qw(capture);
my ($stdout, $stderr, $exitcode) = capture {
system("ls", "/", "/nosuchdir");
};
print "STDOUT: $stdout\n";
@rsperl
rsperl / interactive.pl
Last active August 4, 2021 20:02
check for an interactive session #perl
#!/usr/bin/env perl
# Checking for STDIN in Perl
# Use -t to test STDIN and STDOUT:
sub i_am_interactive { return -t STDIN && -t STDOUT;}
@rsperl
rsperl / bind_to_ldap.pl
Last active August 4, 2021 20:02
bind to ldap #perl #ldap
#!/usr/bin/env perl
use Net::LDAP;
my $user = $ENV{LDAP_USER};
my $pass = $ENV{LDAP_PASS};
my $host = $ENV{LDAP_HOST};
my $ldap = Net::LDAP->new($host);
die "can't connect to $host $!" unless $ldap;
@rsperl
rsperl / formatted_timestamp.sql
Last active August 4, 2021 20:02
formatted timestamp #mysql #dates
select TIME_FORMAT(SEC_TO_TIME(endtime - runtime),'%Hh %im');
@rsperl
rsperl / goplay.sh
Last active July 11, 2022 17:41
create a local go playground
#!/usr/bin/env bash
# default root directory for goplay
GOPLAY_ROOT=""$HOME/goplay""
# fn:goplay:create a go playground
function goplay() {
local module_name="${1:-anon}"
local ts="$(date +%Y-%m-%d_%H%M%S)"
local dirname="$GOPLAY_ROOT/$module_name-$ts"
local go_version=$(go version | awk '{print $3}' | sed -e 's/go//' | awk -F. '{print $1"."$2}')