I am beginner perl programmer. I have following perl code snippet and can not understand why I can not print the ascii characters ($ascii_chr) to OUTFILE. it seems to work fine when i print to console.
Can not understand why it is not dumping chacracters to OUTFILE in following print statement
print OUTFILE $ascii_chr; ## But this print statement does not work.
Here is a code snippet of this particular loop.
#Now look for start_conversion = 1 and then convert the hex data into ascii data
if (($start_conversion == 1) && ($_ != '5f535452') && ($_ != '5f454e44')) {
chomp;
$_ =~ s/000000/ 0x/g;
@my_array = split(/ /, $_);
foreach $split_word(@my_array) {
$ascii_chr = chr(hex($split_word));
print $ascii_chr; ## This print statement works.
print OUTFILE $ascii_chr; ## But this print statement does not work.
}
}
Here is complete code. I tried several things but could never print to OUTFILE.
#!/usr/bin/perl
$num_arg = $#ARGV + 1;
if (($ARGV[0] =~ /help/)) {
print "post_code_log.pl <inputfile> <outputfile> \n";
exit;
}
$infile_name = $ARGV[0];
$outfile_name = $ARGV[1];
#Logic to remove non-ascii characters from a text file
$count = 0;
$start_num = '5f535452';
$stop_num = '5f454e44';
$start_conversion = 0;
open (DATA, "$infile_name");
open (OUTFILE, ">$outfile_name");
while (<DATA>) {
s/^;.*//g; # Remove a line starting with ;
s/^\n//g; # Remove blank lines
s/.*?://; # Remove first column
s/.*?Port80Wr//; # Remove the first column look for Port80Wr
# (since the first column contains "Port80Wr")
s/^\s+//g; # Remove the space in front
#Look for start signature
if ($_ =~ '5f535452') {
print $_;
$start_conversion = 1;
}
#Look for stop signature
if ($_ =~ '5f454e44') {
print OUTFILE "\n"; # May need to print newline
print $_;
$start_conversion = 0;
}
# Now look for conversion start and then convert hex to ascii
if (($start_conversion == 1) && ($_ != '5f535452') && ($_ != '5f454e44')) {
chomp;
$_ =~ s/000000/ 0x/g;
@my_array = split(/ /, $_);
foreach $split_word(@my_array) {
$ascii_chr = chr(hex($split_word));
print $ascii_chr;
print OUTFILE $ascii_chr;
}
}
# Now look for conversion end and send the data
# as is without making any changes
if (($start_conversion == 0) && ($_ != '5f535452') && ($_ != '5f454e44')) {
print OUTFILE $_;
}
}
print "Done\n";