Why can I not name my own formats?

E

elhombre

The following code attempts to create a report of any line created by diff
in the format nnnannn, 10c10, 10a11 etc. However perl complains about
"write() on unopened filehandle OUTPUT ", however I'm not writing to the
filehandle, I'm trying to write to the defined format which I understand has
its own namespace?

I'm not using STDOUT for this section as that is used for the first part of
the report that lists the file sizes, number of words etc.

Any hints?

Thanks in advance.




use English;
#Check that the correct number of arguments are passed to the interpreter
if($#ARGV != 1) {
print "usage : levelA.pl file1 file2";
exit;
}
system("diff -l -w $ARGV[0] $ARGV[1] > output.txt"); #For *nix
#now report the lines that are different
open(OUTPUT_FILE, "output.txt") or die "File $_ does not exist";
my $FORMAT_NAME = "OUTPUT";
my $FORMAT_TOP_NAME = "OUTPUT_TOP";
while (our $ln = <OUTPUT_FILE>) {
if($ln =~ m/^[0-9]*[a c][0-9]*/) {
write OUTPUT;
}
}
format OUTPUT_TOP =
line
------------------------------------------------
..
format OUTPUT =
@<<<<<<<<<<<<<<<<<<<<<<<<
our $ln
..
 
C

cmic

Hello
The following code attempts to create a report of any line created by diff
in the format nnnannn, 10c10, 10a11 etc. However perl complains about
"write() on unopened filehandle OUTPUT ", however I'm not writing to the
filehandle, I'm trying to write to the defined format which I understand has
its own namespace?

I'm not using STDOUT for this section as that is used for the first part of
the report that lists the file sizes, number of words etc.

Any hints?
Yes. You should assign the format to $~, (the name of the current
report format) then write
Provided your formats are complete with the variables you want to
output, you could write somethink like :

local $~="OUTPUT_TOP";
write;
local $~="OUTPUT";
write;

rgds
--
michel marcon aka cmic

Thanks in advance.

use English;
#Check that the correct number of arguments are passed to the interpreter
if($#ARGV != 1) {
    print "usage : levelA.pl file1 file2";
    exit;}

system("diff -l -w $ARGV[0] $ARGV[1] > output.txt");  #For *nix
#now report the lines that are different
open(OUTPUT_FILE, "output.txt") or die "File $_ does not exist";
my $FORMAT_NAME = "OUTPUT";
my $FORMAT_TOP_NAME = "OUTPUT_TOP";
while (our $ln = <OUTPUT_FILE>) {
    if($ln =~ m/^[0-9]*[a c][0-9]*/) {
        write OUTPUT;
    }}

format OUTPUT_TOP =
line
------------------------------------------------
.
format OUTPUT =
@<<<<<<<<<<<<<<<<<<<<<<<<
our $ln
.
 
E

elhombre

Yes. You should assign the format to $~, (the name of the current
report format) then write
Provided your formats are complete with the variables you want to
output, you could write somethink like :

local $~="OUTPUT_TOP";
write;
local $~="OUTPUT";
write;

rgds
Thanks Michel. I would never have figured this out on my own. I have a lot
to learn about this cryptic language.
 
T

Tad J McClellan

elhombre said:
The following code attempts to create a report of any line created by diff
in the format nnnannn, 10c10, 10a11 etc. However perl complains about
"write() on unopened filehandle OUTPUT ", however I'm not writing to the
filehandle,


Yes you are.

I'm trying to write to the defined format which I understand has
its own namespace?


You cannot write() to a format, you can only write() to a filehandle.

Any hints?


open() an OUTPUT filehandle.

open OUTPUT, '>', 'report_out.txt' or
die "could not open 'report_out.txt' $!";

use English;


Why have you included this module?

You do not make use of it anywhere...

#Check that the correct number of arguments are passed to the interpreter
if($#ARGV != 1) {
print "usage : levelA.pl file1 file2";
exit;
}


Error messages should go to STDERR, not STDOUT.

Programs that encounter an error should have a NON-zero exit value.

Using an array in scalar context is easier to understand that
the funky $#array syntax.

You can replace that entire if block with:

warn "usage : levelA.pl file1 file2" unless @ARGV == 2;

system("diff -l -w $ARGV[0] $ARGV[1] > output.txt"); #For *nix
#now report the lines that are different
open(OUTPUT_FILE, "output.txt") or die "File $_ does not exist";
^^
^^

What value do you expect $_ to have at this point in your program?

You should include the $! special variable in your error message.

There is no need for the output.txt intermediate file, unless
you happen to need it for something else.

You should choose accurate descriptive names for your filehandles.


open DIFF_OUT, "diff -l -w @ARGV|" or die "problem running diff $!";

my $FORMAT_NAME = "OUTPUT";
my $FORMAT_TOP_NAME = "OUTPUT_TOP";


What purpose do those assignments have?

You do not make use of those variables anywhere...

while (our $ln = <OUTPUT_FILE>) {
if($ln =~ m/^[0-9]*[a c][0-9]*/) {


That will match lines that have no digit characters at all.

It will match lines that start with a space character.

It will not match the diff output for lines that were deleted.

if ( $ln =~ m/^[0-9]+[acd][0-9]+$/ ) {

write OUTPUT;


perldoc -f write

shows that write() may be called in one of three ways:
write FILEHANDLE
write EXPR
write

You are using the 1st way, where you need to provide a filehandle.
 
E

elhombre

Thank you Tad !

Tad J McClellan said:
elhombre said:
The following code attempts to create a report of any line created by
diff
in the format nnnannn, 10c10, 10a11 etc. However perl complains about
"write() on unopened filehandle OUTPUT ", however I'm not writing to the
filehandle,


Yes you are.

I'm trying to write to the defined format which I understand has
its own namespace?


You cannot write() to a format, you can only write() to a filehandle.

Any hints?


open() an OUTPUT filehandle.

open OUTPUT, '>', 'report_out.txt' or
die "could not open 'report_out.txt' $!";

use English;


Why have you included this module?

You do not make use of it anywhere...

#Check that the correct number of arguments are passed to the interpreter
if($#ARGV != 1) {
print "usage : levelA.pl file1 file2";
exit;
}


Error messages should go to STDERR, not STDOUT.

Programs that encounter an error should have a NON-zero exit value.

Using an array in scalar context is easier to understand that
the funky $#array syntax.

You can replace that entire if block with:

warn "usage : levelA.pl file1 file2" unless @ARGV == 2;

system("diff -l -w $ARGV[0] $ARGV[1] > output.txt"); #For *nix
#now report the lines that are different
open(OUTPUT_FILE, "output.txt") or die "File $_ does not exist";
^^
^^

What value do you expect $_ to have at this point in your program?

You should include the $! special variable in your error message.

There is no need for the output.txt intermediate file, unless
you happen to need it for something else.

You should choose accurate descriptive names for your filehandles.


open DIFF_OUT, "diff -l -w @ARGV|" or die "problem running diff $!";

my $FORMAT_NAME = "OUTPUT";
my $FORMAT_TOP_NAME = "OUTPUT_TOP";


What purpose do those assignments have?

You do not make use of those variables anywhere...

while (our $ln = <OUTPUT_FILE>) {
if($ln =~ m/^[0-9]*[a c][0-9]*/) {


That will match lines that have no digit characters at all.

It will match lines that start with a space character.

It will not match the diff output for lines that were deleted.

if ( $ln =~ m/^[0-9]+[acd][0-9]+$/ ) {

write OUTPUT;


perldoc -f write

shows that write() may be called in one of three ways:
write FILEHANDLE
write EXPR
write

You are using the 1st way, where you need to provide a filehandle.

}
}
format OUTPUT_TOP =
line
------------------------------------------------
.
format OUTPUT =
@<<<<<<<<<<<<<<<<<<<<<<<<
our $ln
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top