[EMAIL PROTECTED] <> wrote:

: #!/usr/bin/perl
: require RTF::TEXT::Converter;
: my $inputfile="document.rtf";
: my $outputfile="document.txt";
:
: open(IN,"<$inputfile") or die;
: open(OUT,">$outputfile") or die;
: my $self = new RTF::TEXT::Converter(Output => \*OUT);
: foreach my $inputfile (@ARGV) {
:  $self->parse_stream($outputfile);
: }

    One problem is here, you want to parse the input file, not the
output file.


: close(IN);
: close(OUT);
:
: What is wrong whith this script?

    IMO, it should first start with strictures and warnings
enforced. I don't use it here, but perhaps taint checking (T)
should also be turned on.

#!/usr/bin/perl

use strict;
use warnings;

require RTF::TEXT::Converter;
my $inputfile="document.rtf";
my $outputfile="document.txt";

open(IN,"<$inputfile") or die;
open(OUT,">$outputfile") or die;
my $self = new RTF::TEXT::Converter(Output => \*OUT);
foreach my $inputfile (@ARGV) {
 $self->parse_stream($inputfile);
}
close(IN);
close(OUT);

__END__


    Also, modules are more commonly used than required. Unless you
plan to subclass RTF::TEXT::Converter, use it.

    Since RTF::TEXT::Converter is a subclass of RTF::Control,
there is no reason to open files for input. We just pass the file
name into the parse_stream() method.

#!/usr/bin/perl

use strict;
use warnings;

use RTF::TEXT::Converter;

my $outputfile="document.txt";

open(OUT,">$outputfile") or die;
my $self = new RTF::TEXT::Converter(Output => \*OUT);
foreach my $inputfile (@ARGV) {
 $self->parse_stream($inputfile);
}
close(OUT);


    Now we'll change that open to a nicer format and add more
white space. We'll change the names of some of the variables to
describe what they hold rather than where they come from.

    And finally, we will stuff @ARGV with a value we already know
is an RTF file to avoid a command line typos and to begin to test
the script.

#!/usr/bin/perl

use strict;
use warnings;

use RTF::TEXT::Converter;

{   # Stuff ARGV for testing -- Comment out this block in production.
    # This code block stuffs @ARGV with various files which we
    # know ahead of time should pass or fail. It gives us a chance
    # to check reactions to errors without worrying about all that
    # command line typing.

    my $rtf_path = 'C:/Documents and Settings/Charles/My Documents/Real
Estate';

    @ARGV = "$rtf_path/Roth IRA Explained.rtf";
}

my $file = 'document.txt';
open my $text, '>', $file or die qq(Cannot open "$file": $!);

my $rtf_converter = RTF::TEXT::Converter->new( Output => $text );

foreach my $rtf_file ( @ARGV ) {
    $rtf_converter->parse_stream( $rtf_file );
}

close $text;

__END__



    Good, so far. Let's do more rigorous testing.

#!/usr/bin/perl

use strict;
use warnings;

use RTF::TEXT::Converter;

{   # Stuff @ARGV for testing -- Comment out this block in production.
    # This code block stuffs @ARGV with various files which we
    # know ahead of time should pass or fail. It gives us a chance
    # to check reactions to errors without worrying about all that
    # command line typing.

    my $rtf_path = 'C:/Documents and Settings/Charles/My Documents/Real
Estate';

    @ARGV = (
        1,                                      # should fail -- I think,
        "$rtf_path/Roth IRA Explained.rtf",     # should succeed,
        "$rtf_path/Roth IRA Explained",         # should fail,
    );
}

my $file = 'document.txt';
open my $text, '>', $file or die qq(Cannot open "$file": $!);

my $rtf_converter = RTF::TEXT::Converter->new( Output => $text );

foreach my $rtf_file ( @ARGV ) {
    $rtf_converter->parse_stream( $rtf_file );
}

close $text;

__END__

    In this revision I realized that the script will only work as
far as the first missing file. This may or may not present a
problem. The reason is that RTF::Parser croaks (dies) on file
open errors. We could open our own files and skip files which
didn't open.



#!/usr/bin/perl

use strict;
use warnings;

use RTF::TEXT::Converter;

{   # Stuff @ARGV for testing -- Comment out this block in production.
    # This code block stuffs @ARGV with various files which we
    # know ahead of time should pass or fail.

    my $rtf_path = 'C:/Documents and Settings/Charles/My Documents/Real
Estate';

    @ARGV = (
        1,                                      # should fail -- I think,
        "$rtf_path/Roth IRA Explained.rtf",     # should succeed,
        "$rtf_path/Roth IRA Explained",         # should fail,
        'document.txt',                         # not an RTF file
    );
}


my $file = 'document.txt';
open my $text, '>', $file or die qq(Cannot open "$file": $!);

my $rtf_converter = RTF::TEXT::Converter->new( Output => $text );

foreach my $rtf_file ( @ARGV ) {
    my $rtf_fh = open_file( $rtf_file ) or next;
    $rtf_converter->parse_stream( $rtf_fh );
}

close $text;

sub open_file {
    my $file = shift;

    my $fh;
    return $fh if open $fh, '<', $file;

    # file didn't open
    warn qq("$file" didn't open: $!);

    # return undef (false)
    return;
}

__END__

    In the end we can delete that test block.

HTH,


Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328




#!/usr/bin/perl

use strict;
use warnings;

use RTF::TEXT::Converter;

my $file = 'document.txt';
open my $text, '>', $file or die qq(Cannot open "$file": $!);

my $rtf_converter = RTF::TEXT::Converter->new( Output => $text );

foreach my $rtf_file ( @ARGV ) {
    my $rtf_fh = open_file( $rtf_file ) or next;
    $rtf_converter->parse_stream( $rtf_fh );
}

close $text;

sub open_file {
    my $file = shift;

    my $fh;
    return $fh if open $fh, '<', $file;

    # file didn't open
    warn qq("$file" didn't open: $!);

    # return undef (false)
    return;
}

__END__

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to