in reply to Re: concatenate a non-printable character onto an ascii string and inspect the content
in thread concatenate a non-printable character onto an ascii string and inspect the content
I upvoted your post for its pack-related links and H example.
However, there was one feature of the example that made my buggy-sense tingle: assignment to $, to achieve desired output formatting. (I realize you may have done this merely to support a quick-and-dirty example, but I think it's a best-practice to always exemplify Best Practices in example code.)
Of course, $, is a Perl special variable (see perlvar). These variables are all package-globals, and as such should IMHO always be carefully local-ized within the narrowest possible scope when they are changed. E.g.:
Even so, localized scoping is dynamic, not lexical, so such a change propagates to all subsidiary scopes, such as that in an invoked subroutine. This can lead to some very perplexing bugs.c:\@Work\Perl\monks>perl -le "use strict; use warnings; use charnames qw(:full); ;; my $string = qq{123456\N{FS}}; { local $, = q( ); print unpack '(H2)*', $string; } " 31 32 33 34 35 36 1c
I think an even better practice is to format an output string (or do most other things) in a way that avoids scoping issues altogether:
join does just what you want and has absolutely no effect outside of the statement in which it appears. Control of scope gives you great power, but with great power comes great capability to blow your entire leg off!c:\@Work\Perl\monks>perl -le "use strict; use warnings; use charnames qw(:full); ;; my $string = qq{123456\N{FS}}; print join ' ', unpack '(H2)*', $string; " 31 32 33 34 35 36 1c
Give a man a fish: <%-{-{-{-<
|
|---|