Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SUBROUTINES/FILEHANDLES 1

Status
Not open for further replies.

nerri

Programmer
Sep 1, 2006
21
US
So there isn't a problem. That has already been solved. But the solution doesn't make sense to me. If someone could please explain how filehandles and subroutines act and interact, I would appreciate it. Here's what happened.

Code:
sub one{
  open (FOUT, ">sample.txt") || die "Cannot open file: $!";
  print FOUT "I am a happy file.\n";
}

sub two{
  open (FOUT, ">>sample.txt") || die "Cannot open file: $!";
  print FOUT "No problems here!\n";
}

Now if I run one and then two, only sub one will print to sample.txt. If I change the filehandle in two, to FOUTS, there is no problem. If I tell sub two to create a file, instead of using an already created file, it prints normally to the newly created file, even if I reuse the filehandle. How can this behavior be explained?
 
It works fine on my environment.

Running "one();two();" outputs the following results to sample.txt: "I am a happy file.\nNo problems here!\n".

I personally would always make sure to close filehandles when they are done, but it's not strictly necessary in this code example. Anyway, can't explain why you are having problems.
 
works for me as well. No warnings, no errors, the file is printed to:

I am a happy file.
No problems here!


Code:
[maroon]one[/maroon][red]([/red][red])[/red][red];[/red]
[maroon]two[/maroon][red]([/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]one[/maroon][red]{[/red]
  [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red]FOUT, [red]"[/red][purple]>sample.txt[/purple][red]"[/red][red])[/red] || [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Cannot open file: [blue]$![/blue][/purple][red]"[/red][red];[/red]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] FOUT [red]"[/red][purple]I am a happy file.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]sub[/b][/black] [maroon]two[/maroon][red]{[/red]
  [black][b]open[/b][/black] [red]([/red]FOUT, [red]"[/red][purple]>>sample.txt[/purple][red]"[/red][red])[/red] || [black][b]die[/b][/black] [red]"[/red][purple]Cannot open file: [blue]$![/blue][/purple][red]"[/red][red];[/red]
  [black][b]print[/b][/black] FOUT [red]"[/red][purple]No problems here![purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

what operating system are you using?




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm using a Sun Machine. Would that do something weird? Is there an fflush equivalent I should be using if it's just the computer being finicky?

I mean, the code I typed above isn't the actual code I'm using (trade secret company ickness), but I thought that it should be a fair comparison. Just to add another thought, all the files sit in the same directory, which I own and I have read, write and execute permissions on all the files.
 
try the actual code you posted and report back.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I thought I read somewhere that all file handles in Perl are global. And global as in really global indeed!

So that is why it does work when you give one of them another name.
In the example you are just trying to open an already opened handle...

So when some of the other guys got your script running those were the odd cases really, I think.
 
They are indeed global. However when you open it for the second time, you implicity close the first file. I see the same behaviour that Miller and Kevin are getting, which is what I'm expecting. (This is perl, v5.8.8 built for i486-linux-gnu-thread-multi)
 
I think I must be insane. I took out the fix I had put in and reran for more troubleshooting and it works now. I *hate* it when things fix themselves. Now I cannot know the reason this happened. In any case, I will update if anything new and relevant happens with this program. Thanks for all your help, everyone.

And thanks especially for letting me know the filehandles are global. I had thought that one opened in a sub would be local.
 
you can make them "local" by using an indirect filehandle:

Code:
sub one{
  open ([b]my $FOUT[/b], ">sample.txt") || die "Cannot open file: $!";
  print $FOUT "I am a happy file.\n";
}

sub two{
  open ([b]my $FOUT[/b], ">>sample.txt") || die "Cannot open file: $!";
  print $FOUT "No problems here!\n";
}

if they go out of scope perl even closes them for you.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top