substr outside of string at ...
I don't encounter this warning often, but when I do it indicates a bug in my code.
I saw this at two my clients recently, one we turned use warinings on. Frankly I don't understand why even in 2018 some companies insist on not using warnings.
This is the script:
examples/hello_perl_world.pl
use 5.010; use strict; use warnings; my $str = "Hello Perl World"; my $part = substr $str, 6, 4; say qq('$part'); my $other = substr $str, 11, 10; say qq('$other'); my $more = substr $str, 20, 10;
It does not do anything special. It just runs substr on a string. In the first 2 cases the 2nd parameter, that indicates the beginning of the substring is inside the original string. This does not generate a warning even if the full substring is expected to be longer than what we have the string.
In the 3rd case however, the 2nd parameter is bigger than the full length of the string, which means that already the beginning of the substring is outside the original string.
That generates a warning.
$ perl hello_perl_world.pl 'Perl' 'World' substr outside of string at hello_perl_world.pl line 13.
See all the other common warnings and errors in Perl.
Published on 2019-03-25