[comp.lang.perl] formats inside an eval

raymond@sunkist.berkeley.edu (Raymond Chen) (03/21/90)

My perl man page says

eval EXPR
	EXPR ... is executed in the context of the current perl program,
	so that any variable settings, subroutine or format definitions 
	remain afterwards.

Now everybody uses eval for dynamic variable settings and subroutines,
so I figured, hey, why not dynamic formats?

% cat foo
eval "format =
An on-the-fly format.
.
";
print "Error: $@\n";
% perl foo				(Patchlevel 12)
Error: syntax error in file (eval) at line 2, next 2 tokens "=
An"

Changing "format =" to "format STDOUT =" didn't seem to help any.

The usual concluding sentences:
  Am I doing something wrong?
  If I'm not in error, has this been fixed in a recent patch?

sub _{print shift;"&_('@_')";}$"="','";$_=&_(split(//,"Not another perl hacker,"));$_=eval while!/''/;
--
raymond@math.berkeley.edu   "And perl pulls ahead of C, but APL still has
                             a commanding lead..." --- play-by-play of the
                            `Most closely approximates line noise' competition

jgreely@oz.cis.ohio-state.edu (J Greely) (03/21/90)

In article <1990Mar21.073635.25523@agate.berkeley.edu>
 raymond@sunkist.berkeley.edu (Raymond Chen) writes:
>Now everybody uses eval for dynamic variable settings and subroutines,
>so I figured, hey, why not dynamic formats?

Larry will probably explain this one in greater detail, but the short
version is that it just doesn't work.  Note that you can use 'do' to
accomplish what you want:

&feval(<<'EOF');
format=
@||
$foo
.
EOF
$foo="X";
write;

sub feval {
	local($format,$tempfile) = (@_,"/tmp/feval.$$");
	open(tempfile,">$tempfile") || die "$tempfile: $!\n";
	print tempfile $format;
	close(tempfile);
	do $tempfile;
	die "feval: $@" if $@;
	unlink($tempfile);
}

  Now all we need is an easy way to capture the output of a write.
Maybe a companion named "swrite"?
--
J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)

raymond@hilbert.berkeley.edu (Raymond Chen) (03/23/90)

In article <JGREELY.90Mar21050134@oz.cis.ohio-state.edu> 
        J Greely <jgreely@cis.ohio-state.edu> writes:
>Larry will probably explain this one in greater detail, but the short
>version is that it just doesn't work.

If the failure of formats to work inside an eval is intended behaviour,
shall I take it that the man page is in error?  After all, the phrase
"format definition" is explicitly listed as one of the things that eval
preserves.

Can we get an official ruling from Larry on this?

$_="krJhruaesrltre c a cnp,ohet";$_.=$1,print$2while s/(..)(.)//;

(Any by the way, who is archiving all these one-liners anyways?)
--
raymond@math.berkeley.edu     Maintainer of the csip Frequently Asked Questions

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/23/90)

In article <1990Mar23.043757.26371@agate.berkeley.edu> raymond@hilbert.UUCP (Raymond Chen) writes:
: If the failure of formats to work inside an eval is intended behaviour,
: shall I take it that the man page is in error?  After all, the phrase
: "format definition" is explicitly listed as one of the things that eval
: preserves.
: 
: Can we get an official ruling from Larry on this?

The official ruling is that I've just fixed formats to work inside
evals, and the fix will be in patch 16.

By the by, there are a couple of other items that will be in patch 16.
Some MSDOS support, for one.  Also, you'll be able to say

	@alphabet = ('a' .. 'z');

: $_="krJhruaesrltre c a cnp,ohet";$_.=$1,print$2while s/(..)(.)//;
: 
: (Any by the way, who is archiving all these one-liners anyways?)

I am, I guess.

Larry