in reply to Re: perlfunc index name conflict
in thread perlfunc index name conflict

the second parameter should be a substring to match, not a regular expression

Well, /(\w+)/ will return a substring

Replies are listed 'Best First'.
Re^3: perlfunc index name conflict
by AnomalousMonk (Archbishop) on Sep 24, 2012 at 08:30 UTC

    In scalar context such as supplied by index, a successfully matching regex will return 1, which will be stringized to '1' for the substring for which to search:

    >perl -wMstrict -le "my $str = 'abc1def'; $_ = 'xyz'; ;; print 'index: ', index $str, /(\w+)/; print qq{captured: '$1'}; " index: 3 captured: 'xyz'

    An unsuccessful regex match returns the empty string in scalar context.