my $pass; while (my $input = <$sock>) { if (index($input, "/QUOTE") != -1) { my $str = $input; ($pass) = $str =~ m{/QUOTE\sPASS\s(.*?)$}; print $sock "QUOTE PASS $pass\r\n"; } }