Dear Monks,
I’ve run into a strange little issue. Communicating with another piece of software (actually, Linphone), the command protocol is line based. To make sure there are no newlines in any of the commands, I decided to split and take the first (0th) element and just go with it, discarding the rest — instead of using a regex to do it.
Later on, I figured I’d allow more arguments to the method, joining them with spaces, so I can just pass a list when calling it (and so I don’t have to concatenate variables into the command string all the time).
Long story short, here’s some code:
sub cmd($@) { my $self = shift; my @cmd = @_; ## Make sure none of them contain newlines for (0 .. $#cmd) { $cmd[$_] = [split "\n", $cmd[$_]]->[0]; } ## Join words with spaces and send them to linphonec print {$self->{Writer}} join(' ', @cmd) . "\n"; }
Calling it with things like $linphone->cmd("command"), $linphone->cmd("legacy command string I forgot to update\n") or $linphone->cmd("command", $argument1, $argument2) appears to work fine, as split shall return whatever is before the newline and all arguments are happy.
Calling it with $linphone->cmd('') however returns undef, which I feel is a very nasty thing for split to do.
Not sure if I actually have questions, this is probably a known caveat of split, but of course if there’s a way to coerce it to do the right thing, I’m all ears. Until then, I'll just // '' things or something like that.
In reply to Odd workings of split by Ralesk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |