Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Appending a single Scalar after a match

by SavannahLion (Pilgrim)
on May 25, 2015 at 08:36 UTC ( [id://1127660] : perlquestion . print w/replies, xml ) Need Help??

SavannahLion has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Appending a single Scalar after a match
by Athanasius (Archbishop) on May 25, 2015 at 09:07 UTC

    Hello SavannahLion,

    You were so close! In list context, the matches are returned as a list, not an array, and you can subscript a list, so just remove the initial $ (and no need to put $k into parentheses):

    $k .= ($t =~ /^\s*(.*?)\s*$/g)[0];

    :-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Oh... ha! ha! No way! :D
      I kept fiddling with it by adding $ or @ thinking it needed to pulled apart as an array.
      I knew the answer had to be in there somewhere. So many thanks!

Re: Appending a single Scalar after a match
by BrowserUk (Patriarch) on May 25, 2015 at 09:05 UTC

    Um. This? $t =~ /^\s*(.*?)\s*$/g and $k .= $1

    Though it puzzles me why you are using /g if you only want the first match?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Hhhmmm... good point. I must have had a good reason when I originally wrote it and didn't remove it the last time I updated the script. So updated.