Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: how to output special characters?

by pc88mxer (Vicar)
on Jul 11, 2008 at 21:26 UTC ( [id://697097]=note: print w/replies, xml ) Need Help??


in reply to how to output special characters?

I will assume you mean Control-A and Control-B. Those are ASCII characters 1 through 26, so any of the following will work, for instance, to print Control-A:
print chr(1); print "\x1"; print "\001";

Replies are listed 'Best First'.
Re^2: how to output special characters?
by perlfan99 (Sexton) on Jul 11, 2008 at 21:59 UTC
    ^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!

      "^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.

      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.
      ...
      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.

      --
      If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://697097]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found