if ($option =~ m/^\[(\d)\]$/) { print "Activated!"; $str=$1; }
or
if (my ($str) = $option =~ m/^\[(\d)\]$/) { print "Activated!" }
or
if (my ($str) = $option =~ /(\d)/) { print "Activated!" }
.. and many others. You forgot to fix your match with () .
EDIT:
if ($option =~ /(?<=^\[)\d(?=\]$)/p && (my $str = ${^MATCH})) { print "Activated!" }
or
my $str; if ($option =~ /^\[(\d)(?{$str = $^N})\]$/) { print "Activated!" }
or
if ($option =~ /^\[(\d)\]$/ && ($str = $+)) { print "Activated!" }
For $ {^ MATCH}, $ ^ N and $ +, perlvar .
I like these questions :)
source share