Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Okay I have a string defined like this :
my $blah = 'blah' . chr ( 132 ) . 'blah';
When I try to print this onto the screen the right character shows up, but NOT when I print it to a filehandle.
Instead I get a strange blackbox character. Anyone knows why this is happening?

Replies are listed 'Best First'.
Re: special character printed on screen but not to file
by arhuman (Vicar) on Mar 22, 2001 at 15:30 UTC
    It's a vt100 code which is interpreted when you print it on the screen but not when you print itinto a file.
    OR
    It's an extended character (above 127) which is properly displayed by your terminal emulator (using the right associated character table) but not when you write it to a file.

    UPDATE :
    Whatever the case you must understand that the character which is displayed on your screen heavily depends on your screen configuration
    (especially for char with a value above 127 = non pure 7bits ASCII)
    You save/give to the screen a character code, then this character is interpreted either by your screen emulator either by your file viewer (maybe differently)
    on my linux box this chr(132) display an invisible char...
    So if your question is how to see the same char on the screen and in the file, the answer is :
    display the file with an utility using the same configuration for the screen setup.

    The best solution to prevent this kind of problem, is to only use char < 128 (truely portable) and avoiding vt-100 codes which could be interpreted differently.


    "Trying to be a SMART lamer" (thanx to Merlyn ;-)
      So what can I do about that?
Re: special character printed on screen but not to file
by dvergin (Monsignor) on Mar 22, 2001 at 23:27 UTC
    Perhaps a few points of clarification would help us help you.

    I assume from what you say that the blackbox character shows up when you print to a file and then view that file on the screen. Yes? So what are you using to view the file?

    I suspect that the right character is in the file and that it just isn't displaying as you would like with whatever tool you are using to view the file. So my next question is: Is it really important to your project that you be able to view the file on screen, or were you just viewing the file to confirm what is in it?

    Since you have successfully printed the character directly to the screen, have you tried writing a simple program to read the file and print it to the screen? That would help confirm for you whether the char has properly been written to the file.

    Bottom line: what is your goal? If it is to create a file that can be viewed on the screen, you will have to write, find, or configure a tool to do that. If it is to create a file for use in some other way, ignore the little black character and proceed to the next step in your program...

    Edit 2001-03-22 by tye to close <u>