Passing "|" in split function

Hi,

Can we pass delimiter "|" as reference.
Example:

my $delim = '|';
print "delim = <$delim> \n";
my $word = "I|know|you";
my ($fld1,$fld2,$fld3) = split(/\$delim/,$word);

print "fld1 = $fld1 \n";
print "fld2 = $fld2 \n";
print "fld3 = $fld3 \n";

This above code does not work though... Is it possible some how?

Thanks in advance
Js

Yes, by using of quotemeta function it is possible

my $delim = '|';
print "delim = <$delim> \n";
# Add escape sequence non-word characters
$delim=quotemeta($delim); 
my $word = "I|know|you";
my ($fld1,$fld2,$fld3) = split(/$delim/,$word);
print "fld1 = $fld1 \n";
print "fld2 = $fld2 \n";
print "fld3 = $fld3 \n";

Perl documentation

prompt$ perldoc -f quotemeta
2 Likes

Perfect :b:

Thank you so much ... quotemeta is new to me.