I am writing some Perl scripts where I need to do a lot of string matching. For example:
my $str1 = "this is a test string";
my $str2 = "test";
To see if $str1 contains $str2 - I found that there are 2 approaches:
Approach 1: use Index function:
if ( index($str1, $str2) != -1 ) { .... }
Approach 2: use regular expression:
if( $str1 =~ /$str2/ ) { .... }
Which is better? and when should we use each of these over the other?