I'm going to answer the question that (I think) underlies your question - not the question that you asked.
It looks to me like you are parsing command-line options. So use a command-line option parser, rather than reinventing that for yourself. Getopt::Long is part of the standard Perl distribution.
#!/usr/bin/perl
use strict;
use warnings;
# We use modern Perl (here, specifically, say())
use 5.010;
use Getopt::Long 'GetOptionsFromString';
use Data::Dumper;
my %options;
my $str = '-test aa_aa -machine aab-baa-aba -from ccc';
GetOptionsFromString($str, \%options, 'test=s', 'machine=s', 'from=s');
say Dumper \%options;
Normally, you'd use the function GetOptions() as you're parsing the command-line options that are available in @ARGV. I'm not sure how the options ended up in your string, but there's a useful GetOptionsFromString() function for this situation.
Update: To explain why your code didn't work.
$str = "-test aa_aa -machine aab-baa-aba -from ccc";
$str =~ /-test\s*(.*)\s*/;
You're capturing what matches (.*). But .* is greedy. That is, it matches as much data as it can. And, in this case, that means it matches until the end of the line. There are (at least!) a couple of ways to fix this.
1/ Make the match non-greedy by adding ?.
$str =~ /-test\s*(.*?)\s*/;
2/ Be more explicit about what you're looking for - in this case non-whitespace characters.
$str =~ /-test\s*(\S*)\s*/;
perl -e 'use strict; use warnings; my $str = "-test aaaa -machine bbb -from ccc"; while ($str =~ m/ (\w+)/g) { print $1."\n"; }'