Ralesk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Odd workings of split
by moritz (Cardinal) on Mar 29, 2012 at 12:49 UTC | |
by Ralesk (Pilgrim) on Mar 29, 2012 at 13:03 UTC | |
|
Re: Odd workings of split
by tobyink (Canon) on Mar 29, 2012 at 13:42 UTC | |
by Ralesk (Pilgrim) on Mar 29, 2012 at 15:24 UTC | |
|
Re: Odd workings of split
by morgon (Priest) on Mar 29, 2012 at 12:50 UTC | |
|
Re: Odd workings of split
by JavaFan (Canon) on Mar 29, 2012 at 12:58 UTC | |
|
Re: Odd workings of split
by Jenda (Abbot) on Mar 30, 2012 at 09:30 UTC | |
by Ralesk (Pilgrim) on Mar 30, 2012 at 23:17 UTC | |
by Jenda (Abbot) on Mar 31, 2012 at 07:16 UTC | |
|
Re: Odd workings of split
by Anonymous Monk on Mar 29, 2012 at 13:03 UTC | |
by Ralesk (Pilgrim) on Mar 29, 2012 at 13:08 UTC | |
|
Re: Odd workings of split
by flexvault (Monsignor) on Mar 29, 2012 at 12:48 UTC | |
by JavaFan (Canon) on Mar 29, 2012 at 12:55 UTC | |
by flexvault (Monsignor) on Mar 29, 2012 at 14:03 UTC |