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: ######################################################################
True laziness is hard work

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.