Skip to content

Instantly share code, notes, and snippets.

@AndrewRussellHayes
AndrewRussellHayes / searchString.pl
Created November 20, 2013 20:29
very basic string match
$sentence = "the boy is 20";
if($sentence =~ /the/)
{
print "true";
}
if($sentence =~ /Trhe/)
{
print "true"; #will return false
@AndrewRussellHayes
AndrewRussellHayes / PerlTemplate.pl
Last active December 28, 2015 22:19
General Perl template.
#!/usr/bin/perl -w
use strict;
use warnings;
#To view This Program's Documentation execute `perldoc programname.pl`
=head2 utilityName.pl
AUTHOR: Joe Developer
CREATION DATE:
@AndrewRussellHayes
AndrewRussellHayes / ModuleTemplate.pl
Last active December 28, 2015 22:19
Genera Perl module template.
#!/usr/bin/perl -w
package moduleName;
use strict;
use warnings;
#To view This Module's Documentation execute `perldoc modulename.pl`
=head2 module.pm
AUTHOR: Joe Developer
@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))
{