in reply to Surprising capture of undef with zero repetitions

Update: I should have thought a bit more before pressing "submit".. "list" at the beginning of the line is part of the problem statement. I would be thinking of removing that, then doing the match global on what is left as shown below.  /^list\s+(\w+)/g won't work, only gets the foo, not also the bar. I think that there is some way to get match global to back up and keep capturing (\w+)'s, in a single regex, but at the moment, I don't know how to do that. Sorry.

Update 2: now another solution occurred to me, that appears to work:
(updated code to add: f("foo,bar,bar2,bar3"); - no regex change, just another data example)

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; sub f { my $in = shift; my (@tmp) = "list $in" =~ /(?:^list\s+)?(\w+)/g; print(Dumper(\@tmp)); } f("foo"); f("foo,bar"); f("foo,bar,bar2,bar3"); __END__ $VAR1 = [ 'foo' ]; $VAR1 = [ 'foo', 'bar' ]; $VAR1 = [ 'foo', 'bar', 'bar2', 'bar3' ];
One way is to use match global:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; sub f { my $in = shift; my (@tmp) = $in =~ /(\w+)/g; print(Dumper(\@tmp)); } f("foo"); f("foo,bar"); __END__ $VAR1 = [ 'foo' ]; $VAR1 = [ 'foo', 'bar' ];