I'm brand new to Perl, but based on the documentation that I have read, it looks like the split function in Perl asks for a regex pattern rather than a string delimiter as the first parameter, but I found that using something like print +(split(' ', $string))[0] will still split the string correctly.
Based on that, I was trying to use a variable delimiter (ex. print +(split($var, $string))[0] where $var = ' ') and found that it did not work. What am I doing wrong?
Thanks!
EDIT: Sorry for the terrible question. I was running this against a string with leading spaces and found that the split function didn't like the leading spaces. For example:
my $var = ' ';
print +(split($var, ' abc ddddd'))[0]
gives a blank output. Is $var being interpreted as /$var/ inside the split function?
versus
print +(split(' ', ' abc ddddd'))[0]
which gives an output of abc
So when I read the docs I was assuming my variable would be considered a literal string, when in reality it was not, and therefore the leading whitespace was not stripped.
perldoc. If it's installed on your system, you can runperldoc -f <function>, or in this case,perldoc -f split(I linked to the online version for convenience). The documentation is excellent. Your uncertainty aboutsplitusing regex vs. string is explained in detail, including the special case ofsplit ' '.split ' 'will use a literal space as delimiter, invoking the special case described in the perldoc.$var = ' '; split $varwill be equivalent ofsplit / /, which is a regex split, not the same thing.