I was following along with this tutorial on how to split strings when I came across a quote that confused me.
Words about Context
Put to its normal use, split is used in list context. It may also be used in scalar context, though its use in scalar context is deprecated. In scalar context, split returns the number of fields found, and splits into the @_ array. It's easy to see why that might not be desirable, and thus, why using split in scalar context is frowned upon.
I have the following script that I've been working with:
#!/usr/bin/perl
use strict;
use warnings;
use v5.24;
doWork();
sub doWork {
my $str = "This,is,data";
my @splitData = split(/,/, $str);
say $splitData[1];
return;
}
I don't fully understand how you would use split on a list.
From my understanding, using the split function on my $str variable is frowned upon? How then would I go about splitting a string with the comma as the delimiter?
@_behavior was removed in 5.12 and deprecated in 5.8.8 - see stackoverflow.com/a/44153498/1331451split()a scalar to work on. Whether the call is in scalar or list context is determined by whether the results are stored in an array (or list) or in a scalar.