[comp.lang.perl] Passing file handles as parameters

crehta@tasu74.UUCP (Ran Ever-Hadani) (05/02/90)

I have a perl program that keeps many similar output files 
open simultaniously.

I have a single routine which does the formatting of output
to all of them.  I would love to be able to call the routine
with the output file as a parameter.  Would this be possible
in the future?

What I tried do far was this:

- I had the routine print to its own file handle OUT, which I attached
  to the output file; the calling line was 
	open (OUT, ">&$file"); &do_out;
  This forced me to set all outputs to unbuffered, which I didn't want.

- I passed a string containing the filehandle name as a parameter,
  and called eval to insert it to a print command;  This made the
  program about 15 times slower.

- I defined an output format for each of the files which contained
  a single variable $out_line.  This seemed the best sollution.

-- Ran
  my program about 15 times slower than simply copying the damn
  routine inline without the eval.

- 
-----------------------------------------------
Reply-To: crehta@taux01.nsc.com (Ran Ever-Hadani)
Disclaimer: The above is to be attributed to me only, not to any organization.
Apology: Bad English.  E-mailed spelling and style corrections are welcome.

merlyn@iwarp.intel.com (Randal Schwartz) (05/02/90)

In article <3751@taux01.UUCP>, crehta@tasu74 (Ran Ever-Hadani) writes:
| I have a single routine which does the formatting of output
| to all of them.  I would love to be able to call the routine
| with the output file as a parameter.  Would this be possible
| in the future?

It has been possible for a long time.

sub say_hello {
	local($filehandle) = @_;

	print $filehandle "hello world\n";
}

&say_hello("STDOUT");
&say_hello("STDERR");
open(SMURF,">/dev/tty");
&say_hello("SMURF");
close(SMURF);

Pretty durn easy if you ask me.  And it's even documented!

$_ = "Jvtu bopuifs Pfsm ibdlfs,"; y/a-z/za-y/; print
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

crehta@tasu74.UUCP (Ran Ever-Hadani) (05/03/90)

In article <1990May2.164445.11633@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
>In article <3751@taux01.UUCP>, crehta@tasu74 (Ran Ever-Hadani) writes:
>|                    ....               Would this be possible
>| in the future?
>
>It has been possible for a long time.

>	print $filehandle "hello world\n";

>Pretty durn easy if you ask me.  And it's even documented!

I plea not guilty.

I tried

    print "STDOUT" "Hello\n";

Which gave a syntax error.  I didn't realise the possibility that
$variable as a special case may be used where EXPR is not legal,
so I didn't look it up in the manual.  

Sorry

-- Ran
-----------------------------------------------
Reply-To: crehta@taux01.nsc.com (Ran Ever-Hadani)
Disclaimer: The above is to be attributed to me only, not to any organization.
Apology: Bad English.  E-mailed spelling and style corrections are welcome.