Hello Monks,

I wrote a program that reads from an input file and that write hte output to another file.To give it more versatility, I want to see if there is an argument specifying the pathes and names of the files otherwise to read from STDIN and STDOUT. I sort of successed in doing that even though I don't know if is the most beautiful code. I just have a warning I can't get rid of.

Example of code and error message provided

Here is an example I wrote just to show you, so it doesn't actually do anything. However it outputs the warning produces by the pragma diagnostics (so I know that if I would get rid of the pragma, I would get rid of the message, but is that moral :-) )

Here is the code

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Benchmark; use Getopt::Long qw(:config no_ignore_case bundling); #defines variables my $man = 0; my $help = 0; my $input; my $output; my @blubb; my %options = ( 'help|h' => \$help, 'man' => \$man, 'input|i=s' => \$input, 'output|o=s' => \$output, ); GetOptions(%options); open STDIN, "<$input" or die "Can't open $input: $!" if $input; while(<STDIN>) { chomp; push @blubb, $_; } close STDIN if $input; open STDOUT, ">$output" or die "Can't open $output: $!" if $output; print join "\n", @blubb; print "\n"; close STDOUT if $output; exit 0;

And here is the error message

Filehandle STDIN reopened as STDOUT only for output at ex.pl line 36 ( +#1) (W io) You opened for writing a filehandle that got the same filehandl +e id as STDIN. This occured because you closed STDIN previously.

I don't understand this error message as I don't touch STDIN at line 36.
Anyone has a clue about the reason?

Thanks in advance

--
jey

In reply to problem of filehandling by jey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.