Skip to content

Instantly share code, notes, and snippets.

@AndrewRussellHayes
AndrewRussellHayes / addDashes.pl
Created November 20, 2013 20:13
Add Dashes to date in file name. Takes: "part_part_20120513_part_part.ext" and makes: "part_part_2012-05-13_part_part.ext".
sub addDashes
{
my $originalFileName = shift;
#split along uscores into array
my @parts = split('_',$originalFileName);
# take the third index of the array and add dashes in this pattern ####-##-## and put it back in the array
$parts[2] = join('-', unpack('A4A2A2', $parts[2]));
@AndrewRussellHayes
AndrewRussellHayes / dirByDate.pl
Last active December 28, 2015 22:09
reads and lists a directory forwards and backwards by date
my $path = '/home/opuperl';
opendir my($dirh), $path or die "can't opendir $path: $!";
my @flist = sort { -M $a <=> -M $b } # sort by mod time
map { "$path/$_" } # need full paths for sort
grep ! m/^\./ # remove dotfiles
,readdir $dirh;
closedir $dirh;
@AndrewRussellHayes
AndrewRussellHayes / flipSwitch.pl
Created November 20, 2013 17:27
flips single file switch, used for development purposes typically
sub flipSwitch{
my $file = shift;
my $fileHandle;
my $value;
open($fileHandle,'<',$file) || warn "cant open switch file $!";
$value = <$fileHandle>;
close($fileHandle);
chomp($value);
@AndrewRussellHayes
AndrewRussellHayes / basicUnixMenu.pl
Last active December 28, 2015 21:49
prints basic unix menu and handles input
sub menu{
manager::clearScreen();
my $menuText = "\n".$MENUPARTITION;
$menuText .= "\t MENU\n";
$menuText .= $MENUPARTITION;
$menuText .= " To Select an option, type\n it's number and press Enter.\n";
$menuText .= $MENUPARTITION;
$menuText .= " 1. Start Menu\n";
$menuText .= " 2. Stop Menu\n";
$menuText .= " 3. Change System Setting\n";
@AndrewRussellHayes
AndrewRussellHayes / continueUntilEnterKey.pl
Last active December 28, 2015 21:49
this code will keep running until enter is pressed and then continue running.
while(){
runUntilEnterPressed();
enterWasPressed();
}
sub runUntilEnterPressed{
my $input;
my $select_object = IO::Select->new(); $select_object->add(\*STDIN);
my $sleep = .1;
until($input){
@AndrewRussellHayes
AndrewRussellHayes / dynamicStringSeparator.pl
Created November 20, 2013 17:08
dynamically generates equal sized dividers for a row of display content based upon the length of the desired line, number of columns, its content and available empty space.
my $PARTITION = "================================================================================\n";
my $PARTITIONLENGTH = length($PARTITION)-1;#-1 to remove newline from count
my $emptySpaceLen = $PARTITIONLENGTH - length($partOne.$partTwo.$partThree.$partFour);
my $divider = makeDivider($emptySpaceLen, 4);
my $return = "$partOne$divider$partTwo$divider$partThree$divider$partFour\n";
$return .= $PARTITION; #for this application i needed a partition after each section
return $return;
@AndrewRussellHayes
AndrewRussellHayes / getCount.pl
Created November 20, 2013 16:43
Accepts: directory path Does: counts files in directory ignoring dotfiles and files named "file_list" Returns: count as an int Note: this is probably the fastest "pure perl" way to do this. It is certainly faster than reading the whole file in to an array and then processing through the array. This just processes as the directory is read.
sub getCount{
my $DH; # Directory Handle
my $WD = $_[0]; # Working Directory
my $DE; # Directory Entry (file)
my $CT; # Count
$CT=0;
opendir($DH, $WD) or die "Cannot open working directory |$WD|: $!";
while ($DE= readdir($DH))
{
@AndrewRussellHayes
AndrewRussellHayes / clearScreen.pl
Created November 20, 2013 16:40
clear screen in any major system
sub clearScreen{
system $^O eq 'MSWin32' ? 'cls' : 'clear';
}
@AndrewRussellHayes
AndrewRussellHayes / getSwitchState.pl
Last active December 28, 2015 21:39
Code to get the value of a switch (in this case the switch is a single line file with a boolean value on it)
my $switchName = "Debug Switch";
open($switchFileHandle,'<',$switchFile) || warn "cant open switch file $!";
$switch = <$switchFileHandle>;
close($switchFileHandle);
chomp($switch);
if($switch){$switch = "ON";}
else{$switch = "OFF";}
$switchString = "$switchName($switch)";
@AndrewRussellHayes
AndrewRussellHayes / updateGitSubmodules.sh
Last active December 28, 2015 19:09
update submodule to their current head status in git
# get the submodule initially
git submodule add ssh://bla submodule_dir
git submodule init
# time passes, submodule upstream is updated
# and you now want to update
# change to the submodule directory
cd submodule_dir