Skip to content

Instantly share code, notes, and snippets.

Andrew Hayes AndrewRussellHayes

View GitHub Profile
@AndrewRussellHayes
AndrewRussellHayes / matchEnd.pl
Created Nov 20, 2013
perl string matching with tail anchor
View matchEnd.pl
#!/usr/bin/perl
use strict;
use warnings;
# if the string ends in z regardless of case the below returns true
my $string = '"abcdefz"';
my $string2 = "abcefj";
my $last;
View searchString.pl
$sentence = "the boy is 20";
if($sentence =~ /the/)
{
print "true";
}
if($sentence =~ /Trhe/)
{
print "true"; #will return false
@AndrewRussellHayes
AndrewRussellHayes / PerlTemplate.pl
Last active Dec 28, 2015
General Perl template.
View PerlTemplate.pl
#!/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 Dec 28, 2015
Genera Perl module template.
View ModuleTemplate.pl
#!/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 Nov 20, 2013
Add Dashes to date in file name. Takes: "part_part_20120513_part_part.ext" and makes: "part_part_2012-05-13_part_part.ext".
View addDashes.pl
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 Dec 28, 2015
reads and lists a directory forwards and backwards by date
View dirByDate.pl
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 Nov 20, 2013
flips single file switch, used for development purposes typically
View flipSwitch.pl
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 Dec 28, 2015
prints basic unix menu and handles input
View basicUnixMenu.pl
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 Dec 28, 2015
this code will keep running until enter is pressed and then continue running.
View continueUntilEnterKey.pl
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 Nov 20, 2013
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.
View dynamicStringSeparator.pl
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;
You can’t perform that action at this time.