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

how to output special characters?

by perlfan99 (Sexton)
on Jul 11, 2008 at 19:24 UTC ( #697062=perlquestion: print w/replies, xml ) Need Help??
perlfan99 has asked for the wisdom of the Perl Monks concerning the following question:

I want to print out special character like ^A, ^B, but "print" function doesn't display those characters. is there a way to print out those special characters to the standard out?

Replies are listed 'Best First'.
Re: how to output special characters?
by stephen (Priest) on Jul 11, 2008 at 20:58 UTC
    If you want to print a control character, you need to escape it with "\c". So:
    print "Here is ^A: \cA Here is ^B: \cB";
    Of course, as stated above, you won't be able to see those control characters since they're invisible.


Re: how to output special characters?
by pc88mxer (Vicar) on Jul 11, 2008 at 21:26 UTC
    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";
      ^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...


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


        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.
Re: how to output special characters?
by igelkott (Priest) on Jul 11, 2008 at 20:49 UTC

    In a Unix environment, try od -c to see the escape codes.

Re: how to output special characters?
by linuxer (Curate) on Jul 11, 2008 at 19:35 UTC

    Can you explain more detailled what you mean? I don't get it.

    Generally I think it's no Perl problem, but a problem of your Terminal to display such special characters. Because they have special meanings, they are treated as "not printable" and not shown...

Re: output special characters
by jethro (Monsignor) on Jul 11, 2008 at 22:23 UTC
    perl -e ' print "\x41\n";' A
    print does work. ^A should be \x01

    If you need to handle a lot of binary data you could take a look at unpack()

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://697062]
Approved by Joost
[virtualsue]: 2 people - woot
[virtualsue]: an exclusive club
marto waves
choroba sees 5
Discipulus more.. and count cloaked monks..
shmem joins the club
[Discipulus]: welcome back to Tkperlmonks virtualsue! ;=)
[virtualsue]: I missed my 17 year anniversary

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-04-24 11:00 GMT
Find Nodes?
    Voting Booth?