So I found myself updating an old script (parses oodles of HTML files). and unknowingly introduced a subtle bug.
Here is a condensed version of the original code. The match is irrelevant.
my $k = ''; # A bunch of junk happens to this scalar before this point
my $t = "\ttest \n \n";
($k) = $t =~ /^\s*(.*?)\s*$/g;
print $k ."\n";
Without thinking, I made a subtle change.
my $k = ''; # A bunch of junk happens to this scalar before this point
my $t = "\ttest \n \n";
($k) .= $t =~ /^\s*(.*?)\s*$/g;
print $k ."\n";
A '1' kept getting shoved into $k. Goes without saying that I spend hours chasing this new bug down until I realized I was trying to use a list in a scalar context. So after much cursing I went and fixed the bug like thus.
my $k = ''; # A bunch of junk happens to this scalar before this point
my $t = "\ttest \n \n";
$t =~ /^\s*(.*?)\s*$/g;
($k) .= $1;
print $k ."\n";
After a fashion, I got to thinking. Is there a way to get it back to a one liner again and still append the value of $1 to $k? I tried several variations on the original but can't seem to work out a solution. My thought was something along the lines of:
my $k = ''; # A bunch of junk happens to this scalar before this point
my $t = "\ttest \n \n";
($k) .= $($t =~ /^\s*(.*?)\s*$/g)[0];
print $k ."\n";
I tried several variations but I can't quite seem to get it right. My gut says it should be feasible, but my brain can't quite put it together
Any ideas?
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.