^A is one character, in emacs I type ctrl-q and then ctrl-a to get ^A character.
basically i have a file which contains this type of characters. when i read the file and then using print() to print them out in perl, those characters are not visible. but if i run the command : "more <filename>" or "less" in FreeBSD 4.3, those characters are displayed. just wondering why print() function cannot display them.
thanks! | [reply] |
"^A" is a *representation* of a *non-printable* character used by more and other programs. When you print the ^A character in Perl, it prints that character as you requested, not the two characters "^" and "A" like more does. If you want to print "^" and "A" like more does, feel free to format the output as you desire.
{
my %map = map { $_ => chr(0x40 + $_) } 0x00..0x1F;
my ($re) = map qr/[$_]/,
join '',
map quotemeta,
keys %map;
sub format_non_printables {
my ($text) = @_;
$text =~ s/($re)/$map{$1}/g;
return $text;
}
}
You could tie STDOUT to make this transparent.
| [reply] [d/l] [select] |
I'd say, Because Perl's print prints that character and more and less do a conversion before/during printing; so the user can "see" (recognize) the special characters...
update:
You could do a conversion of that characters to a visible character/string before you let Perl print it...
Try to redirect the perl's output into a file and check that file with less or more... You should see your ^A again...
update2:
My less on my linux box knows an option -r which disables the behaviour, that \x01 is printed as ^A...
...
-r or --raw-control-chars
Causes "raw" control characters to be displayed. The default is
to display control characters using the caret notation; for example,
a control-A (octal 001) is displayed as "^A". Warning: when the -r
option is used, less cannot keep track of the actual appearance of
the screen (since this depends on how the screen responds to each
type of control character). Thus, various display problems may result,
such as long lines being split in the wrong place.
... | [reply] |
just wondering why print() function cannot display them.
I personally believe that you're confused: as others duly explained to you, print indeed does print() them. The fact that they're control charachters makes them not visible on a plain terminal, though. The fact that some specialized tools like more or less will display them in such a way that makes them visible is totally irrelevant. Thus, to literally answer your question: "because print() is made to print stuff as opposed to especially display it." Of course, nobody prevents you from rolling your own display() sub to do so. E.g.:
sub display {
map {
(my $s = $) =~ s/[[:cntrl:]]/^A/g;
$s;
} @_;
}
Here, I just made it show any control carachter like "^A" which may be or not what you want, but I don't know of any standard mapping or representation for them. If you have one, then again feel free to just refine the thing. All that perldoc perlre has to say is: 'all characters with ord() less than 32 are usually classified as control characters (assuming ASCII, the ISO Latin character sets, and Unicode), as is the character with the ord() value of 127 ("DEL")'
To be completely fair, Perl IO routines do some translations, and if you're really motivated to have print() do it for you, then you may move your display() code to a suitable layer once you're fine with it.
| [reply] [d/l] [select] |