2 ms·
> Inserting a NUL byte between each word, and at the end doesn't sound like it requires Perl... No, it's more clever than that. The shell will actually execute
by SrslyJosh 12y ago
> Inserting a NUL byte between each word, and at the end doesn't sound like it requires Perl...
No, it's more clever than that. The shell will actually execute something like this:
/usr/bin/perl -e 'print join(chr(0), @ARGV), chr(0)' -- your input string goes here
So, what happens is that the shell (in this case, bash) performs argument expansion on your input string, then calls perl -e '...' with the expanded words as its arguments. What perl does is join all of those arguments with a NUL byte and spit them back out for the calling process to read, which makes figuring out where each expanded "word" begins and ends really simple.
Could you do this in shell? Probably. Would a correct implementation be as short and easy to understand? Probably not. Therefore, perl.