--- Charles Lu <[EMAIL PROTECTED]> wrote:
> If you want to write a subroutine that write the output to a text
> file you can do this:
> 
> open FILE ">temp.txt" or die;
> &function_one (\*FILE);
> 
> sub function_one{
>     my $fh = shift;
>     print $fh "blah blah blah\n";
> }
> 
> OR you can do this:
> 
> &function_one("temp.txt");
> 
> sub function_one{
>     my $file = shift;
>     open FILE ">$file" or die;
>     print FILE "blah blah blah\n";
> }
> 
> Why is the first method almost always the preferred one? 

It isn't *always*. It depends on the circumstances.
If the function is always working on the same file, it's probably a bad
idea to keep opening and closing it, especially since you aren't doing
an appending open, so you truncate every time, blasting the previous
contents. On the other hand, if the sub could encapsulate the whole
write, there are times when it would be better to have it handle all
the open/write/close process.

> More importantly, is it better to pass filehandle into a subroutin
> than pass in the name of  the output file as arugument and
> construct(define) the filehandle WITHIN the subroutine?

Same (see above).
Still, I'd use FileHandle when I could.
It makes cleaner code.

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to