What will be the size of the @data array ? my $var = ':x:y:z:'; my @data = split(':', $var, -1);
-
0
-
3
-
1
-
5
In Perl's split function, when the LIMIT is negative, trailing empty fields are NOT stripped. The string ':x:y:z:' split by ':' produces fields: '', 'x', 'y', 'z', '' (5 elements). With LIMIT=-1, all fields including trailing empty ones are preserved. If LIMIT were omitted or positive, trailing empty strings would be removed, leaving only 4 fields.
5 is correct. Perl's split(PATTERN, STRING, LIMIT) with a LIMIT of -1 preserves all trailing empty fields (a positive limit would cap the field count, and omitting limit/using 0 strips trailing empty strings, but -1, like any negative number, keeps everything). Splitting ':x:y:z:' on ':' produces the fields: '' (before the first colon), 'x', 'y', 'z', and '' (after the trailing colon) — five elements total. Without the -1 limit, Perl would have dropped that final trailing empty string, giving only 4 elements, which is the distinction this question is testing.