http://www.perlmonks.org?node_id=952367

mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I was hoping someone could lend me a hand.
I have a Perl script that works alongside MRTG building cfg files. This isn't really relevant to know what I want to do but I thought it couldn't hurt. MRTG has a Perl script called "cfgmaker" which as the name suggests builds cfg files. At the top of these config files it automatically generates a very small comment block.

What I would like to do is open that file read in it's contents, then print back out to the file with a modified comment block at the top. I have it pretty much where I want it except for making some of the strings a fixed width so that it doesn't span all the way to the end of the string on the same line. Because for example, one of the strings (which is the actual command executed from the command line) could be anywhere from 250-300+ characters.

This is what I have so far. (The variable $module_cmd will be gotten dynamically later when I add this to the full/production script, using ($0 and @ARGV):
Also it will only print out to the screen right now. After I get it working I will print it back to the file.
#!/usr/bin/perl use warnings; use strict; use Time::Local; #Will hold the command line data from the execution of module "add_us +erDefined" (my $module_cmd = "$0 @ARGV";) #But for now hard-code in an example. my $module_cmd = "./add_userDefined --cfg=Router-Memory_and_Storage.cf +g --host-template=In-Progress/Memory-and-Storage.htp --if-template=In +-Progress/Memory.template --ip=10.10.10.10"; #Will hold the command line data executed from cfgmaker (will be take +n from comment block of <DATA> file below my $cfgmaker_cmd; my $comment; my $wholeFile; &appendToFile(); sub appendToFile() { #Get ONLY the Month and the Year my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(); my %months = (0=>'January', 1=>'Feburary', 2=>'March', 3=>'April', + 4=>'May', 5=>'June', 6=>'July', 7=>'August', 8=>'September', 9=>'Oct +ober', 10=>'November', 11=>'December'); $year = 1900+$year; my $month_year = "$months{ $mon } $mday, $year"; open (DATA, "< /mrtg/mrtgwww/cfg/devices/Tests/Router-Memory_and_S +torage.cfg") or die "There was an error while trying to open the file +. $!\n"; while (my $line = <DATA>) { if ($line =~ /^# Created by/i) { #Skip this line... next; } elsif ($line =~ /\/jwpmrtg\/bin\/cfgmaker/i) { chomp($line); $cfgmaker_cmd .= $line; } else { $wholeFile .= $line; } } close DATA; $comment .= <<EOF; ###################################################################### +######################################### ## Date: $month_year ## Module CMD: $module_cmd ## cfgmaker CMD: $cfgmaker_cmd EOF $comment .= "##################################################### +##########################################################\n"; print $comment; # print $wholeFile; }
All I really am trying to figure out is how to make the strings $module_cmd and $cfgmaker_cmd be split into multiple lines depending on it's length.
I know this can be done using printf/sprintf. But not really sure where to begin with it.

Here is the output from the code above (printing only $comment)

###############################################################################################################
## Date: Feburary 7, 2012
## Module CMD: ./add_userDefined --cfg=Router-Memory_and_Storage.cfg --host-template=In-Progress/Memory-and-Storage.htp --if-template=In-Progress/Memory.template --ip=10.10.10.10
## cfgmaker CMD: /jwpmrtg/bin/cfgmaker --nointerfaces --host-template=/jwpmrtg/mrtgwww/cfg/templates/In-Progress/NetTools_Memory-and-Storage.htp --global "routers.cgi*Icon: router2-sm.gif" --output /jwpmrtg/mrtgwww/cfg/build_userDefined/NetTools-Percentage.cfg jwpadmin!@192.168.5.6
###############################################################################################################


What I would like it to look like is to have it print with each line ending with the same length as the "#####...####" border lines, with closing "#" at the end of each line):

I tried printing an example of what I want it to look like but can't seem to get it to look right on here.


I'm pretty sure I would be using something like... printf "%-100s #", $module_cmd;

Any help would be much appreciated.


Thanks,
Matt