in reply to Re^2: Printing Fixed Width Strings and Spliting String into Multiple Lines
in thread Printing Fixed Width Strings and Spliting String into Multiple Lines

Don't apologise for asking questions to find out about stuff you don't know! After all, that is exactly why you are visiting.

$_ is Perl's default variable. It is used as an alias to the current item for things like grep and map. It is also used in the same way when you use for as a statement modifier such as in $_ = wrapLine($_) for $module_cmd, $cfgmaker_cmd;. I used that trick by the way to save writing out:

$_ = wrapLine($module_cmd); $_ = wrapLine($cfgmaker_cmd);

and of course to expose you to some interesting Perl.

$line =~ m~(.{1,$kMaxLine})(?:\s|(?<![-.\w=/]))~g is a regular expression match. m is the match operator and uses the following non-whitespace character as the quote character for the regular expression string. In this case it's using ~ which I chose to avoid having to quote the / used in the match string. The g on the end is the "global match" flag - match as many times as you can. There is a lot more in that regular expression and I strongly encourage you to read perlretut and perlre to at least get an overview of whet regexes do and look like. You can simplify the regex line to:

return join "\n## ", $line =~ m~(.{1,$kMaxLine})~g;

to have it break the line into fixed size pieces without regard to white space or punctuation. You would find it instructive however to read the documentation for regexen engough to be able to figure out how the original regex works. ;)

True laziness is hard work

Replies are listed 'Best First'.
Re^4: Printing Fixed Width Strings and Spliting String into Multiple Lines
by mmartin (Monk) on Feb 09, 2012 at 20:01 UTC
    Hey GrandFather,

    Once again thanks for the explanation!

    Thanks for dumbing it down a bit for me lol... Yea I will definitely check out those docs for the regex stuff. I've gotten somewhat decent with regular expressions lately, but like most things "code" unless you wrote it yourself or are that good with the language in question it can be pretty hard to comprehend. But I think I got it now!

    But thanks for your time, I really appreciate it.


    Thanks Again,
    Matt