I am writing a perl script to parse a 'c' header file and extract enumeration types and their respective members.
- Ubuntu 12.04
- Perl 5.14.2
The perl script seems to work, but in some circumstances some of the variables will not print. I've stripped down the code to the following minimal duplication.
I'm pretty sure this has something to do with buffering... because if I add a newline between printing the first variable and the other variables it works.
I did read through suffering from buffering and Perl not printing properly, without finding a solution to my own issue.
Here's the header file:
#ifndef _ENUM_H_
#define _ENUM_H_ 1
/**
* Enum definition
*/
typedef enum
{
eENUMVAL0 = 0,
eENUMVAL1,
eENUMVAL2,
eENUMVAL_LAST,
} eMySuperEnum;
#endif /* _ENUM_H_ */
And here's the script:
#! /usr/bin/perl
use strict;
use warnings;
our @apiFile = ();
if (exists $ARGV[0])
{
chomp( @apiFile = `cat $ARGV[0]`);
}
for my $i (0 .. $#apiFile)
{
if ($apiFile[$i] =~ /^\s*typedef\s+enum/)
{
print "$apiFile[$i] starts at $i\n";
}
}
This prints:
starts at 6
But if I add a newline in the print after $apiFile[$i], then it prints the whole thing:
print "$apiFile[$i]\nstarts at $i\n";
Will print:
typedef enum
starts at 6
I have many times printed multiple variables on the same line. What is happening under the hood such that perl is not printing these two variables on a single line?