#!/usr/bin/perl -w use strict; my $regex = qr/foo/; my $test_string= 'bar'; if (! $test_string =~ /$regex/){ print "no match\n"; }else{ print "match\n"; } unless ( $test_string =~ /$regex/){ print "no match\n"; }else{ print "match\n"; }