Suppose I have the following code:
my ($var1, $var2, $var3, $var4, $var5) = map { $_ ||= ""; Trim($_); $_
+; } split(/\|/, $str);
sub Trim
{
$_[0] =~ /\s+$//g;
$_[0] =~ /^\s+//g;
}
As I understand it, that will set to the empty string and trim any undefined strings that are split. However, that is dependent on how many pipe symbols there are in str. So, it is very possible that $var2 through $var5 will still be undefined after this code is executed. What is a better way of writing this code so that I'm sure all the variables in the array are at least defined or set to the empty string, even if there aren't enough pipe symbols in $str?
Also, am I correct in thinking that the $_ ||= "" in the map is redundant since the parts of the string that are split will always be defined?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.