Differences between version 19 and previous revision of PerlOneLiners.
Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 19 | Last edited on Friday, April 9, 2004 11:37:38 am | by AristotlePagaltzis | Revert |
Older page: | version 18 | Last edited on Friday, April 9, 2004 11:33:23 am | by AristotlePagaltzis | Revert |
@@ -46,11 +46,11 @@
This time the loop is explicit. Again, there are two parts to the program -- selecting files and doing [IO] on them.
To read the directory, a convenience function is pulled in from the File::Slurp module, loaded using the __-M__ switch. The module has been part of the core distribution since Perl 5.8.0. Reading a directory manually is straightforward but the code would be longer and clumsier.
-Selecting the files is pretty simple, if a little obtuse: it's done by reading the contents of __/proc__, then using __grep !/D/__ to reject any entries that contain non-digit characters from the list. The results,
are then sorted numerically, done explicitly using __<=>__ because the default sort is [ASCII]betical. Each entry is then concatenated
into a full path and stuck into __@ARGV__ one by one, from where the __<>__ "diamond operator" will pick it up, auto-open it and read it for us, even autoreporting any errors in a nicely verbose format.
+Selecting the files is pretty simple, if a little obtuse: it's done by reading the contents of __/proc__, then using __grep !/D/__ to reject any entries that contain non-digit characters from the list. The results are then sorted numerically, which needs to be
done explicitly using the
__<=>__ operator
because the default sort is [ASCII]betical. Each entry is then interpolated
into a full path and stuck into __@ARGV__ one by one, from where the __<>__ "diamond operator" will pick it up, auto-open it and read it for us, even autoreporting any errors in a nicely verbose format.
-Producing human-readable output is a little more involved, using some
switches to abbreviate a bit of magic. The __-__ switch sets the __$/__ variable: here, because it
is not followed by a digit, it sets it
to a null character. This means that null characters will be regarded as line separators on input. The __-l__ switch has two effects, of which only one is relevant to us: it automatically __chomp()__s
lines read using the diamond operator. (The other is to set the __$\__ variable, which we aren't interested in or affected by.)
+Producing human-readable output is a little more involved, using switches to abbreviate a bit of magic. The __-__ switch sets the __$/__ variable: here, because the switch
is not followed by a digit, it sets the variable
to a null character. This means that null characters will be regarded as line separators on input. The __-l__ switch has two effects, of which only one is relevant to us: it automatically removes line terminators from
lines read using the diamond operator. (The other is to set the __$\__ variable, which we aren't interested in or affected by.)
;: Note that the __-0__ and __-l__ switches are order sensitive both in syntax and semantics. We order them for syntax here, because they can both accept an octal number as input, but we don't to pass one to either of them (particularly, __-0__ will be mistaken for a digit parameter to __-l__ if we turn them around).
Together, these switches effectively mean that we get null terminated lines from files, with the nulls removed on input. So we get the command line arguments listed in a __/proc/*/cmdline__ file as nice list of separate strings. And because __join()__ expects a list, __<>__ returns all "lines" (ie command line arguments) at once, which __join()__ then dutifully puts together with spaces between.