Perl qr function usage

I'm not sure why I'm having so much trouble with this.
I think I'm really not understanding how this works.

I'm trying to store a regex in a variable for use later in a script.

Can someone tell me why this doesn't match???

#!/usr/bin/perl
#
#
#
$ticket=1212;

my $rx_ticket = qr/^\d{4}$/;

if ($ticket !~ /$rx_ticket/) {
        print "It matches\n";
}
else {
        print "No match\n";
}


print "Finished\n\n";
print "$ticket\n";

I expected it to match, but I get the following output:

$ ./ticket.pl
No match
Finished

1212

What am I not understanding here???
I'm sure it something simple.

Thanks

It's actually working correctly.
The !~ operator returns true if the pattern match fails.
The =~ operator returns true if the pattern match succeeds.

You may want to take note that the ! character is typically used to reverse the sense of comparison, to negate the comparison. For example, == is for numeric comparison and returns true if two numbers are equal, and != returns true if two numbers are unequal.

More information is in the Perl documentation.
http://perldoc.perl.org/perlop.html\#Binding-Operators

And here are a couple of test cases:

$ 
$ perl -le '$ticket = 1212;
            $rx_ticket = qr/^\d{4}$/;
            # ==================================================
            # !~ returns true if scalar does not match pattern
            # =~ returns true if scalar matches pattern
            # ==================================================
            if ($ticket =~ /$rx_ticket/){
                print "It matches\n";
            } else {
                print "No match\n";
            }
            print "Finished\n";
            print "$ticket\n";
           '
It matches

Finished

1212

$ 
$ 
$ perl -le '$ticket = "12ABC12";
            $rx_ticket = qr/^\d{4}$/;
            # ==================================================
            # !~ returns true if scalar does not match pattern
            # =~ returns true if scalar matches pattern
            # ==================================================
            if ($ticket !~ /$rx_ticket/){
                print "It does not match\n";
            } else {
                print "It matches\n";
            }
            print "Finished\n";
            print "$ticket\n";
           '
It does not match

Finished

12ABC12

$ 
$ 
1 Like