Extracting parts of filenames from an array
As said before, use map instead of a foreach loop and change the regex delimiter.
If you put the array in double quote when print, a space is inserted between each element of the array in fact it is the content of the special variable $&quot;, see http://perldoc.perl.org/perlvar.html

$LIST_SEPARATOR
$&quot;
When an array or an array slice is interpolated into a double-quoted string or a similar context such as /.../ , its elements are separated by this value. Default is a space. For example, this:
   print &quot;The array is: @array\n&quot;;

is equivalent to this:
   print &quot;The array is: &quot; . join($&quot;, @array) . &quot;\n&quot;;

Mnemonic: works in double-quoted context.

Your code becomes:
# Use ALLWAYS these two lines in every script.
use strict;
use warnings;

my @configs = glob(&quot;m4/config.*.m4&quot;);
my @outputs = map{ m~m4/(config\..*)\.m4$~ } @configs;
print &quot;@configs\n&quot;;
print &quot;@outputs\n&quot;;

Output:
m4/config.a2.m4 m4/config.mk.m4 m4/config.php.m4
config.a2 config.mk config.php