Something like the magic you want is in wrapLine at the end of the code. However take note of the other changes I've made to your sample code. In particular:
#!/usr/bin/perl use warnings; use strict; use Time::Local; my $kMaxLine = 70; #Will hold the command line data from the execution of module "add_use +rDefined" (my $module_cmd = "$0 @ARGV";) #But for now hard-code in an example. appendToFile( '/mrtg/mrtgwww/cfg/devices/Tests/Router-Memory_and_Storage.cfg', './add_userDefined --cfg=Router-Memory_and_Storage.cfg --host-temp +late=In-Progress/Memory-and-Storage.htp --if-template=In-Progress/Mem +ory.template --ip=10.10.10.10', ); sub appendToFile { my ($cfgFileName, $module_cmd) = @_; #Get ONLY the Month and the Year my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(); my %months = ( 0 => 'January', 1 => 'Feburary', 2 => 'March', 3 => 'April', 4 => 'May', 5 => 'June', 6 => 'July', 7 => 'August', 8 => 'September', 9 => 'October', 10 => 'November', 11 => 'December' ); $year = 1900 + $year; my $month_year = "$months{ $mon } $mday, $year"; my $cfgmaker_cmd; my $wholeFile; #open my $dataIn, '<', $cfgFileName or die "Error opening $cfgFile +Name: $!\n"; while (my $line = <DATA>) { next if $line =~ /^# Created by/i; if ($line =~ /\/jwpmrtg\/bin\/cfgmaker/i) { chomp($line); $cfgmaker_cmd .= $line; } else { $wholeFile .= $line; } } #close $dataIn; $_ = wrapLine($_) for $module_cmd, $cfgmaker_cmd; my $sep = '#' x $kMaxLine; my $comment .= <<EOF; $sep ## Date: $month_year ## Module CMD: $module_cmd ## cfgmaker CMD: $cfgmaker_cmd EOF $comment .= "$sep\n"; print $comment; # print $wholeFile; } sub wrapLine { my ($line) = @_; $line = '' if ! defined $line; return join "\n## ", $line =~ m~(.{1,$kMaxLine})(?:\s|(?<![-. +\w=/]))~g; } __DATA__
Prints:
###################################################################### ## Date: Feburary 8, 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 ## cfgmaker CMD: ######################################################################
In reply to Re: Printing Fixed Width Strings and Spliting String into Multiple Lines
by GrandFather
in thread Printing Fixed Width Strings and Spliting String into Multiple Lines
by mmartin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |