Do you mean you want to print out a file's contents, but substituting ascii code values instead of the characters? Umm, that's a one-liner:
perl -e 'undef $/; print join(",", map {ord} split //, <>)'And the reverse is a one-liner also. Use
chr instead of
ord and switch where the comma and null character are:
perl -e 'undef $/; print join("", map {chr} split /,/, <>)'
Golf aside, deconstructing this will probably be instructive. Concepts that I've exploited here include:
- The $/ variable determines what constitutes a "line" read by the <> operator. (See perlman:perlvar).
- A null first argument to split breaks up the second argument into individual characters.1 (See perlman:perlfunc.)
- The ord and chr functions essentially complement each other.
- map is a powerful way to construct loops in Perl, in a style reminiscent of functional programming languages. (See perlman:perlfunc.)
- To construct a string out of an array's elements, using a given string as separator, use join. In this sense, it's complementary to split. (See perlman:perlfunc also.)
- The -e option to Perl allows you to execute a script directly from the command line. (See perlman:perlrun.)
HTH
1That gets tricky when dealing with multi-byte encodings, but ignore that for the moment....