in reply to Filehandle Error

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.

Replies are listed 'Best First'.
Re^2: Filehandle Error
by muizelaar (Sexton) on Jul 09, 2007 at 14:46 UTC
    Hi I have re wrote my code using your example and it seems to have done the trick, I'll have to do a little more reading to fully understand the cahnges you have made. :) thanks for all your help much appreciated.