Last active
October 6, 2015 20:38
-
-
Save codewizard13/caef4316cc834b7aa6b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use warnings; | |
use Cwd; | |
print "Hello World!\n\n"; | |
######################################################### | |
## CREATED: 2015.10.05 | |
## AUTHOR: Eric Hepperle | |
## | |
## Get all files in dir and concat them together | |
## with a horizontal rule including the filename, | |
## followed by the file contents and 3 blank lines. | |
## | |
## NOTE: This version concats the files but needs work | |
## on how to display filename and bars. | |
## Currently working on how to center the | |
## filename in the bar, but Perl modulus (%) | |
## operator behavior needs examination. | |
## | |
## 10/06/15 - Works. Adds namebar and contents to | |
## screen. Next is to get it to write | |
## out to a file. | |
## | |
######################################################### | |
my @dirContents = `ls | grep testfile`; | |
print "\$dirContents:\n\n"; | |
# $bar = '-'x45,"\n"; | |
joinFileContents(@dirContents); | |
################################### | |
# SUBROUTINES: | |
################################### | |
sub joinFileContents { | |
my (@dirContents) = @_; | |
my $rootPath = `pwd`; | |
print "\$rootPath = $rootPath\n"; | |
foreach my $filename (@dirContents) { | |
# Remove newline from $filename. | |
chomp($filename); | |
# print "FILENAME TO OPEN: $filename\n"; | |
open my $FH, '<', $filename or die "Can't open file $!"; | |
# print "Successfully opened:\t$filename\n"; | |
# verify contents: | |
my $fileContents = do { local $/; <$FH> }; | |
# print "\n\n$bar\n\n"; | |
# print "File Contents:\n\n$fileContents\n"; | |
# print bar and contents | |
print nameBar($filename) . "\n\n" . $fileContents . "\n\n"; | |
} | |
=begin comment | |
print nameBar("Eric's Name Bar", 69); | |
print nameBar("Apples", 48); | |
print nameBar("Incredible Hulk"); | |
=cut | |
} | |
sub nameBar { | |
my ($name, $barlen) = @_; | |
# default bar length. | |
my $barLen = $barlen ? $barlen : 60; | |
my $nameLen = length($name); | |
my $topBar = '#'x$barLen; | |
my $bottomBar = '#'x$barLen; | |
=begin comment | |
print "\$name = $name\n\n"; | |
print "\$topBar = $topBar\n"; | |
print "\$barLen = $barLen\n"; | |
print "\$nameLen = $nameLen\n"; | |
print "\$bottomBar = $bottomBar\n"; | |
=cut | |
my $nameStartPoint = int($barLen / 2) - int($nameLen / 2); | |
my $barCharsFront = $nameStartPoint - 1; | |
my $barCharsRear = $barLen - ($barCharsFront + $nameLen); | |
my $midBar = '##' . ' 'x ($barCharsFront - 2) . $name . ' 'x ($barCharsRear - 2) . '##'; | |
=begin comment | |
print "NAME WILL START AT CHARACTER [$nameStartPoint] of the bar\n\n"; | |
print "\$barCharsFront = $barCharsFront\n\n"; | |
print "\$midBar =\n$midBar\n" . length($midBar) . "\n\n"; | |
=cut | |
# my $fullBar = "\t$topBar\n\t$midBar\n\t$bottomBar\n\n"; | |
my $fullBar = "\t$topBar\n\t$midBar\n\t$bottomBar\n\n"; | |
return $fullBar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment