Take a look at perldoc perldiag under the explanation for the error message (specifically "Filehandle %s reopened as %s only for input"). You're closing STDOUT, the subsequent open of MYFILE gets the STDOUT filehandle id, and since you're only opening that for reading Perl warns you about this seemingly erratic behaviour.

While there's nothing per se wrong with your code there are a few areas where it could be improved. Here's an alternative version (which also avoids the warning by not opening a file as STDOUT, which I consider bad practice):

sub vpenv { my $file = shift; open(my $readf,"<","$file") or die "Unable to open file: $!"; my @stuff=<$readf>; close($readf); $stuff[0]="setenv DSCUSTOMER $db\n"; $stuff[1]="setenv DSSITENO $sno\n"; $stuff[2]="setenv DSCUSTID $sid\n"; open (my $writef,">","$file") or die "Unable to Write to file: $!"; print $writef @stuff; close($writef); } #Updates database.form sub vpdb { my $file = shift; open(my $readf,"<","$file") or die "Unable to Open File: $!"; my @thing=<$readf>; close($readf); $thing[0]=".ARG DATABASE $db\n"; open(my $writef,">",$file") or die "Unable to Write to File: $!"; print $writef @thing; close($writef); } vpenv("$location/$dir/$site/$vpfile"); vpdb("$location/$dir/$site/$vpdir/$vpdata");

Or, even better, using Tie::File:

use Tie::File; sub vpenv { my $file = shift; tie my @stuff, 'Tie::File', $file or die "Unable to tie $file: $!"; $stuff[0]="setenv DSCUSTOMER $db\n"; $stuff[1]="setenv DSSITENO $sno\n"; $stuff[2]="setenv DSCUSTID $sid\n"; untie @stuff; } sub vpdb { my $file = shift; tie my @thing, 'Tie::File', $file or die "Unable to tie $file: $!"; $thing[0]=".ARG DATABASE $db\n"; untie @thing; } vpenv("$location/$dir/$site/$vpfile"); vpdb("$location/$dir/$site/$vpdir/$vpdata");

All dogma is stupid.

In reply to Re: Filehandle Error by tirwhan
in thread Filehandle Error by muizelaar

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.