Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Regex capture consumed by non-capturing match

by ribasushi (Monk)
on Jul 20, 2007 at 15:51 UTC ( #627816=note: print w/ replies, xml ) Need Help??


in reply to Re: Regex capture consumed by non-capturing match
in thread Regex capture consumed by non-capturing match

I feel dumb. Thank you for pointing this out.
As far as your suggestion goes I prefer to fix the function to make a copy of @_, instead of expecting the user of the function to remember this subtle behavior.

Thanks again!


Comment on Re^2: Regex capture consumed by non-capturing match
Re^3: Regex capture consumed by non-capturing match
by ikegami (Pope) on Jul 20, 2007 at 16:16 UTC

    You could fix it in both places. I would definitely consider passing a global a bug.

    May I also suggest an alternate implementation?

    #!/usr/bin/perl sub trim_start { for (my $s = @_ ? $_[0] : $_) { s/\A\s+//m; return $_; } } sub trim_end { for (my $s = @_ ? $_[0] : $_) { s/\s+\z//m; return $_; } } { my $test = 'abc , def'; $test =~ /([\s\w]+),([\s\w]+)/; my @words = map trim_start, map trim_end, "$1", "$2"; }

    The advantage of that implementation is that its flexible as to how its called.

    • It can be used to trim a single value:

      print(trim_start(trim_end($var)), "\n");
    • It can be used to trim a list of values:

      my @trimed = map trim_start, map trim_end, @untrimmed;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://627816]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-09-17 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls