in reply to return a word next to the word you give

You could use the built-in capture variable $1 to extract the word next to the one you are after -

use strict; my @file = <STDIN>; my $word = "color:"; # no space after colon for my $lineno ( 0 .. $#file ) { if ($file[$lineno] =~ /\b$word\s*(\w+)/){ # notice the capture bra +ckets my $value = $1; # $value here is 'red'. ... } }
Another possible variant, say, in case there are multiple color attributes on the line, you want to capture all of them in an array.

my @colors = $file[$lineno] =~ /\b$word\s*(\w+)/g;