Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

My Script is saving a New Line Character as an Unreadable character.

by kramdeav (Initiate)
on Feb 21, 2014 at 22:46 UTC ( #1075789=perlquestion: print w/replies, xml ) Need Help??
kramdeav has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:
$printline="O*N%1d5TG%5d ,CLS=%.8s,XXX,BAT=, + \n"; $dest_mailbox=shift(@ARGV); $file=shift(@ARGV); $message_class=shift(@ARGV); # Set up a temp file as a holding file for valid lines $rand = int(rand(10000)); $tempfile="$file" . "$rand"; print "!Creating temp file: $tempfile!\n"; while (-f $tempfile) { $rand = int(rand(10000)); $tempfile="$file" . "$rand"; } # # Open the input file and temporary file # open (FILE, "$file") or &exit_with_error("!Error opening file for read +: $file!\n"); open (TEMP, ">>$tempfile") or &exit_with_error("!Error opening file fo +r write: $tempfile!\n"); # $total_lines=0; $hort=0; ** Prints to temp file the header record ** printf TEMP $printline,$hort, $dest_mailbox, $message_class; ** Prints to temp file the data from another file ** while (<FILE>) { $total_lines++; chomp($_); print TEMP "$_\n"; $data_lines++; } } ** Prints to TEMP file the trailer record. $hort=9; printf TEMP $printline, $hort, $dest_mailbox, $message_class;

The whole idea behind this code is to take a file that has data records and insert header and trailer records. The data moves over just fine, but the header and trailer records have a small square box which should be a new line character but is not. The file needs to have a the data on the next line. For some reason the temp file does not like the \n on the header and trailer record.

  • Comment on My Script is saving a New Line Character as an Unreadable character.
  • Download Code

Replies are listed 'Best First'.
Re: My Script is saving a New Line Character as an Unreadable character.
by Old_Gray_Bear (Bishop) on Feb 22, 2014 at 16:04 UTC
    Replace this line
    $message_class=shift(@ARGV); print(">>$message_class<<\n");
    and see what is really in the message class you are getting from the command line.

    I Go Back to Sleep, Now.


Re: My Script is saving a New Line Character as an Unreadable character. (binmode)
by Anonymous Monk on Feb 21, 2014 at 23:14 UTC
Re: My Script is saving a New Line Character as an Unreadable character.
by sundialsvc4 (Abbot) on Feb 24, 2014 at 12:53 UTC

    Here’s a great time to use a command-line tool like hexedit (or a programmer’s file-editor) to look at what are the actual bytes in both the input and the output files.   Armed with this information, it ought to be clear how to change the program to suit.   Without it, you are frankly going to be shooting in the dark.   The mere appearance of a square-box on the terminal screen merely indicates that the font-renderer did not know how to decode this particular byte-sequence into a graphic, given all sorts of parameters (including operating-system “locale” settings) that control such things.   To resolve issues like this, you need to look at the bytes.

    Perl, like all languages, has various settings to let it deal with things like Unicode, but sometimes you have to specify those things because the language cannot [always ...] know by examination of the data what the right way to deal with character-data is.   The same thing of course is true of end-of-line markers even in ASCII text.   Once you see the bytes, you should be able to deduce what to do ... and of course, we’ll be happy to help you.   Feel free to post a few hex-dump lines here, if you like.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075789]
Approved by GotToBTru
Front-paged by toolic
[Corion]: Oh yay. Thanks to The New Firefox, my irc client ChatZilla doesn't work anymore either... So I'll have to keep an old FF around just for that...
[Corion]: 100% of my FF add-ons are unsupported currently. Maybe upgrading Firefox wasn't such a good idea after all ;)
[Corion]: Also suffering is my 2-factor Github sign-in, as now, FF can't read my USB token anymore. "This was a horrible idea" is slowly dawning on me ;)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2017-11-20 20:16 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (293 votes). Check out past polls.