[comp.lang.perl] Implemented!

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

In article <5195@convex.convex.com>, tchrist@convex (Tom Christiansen) writes:
| I've been using formatted writes and I find that it's a real
| pain to be forced to associate one sole format() with a 
| given output channel.

Is there anything wrong with setting $~ and $^ (for format and
top-of-page) as you need them?  (Yup, helps to read the *whole*
manpage. :-)

Just another Perl hacker,
-- 
/=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!"=/

tchrist@convex.COM (Tom Christiansen) (02/08/90)

In article <1990Feb7.180424.2566@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
>In article <5195@convex.convex.com>, tchrist@convex (Tom Christiansen) writes:
>Is there anything wrong with setting $~ and $^ (for format and
>top-of-page) as you need them?  (Yup, helps to read the *whole*
>manpage. :-)

I've used $^ but had somehow missed $~.  Sigh, this is as stupid
as my flush() question.  I honestly do like to find ways to do 
these things without modifying the language.

Well, then, here's some tested code that seems to do the right thing.
They're trivial examples but demonstrative.  You could use indirection
on names rather than pass-by-reference on the files if you wanted.  I
haven't tried pass-by-reference with the formats though.

    #!usr/bin/perl

    format FOO =
    home is @<<<<<<<<<<<<<<<<<<<<<<<<
    $ENV{'HOME'}
    .

    format BAR =
    @>>>>>>>>>>>>>>>>>>>>>>>> is home
    $ENV{'HOME'}
    .


    sub write {
	local(*FH, $FORM) = @_;
	local($oldFH) = select(FH);
	local($oldFORM) = $~;
	$~ = $FORM;

	write;

	select($oldFH);
	$~ = $oldFORM;
    } 


    do write(*STDOUT,'FOO');
    do write(*STDERR,'BAR');
    do write(*STDOUT,'FOO');
    do write(*STDERR,'BAR');

In fact, you could extend this using Vroman's write() subroutine 
so you could have a syntax of 

    do write(*FH, 'formname', $var1, $var2, ...);

if you knew ahead of time that the formats used @_ for their vars.


--tom
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/08/90)

In article <1990Feb7.180424.2566@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
: Is there anything wrong with setting $~ and $^ (for format and
: top-of-page) as you need them?

Well, yes, actually.  They're klunky.  I wish I'd designed that part different.

Larry