Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PATCH] format/write (version 2)

0 views
Skip to first unread message

Laun Wolfgang

unread,
Jan 13, 2004, 6:28:31 AM1/13/04
to perl5-...@perl.org
One bugfix added (according to a note from H.Merijn Brand): A single '}' doesn't terminate a format any more. I guess that's it. Well, maybe...
Regards
Wolfgang

=head1 SUMMARY

This patch eliminates a few bugs in format/write and adds a couple of
new features. It's against 5.8.2, with patches #22005 and #22055
(fixing that a format in eval must be properly delimited) added.
A major part of F<perlform.pod> was rewritten. Merijn H. Brand's question
about the handling of control chracters in text fields is discussed in
more detail below (L</The Handling of Control Characters).

One bugfix (L</Bugfix: Error when copying non-UTF to UTF (EBCDIC only)>)
needs to be verified by someone who knows all about UTF on EBCDIC.


=head1 THE CHANGES

=head2 Improvement: NULL chars in picture line

A NULL character embedded in a picture line (in eval) is now tolerated
and does not cause a premature termination of the format. NULLs will not
be treated like LFs (in F<pp_ctl.c>, doparseform) any more which would cause
spurious empty lines.


=head2 Bugfix: Data lost in ^<<<< field if data =~ /\r/

A caret field for text is supposed to pull in text from the SV, modifying
it, either up to the first C<\r> or some suitable line breaking position
(as stored in C<$:>). If there is a C<\r> in the text and if all of the
text would fit into the field, the remainder of the text is lost.

$txt = "line 1\rline 2";
format STDOUT =
^<<<<<<<<<<<<<<<<<<
$txt
^<<<<<<<<<<<<<<<<<<
$txt
.
write;
================================
line 1

================================


=head2 Bugfix: C<@*> shown in output if not alone on a line

The manual says that C<@*> should be alone on a picture line of its own.
The parser accepts (e.g.) literal text before and after, but then some
garbage characters are printed and literl text is lost:

my $txt = "World";
format STDOUT =
Hello @* !
$txt
.
write;
=================================
Hello World@*
=================================


=head2 New feature: C<^*> for variable-width, one-line-at-a-time text

This combines the idea of the C<@*> field with the side effect of caret
fields. The first line (up to C<\n> is removed from the SV and becomes
the value for the field. This permits easy formatting of hanging
indentation, when you don't want to worry about the length of the
lines and/or prefer to keep the lines as they are:

my $text = "line 1\nline 2\nline 3";
format STDOUT =
Text: ^*
$text
~~ ^*
$text
.
=================================
Text: line 1
line 2
line 3
=================================

Up to now, C<^*> was treated just like C<@*>, although not documented so.
Actually, this should have been interpreted as a caret field (length 1),
followed by a plain text C<*>. I preferred the new feature to fixing
this bug.


=head2 Improvement: Diagnostic on C<@#> and C<~~>

This combination will always cause a "runaway format" death. It's safer
and permits a better diagnostic if this is caught at format compile time:
"Repeated format line will never terminate (~~ and @#) at..."


=head2 Bugfix: Segmentation fault on big numbers

Big numbers are likely to cause a segmentation fault when printed through
a numeric format field. (sprintf used on short allocation.) This brings
us to the next item:


=head2 Improvement (maybe): Truncation of numbers produces misleading output

my @a = (1e3, 1e6, 1e9);
format STDOUT =
^###### ~~
shift(@a)
.
write;
==================================
1000
1000000
1000000
==================================

This is not nice: silent truncation produces some erroneous output, which
might easily go undetected. There aren't too many options to deal with
this (also considering the segmentation fault on big numbers), but I think
that an overflow evidence is the best solution: If a number does not fit,
the field is filled with "#".


=head2 Bugfix: "}" terminates format

format STDOUT =
@<<<<<<<
"line 1"
}
.
write;
======================================
syntax error at ..., near "."
======================================

Cf. change in toke.c, scan_formline. (A single '}' to terminate an
expression block is parsed in another lexer mode.)


=head2 Bugfix: Error when copying non-UTF to UTF (EBCDIC only)

The code for copying non-UTF text to the output buffer in F<pp_ctl.c>
(l.623) didn't look right to me. This was the snippet:

for (; t < SvEND(PL_formtarget); t++) {
#ifdef EBCDIC
int ch = *t++ = *s++; // Why copy again and t++ again?
if (iscntrl(ch))
#else
if (!(*t & ~31))
#endif
*t = ' ';

I think that the 3rd line has to be changed to

int ch = *t;


=head2 The Handling of Control Characters

In a text field (except for the special C<@*>) not just a C<tr/\n\t\f/ /> is
done, but all characters with a code < 0x20 (on ASCII, or all isCNTRL() on
EBCDIC) are replaced by a space. (The F<perlform.pod> merely told about
C<\n\t\f>, that has been corrected.)

There is, of course, a perfectly good reason to do so. Control characters
are apt to "disappear" (or do other fancy things) on output media, causing
misaligment (or worse) in subsequent fields.

The code in F<pp_ctl.c> uses the test C<!(ch & ~31)> to distinguish
control characters from others. This omits DEL (0x7F), which shows as a
dotted box on my Linux xterm (but other programs may have other ideas).

More specifically, Merijn asked about escape. Escape sequences are
particularly nasty since they contain printing characters which are
left as they are, e.g. "\e[1m" will be written as " [1m". Although
ANSI escape sequences could be easily recognized, a treatment of
escape sequences as "null strings" (not to be counted for alignment)
isn't simple: There are at fields and caret fields to distinguish,
and there is UTF8 nd non-UTF8, and there is ASCII and EBCDIC...

Note that the following hack is (now) possible for emitting escape
sequences without ruining field alignment in the output (albeit you
won't have such a nice alignment of fields in the format layout any
more):

format STDOUT =
@*@<<<<<<<<<<<<<<<<<<<<<<@*
"\e[1m", "bold face", "\e[0m"
.
write;


=head1 PERLFORM.POD

For easy reading, here are the portions of perlform.pod that were
rewritten:

--8<--

=head1 NAME

perlform - Perl formats

=head1 DESCRIPTION

[SNIP]

Output record formats are declared as follows:

format NAME =
FORMLIST
.

If the name is omitted, format "STDOUT" is defined. A single "." in
column 1 is used to terminate a format. FORMLIST consists of a sequence
of lines, each of which may be one of three types:

=over 4

=item 1.

A comment, indicated by putting a '#' in the first column.

=item 2.

A "picture" line giving the format for one output line.

=item 3.

An argument line supplying values to plug into the previous picture line.

=back

Picture lines contain output field definitions, intermingled with
literal text. These lines do not undergo any kind of variable interpolation.
Field definitions are made up from a set of characters, for starting and
extending a field to its desired width. This is the complete set of
characters for field definitions:

@ start of regular field
^ start of special field
< pad character for left adjustification
| pad character for centering
> pad character for right adjustificat
# pad character for a right justified numeric field
0 instead of first #: pad number with leading zeroes
. decimal point within a numeric field
... terminate a text field, show "..." as truncation evidence
@* variable width field for multi-line values
^* variable width field for next line of a multi-line value
~ suppress line with all fields empty
~~ repeat line until all fields are exhausted

Each field in a picture line starts with either "@" (at) or "^" (caret),
indicating what we'll call, respectively, a "regular" or "special" field.
The choice of pad characters determines whether a field is textual or
numeric. The tilde operators are not part of a field. Let's look at
the various possibilities in detail.


=head2 Text Fields

The length of the field is supplied by padding out the field with multiple
"E<lt>", "E<gt>", or "|" characters to specify a non-numeric field with,
respectively, left justification, right justification, or centering.
For a regular field, the value (up to the first newline) is taken and
printed according to the selected justification, truncating excess characters.
If you terminate a text field with "...", three dots will be shown if
the value is truncated. A special text field may be used to do rudimentary
multi-line text block filling; see L</Using Fill Mode> for details.

Example:
format STDOUT =
@<<<<<< @|||||| @>>>>>>
"left", "middle", "right"
.
Output:
left middle right


=head2 Numeric Fields

Using "#" as a padding character specifies a numeric field, with
right justification. An optional "." defines the position of the
decimal point. With a "0" (zero) instead of the first "#", the
formatted number will be padded with leading zeroes if necessary.
A special numeric field is blanked out if the value is undefined.
If the resulting value would exceed the width specified the field is
filled with "#" as overflow evidence.

Example:
format STDOUT =
@### @.### @##.### @### @### ^####
42, 3.1415, undef, 0, 10000, undef
.
Output:
42 3.142 0.000 0 ####


=head2 The Field @* for Variable Width Multi-Line Text

The field "@*" can be used for printing multi-line, nontruncated
values; it should (but need not) appear by itself on a line. A final
line feed is chomped off, but all other characters are emitted verbatim.


=head2 The Field ^* for Variable Width One-line-at-a-time Text

Like "@*", this is a variable width field. The value supplied must be a
scalar variable. Perl puts the first line (up to the first "\n") of the
text into the field, and then chops off the front of the string so that
the next time the variable is referenced, more of the text can be printed.
The variable will I<not> be restored.

Example:
$text = "line 1\nline 2\nline 3";
format STDOUT =
Text: ^*
$text
~~ ^*
$text
.
Output:
Text: line 1
line 2
line 3


=head2 Specifying Values

The values are specified on the following format line in the same order as
the picture fields. The expressions providing the values must be
separated by commas. They are all evaluated in a list context
before the line is processed, so a single list expression could produce
multiple list elements. The expressions may be spread out to more than
one line if enclosed in braces. If so, the opening brace must be the first
token on the first line. If an expression evaluates to a number with a
decimal part, and if the corresponding picture specifies that the decimal
part should appear in the output (that is, any picture except multiple "#"
characters B<without> an embedded "."), the character used for the decimal
point is B<always> determined by the current LC_NUMERIC locale. This
means that, if, for example, the run-time environment happens to specify a
German locale, "," will be used instead of the default ".". See
L<perllocale> and L<"WARNINGS"> for more information.


=head2 Using Fill Mode

On text fields the caret enables a kind of fill mode. Instead of an
arbitrary expression, the value supplied must be a scalar variable
that contains a text string. Perl puts the next portion of the text into
the field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed. (Yes, this
means that the variable itself is altered during execution of the write()
call, and is not restored.) The next portion of text is determined by
a crude line breaking algorithm. You may use the carriage return character
(C<\r>) to force a line break. You can change which characters are legal
to break on by changing the variable C<$:> (that's
$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
list of the desired characters.

Normally you would use a sequence of fields in a vertical stack associated
with the same scalar variable to print out a block of text. You might wish
to end the final field with the text "...", which will appear in the output
if the text was too long to appear in its entirety.


=head2 Suppressing Lines Where All Fields Are Void

Using caret fields can produce lines where all fields are empty. You can
suppress such lines by putting a "~" (tilde) character anywhere in the
line. The tilde will be translated to a space upon output.


=head2 Repeating Format Lines

If you put two contiguous tilde characters "~~" anywhere into a line,
the line will be repeated until all the fields on the line are exhausted,
i.e. undefined. For special (caret) text fields this will occur sooner or
later, but if you use a text field of the at variety, the expression you
supply had better not give the same value every time forever! (C<shift(@f)>
is a simple example that would work.) Don't use a regular (at) numeric
field in such lines, because it will never go blank.

Example:
@w = qw( ape bee cod doe eel fly gnu hog );
format STDOUT =
@<<< @<<< @<<<~~
splice(@w,0,3)
.
Output:
ape bee cod
doe eel fly
gnu hog


=head2 Top of Form Processing

Top-of-form processing is by default handled by a format with the
same name as the current filehandle with "_TOP" concatenated to it.
It's triggered at the top of each page. See L<perlfunc/write>.

Examples:

[SNIP]

Within strings that are to be displayed in a fixed length text field,
each control character is substituted by a space. (But remember the
special meaning of C<\r> when using fill mode.) This is done to avoid
misalignment when control characters "disappear" on some output media.

-->8--

=head1 TESTS

Tests corresponding to all bugfixes were added. A range of tests
covers numeric fields, including overflow evidence.


=head1 CHANGED FILES

embed.fnc
toke.c
pp_ctl.c
form.h
pod/perlform.pod
pod/perldiag.pod
t/op/write.t


--- embed.fnc.old Tue Sep 30 19:09:51 2003
+++ embed.fnc Sun Jan 4 14:22:08 2004
@@ -1109,7 +1109,8 @@
s |void* |vdocatch_body |va_list args
#endif
s |OP* |dofindlabel |OP *o|char *label|OP **opstack|OP **oplimit
-s |void |doparseform |SV *sv
+s |OP* |doparseform |SV *sv
+s |bool |num_overflow |NV value, I32 fldsize, I32 frcsize
s |I32 |dopoptoeval |I32 startingblock
s |I32 |dopoptolabel |char *label
s |I32 |dopoptoloop |I32 startingblock
--- toke.c.old Tue Jan 6 11:36:51 2004
+++ toke.c Sun Jan 11 10:49:52 2004
@@ -7584,7 +7584,7 @@
bool eofmt = FALSE;

while (!needargs) {
- if (*s == '.' || *s == /*{*/'}') {
+ if (*s == '.') {
/*SUPPRESS 530*/
#ifdef PERL_STRICT_CR
for (t = s+1;SPACE_OR_TAB(*t); t++) ;
@@ -7597,7 +7597,7 @@
}
}
if (PL_in_eval && !PL_rsfp) {
- eol = strchr(s,'\n');
+ eol = memchr(s,'\n',PL_bufend-s);
if (!eol++)
eol = PL_bufend;
}
--- pp_ctl.c.old Tue Jan 6 11:19:44 2004
+++ pp_ctl.c Sun Jan 11 19:51:11 2004
@@ -343,15 +343,20 @@
bool item_is_utf8 = FALSE;
bool targ_is_utf8 = FALSE;
SV * nsv = Nullsv;
+ OP * parseres = 0;
+ char *fmt;
+ bool oneline;

if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
if (SvREADONLY(tmpForm)) {
SvREADONLY_off(tmpForm);
- doparseform(tmpForm);
+ parseres = doparseform(tmpForm);
SvREADONLY_on(tmpForm);
}
else
- doparseform(tmpForm);
+ parseres = doparseform(tmpForm);
+ if (parseres)
+ return parseres;
}
SvPV_force(PL_formtarget, len);
if (DO_UTF8(PL_formtarget))
@@ -387,6 +392,7 @@
case FF_LINEMARK: name = "LINEMARK"; break;
case FF_END: name = "END"; break;
case FF_0DECIMAL: name = "0DECIMAL"; break;
+ case FF_LINESNGL: name = "LINESNGL"; break;
}
if (arg >= 0)
PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
@@ -493,6 +499,7 @@
while (s < send) {
if (*s == '\r') {
itemsize = s - item;
+ chophere = s;
break;
}
if (*s++ & ~31)
@@ -532,6 +539,7 @@
while (s < send) {
if (*s == '\r') {
itemsize = s - item;
+ chophere = s;
break;
}
if (*s++ & ~31)
@@ -622,7 +630,7 @@
sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
for (; t < SvEND(PL_formtarget); t++) {
#ifdef EBCDIC
- int ch = *t++ = *s++;
+ int ch = *t;
if (iscntrl(ch))
#else
if (!(*t & ~31))
@@ -652,7 +660,13 @@
SvSETMAGIC(sv);
break;

+ case FF_LINESNGL:
+ chopspace = 0;
+ oneline = TRUE;
+ goto ff_line;
case FF_LINEGLOB:
+ oneline = FALSE;
+ ff_line:
item = s = SvPV(sv, len);
itemsize = len;
if ((item_is_utf8 = DO_UTF8(sv)))
@@ -661,20 +675,31 @@
bool chopped = FALSE;
gotsome = TRUE;
send = s + len;
+ chophere = s + itemsize;
while (s < send) {
if (*s++ == '\n') {
- if (s == send) {
- itemsize--;
+ if (oneline) {
chopped = TRUE;
+ chophere = s;
+ break;
+ } else {
+ if (s == send) {
+ itemsize--;
+ chopped = TRUE;
+ } else
+ lines++;
}
- else
- lines++;
}
}
SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
if (targ_is_utf8)
SvUTF8_on(PL_formtarget);
- sv_catsv(PL_formtarget, sv);
+ if (oneline) {
+ SvCUR_set(sv, chophere - item);
+ sv_catsv(PL_formtarget, sv);
+ SvCUR_set(sv, itemsize);
+ } else
+ sv_catsv(PL_formtarget, sv);
if (chopped)
SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) - 1);
SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
@@ -684,46 +709,24 @@
}
break;

+ case FF_0DECIMAL:
+ arg = *fpc++;
+#if defined(USE_LONG_DOUBLE)
+ fmt = (arg & 256) ? "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl;
+#else
+ fmt = (arg & 256) ? "%#0*.*f" : "%0*.*f";
+#endif
+ goto ff_dec;
case FF_DECIMAL:
- /* If the field is marked with ^ and the value is undefined,
- blank it out. */
arg = *fpc++;
- if ((arg & 512) && !SvOK(sv)) {
- arg = fieldsize;
- while (arg--)
- *t++ = ' ';
- break;
- }
- gotsome = TRUE;
- value = SvNV(sv);
- /* Formats aren't yet marked for locales, so assume "yes". */
- {
- STORE_NUMERIC_STANDARD_SET_LOCAL();
#if defined(USE_LONG_DOUBLE)
- if (arg & 256) {
- sprintf(t, "%#*.*" PERL_PRIfldbl,
- (int) fieldsize, (int) arg & 255, value);
- } else {
- sprintf(t, "%*.0" PERL_PRIfldbl, (int) fieldsize, value);
- }
+ fmt = (arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl;
#else
- if (arg & 256) {
- sprintf(t, "%#*.*f",
- (int) fieldsize, (int) arg & 255, value);
- } else {
- sprintf(t, "%*.0f",
- (int) fieldsize, value);
- }
+ fmt = (arg & 256) ? "%#*.*f" : "%*.*f";
#endif
- RESTORE_NUMERIC_STANDARD();
- }
- t += fieldsize;
- break;
-
- case FF_0DECIMAL:
+ ff_dec:
/* If the field is marked with ^ and the value is undefined,
blank it out. */
- arg = *fpc++;
if ((arg & 512) && !SvOK(sv)) {
arg = fieldsize;
while (arg--)
@@ -732,31 +735,22 @@
}
gotsome = TRUE;
value = SvNV(sv);
+ /* overflow evidence */
+ if (num_overflow(value, fieldsize, arg)) {
+ arg = fieldsize;
+ while (arg--)
+ *t++ = '#';
+ break;
+ }
/* Formats aren't yet marked for locales, so assume "yes". */
{
STORE_NUMERIC_STANDARD_SET_LOCAL();
-#if defined(USE_LONG_DOUBLE)
- if (arg & 256) {
- sprintf(t, "%#0*.*" PERL_PRIfldbl,
- (int) fieldsize, (int) arg & 255, value);
-/* is this legal? I don't have long doubles */
- } else {
- sprintf(t, "%0*.0" PERL_PRIfldbl, (int) fieldsize, value);
- }
-#else
- if (arg & 256) {
- sprintf(t, "%#0*.*f",
- (int) fieldsize, (int) arg & 255, value);
- } else {
- sprintf(t, "%0*.0f",
- (int) fieldsize, value);
- }
-#endif
+ sprintf(t, fmt, (int) fieldsize, (int) arg & 255, value);
RESTORE_NUMERIC_STANDARD();
}
t += fieldsize;
break;
-
+
case FF_NEWLINE:
f++;
while (t-- > linemark && *t == ' ') ;
@@ -3536,7 +3530,7 @@
RETURNOP(retop);
}

-STATIC void
+STATIC OP *
S_doparseform(pTHX_ SV *sv)
{
STRLEN len;
@@ -3552,14 +3546,15 @@
U32 *linepc = 0;
register I32 arg;
bool ischop;
- int maxops = 2; /* FF_LINEMARK + FF_END) */
+ bool unchopnum = FALSE;
+ int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */

if (len == 0)
Perl_croak(aTHX_ "Null picture in formline");

/* estimate the buffer size needed */
for (base = s; s <= send; s++) {
- if (*s == '\n' || *s == '\0' || *s == '@' || *s == '^')
+ if (*s == '\n' || *s == '@' || *s == '^')
maxops += 10;
}
s = base;
@@ -3592,8 +3587,12 @@
case ' ': case '\t':
skipspaces++;
continue;
-
- case '\n': case 0:
+ case 0:
+ if (s < send) {
+ skipspaces = 0;
+ continue;
+ } /* else FALL THROUGH */
+ case '\n':
arg = s - base;
skipspaces++;
arg -= skipspaces;
@@ -3649,8 +3648,12 @@
*fpc++ = FF_FETCH;
if (*s == '*') {
s++;
- *fpc++ = 0;
- *fpc++ = FF_LINEGLOB;
+ *fpc++ = 2; /* skip the @* or ^* */
+ if (ischop) {
+ *fpc++ = FF_LINESNGL;
+ *fpc++ = FF_CHOP;
+ } else
+ *fpc++ = FF_LINEGLOB;
}
else if (*s == '#' || (*s == '.' && s[1] == '#')) {
arg = ischop ? 512 : 0;
@@ -3668,6 +3671,7 @@
*fpc++ = s - base; /* fieldsize for FETCH */
*fpc++ = FF_DECIMAL;
*fpc++ = (U16)arg;
+ unchopnum |= ! ischop;
}
else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
arg = ischop ? 512 : 0;
@@ -3686,6 +3690,7 @@
*fpc++ = s - base; /* fieldsize for FETCH */
*fpc++ = FF_0DECIMAL;
*fpc++ = (U16)arg;
+ unchopnum |= ! ischop;
}
else {
I32 prespace = 0;
@@ -3740,6 +3745,38 @@
Safefree(fops);
sv_magic(sv, Nullsv, PERL_MAGIC_fm, Nullch, 0);
SvCOMPILED_on(sv);
+
+ if (unchopnum && repeat)
+ DIE(aTHX_ "Repeated format line will never terminate (~~ and @#)");
+ return 0;
+}
+
+
+STATIC bool
+S_num_overflow(NV value, I32 fldsize, I32 frcsize)
+{
+ /* Can value be printed in fldsize chars, using %*.*f ? */
+ NV pwr = 1;
+ NV eps = 0.5;
+ bool res = FALSE;
+ int intsize = fldsize - (value < 0 ? 1 : 0);
+
+ if (frcsize & 256)
+ intsize--;
+ frcsize &= 255;
+ intsize -= frcsize;
+
+ while (intsize--) pwr *= 10.0;
+ while (frcsize--) eps /= 10.0;
+
+ if( value >= 0 ){
+ if (value + eps >= pwr)
+ res = TRUE;
+ } else {
+ if (value - eps <= -pwr)
+ res = TRUE;
+ }
+ return res;
}

static I32
--- form.h.old Tue Jan 6 07:37:43 2004
+++ form.h Tue Jan 6 08:23:43 2004
@@ -24,3 +24,4 @@
#define FF_BLANK 14
#define FF_MORE 15
#define FF_0DECIMAL 16
+#define FF_LINESNGL 17
--- pod/perlform.pod.old Sat Jan 3 18:44:35 2004
+++ pod/perlform.pod Sun Jan 11 10:52:26 2004
@@ -29,8 +29,9 @@
FORMLIST
.

-If name is omitted, format "STDOUT" is defined. FORMLIST consists of
-a sequence of lines, each of which may be one of three types:
+If the name is omitted, format "STDOUT" is defined. A single "." in
+column 1 is used to terminate a format. FORMLIST consists of a sequence
+of lines, each of which may be one of three types:

=over 4

@@ -48,29 +49,106 @@

=back

-Picture lines are printed exactly as they look, except for certain fields
-that substitute values into the line. Each field in a picture line starts
-with either "@" (at) or "^" (caret). These lines do not undergo any kind
-of variable interpolation. The at field (not to be confused with the array
-marker @) is the normal kind of field; the other kind, caret fields, are used
-to do rudimentary multi-line text block filling. The length of the field
-is supplied by padding out the field with multiple "E<lt>", "E<gt>", or "|"
-characters to specify, respectively, left justification, right
-justification, or centering. If the variable would exceed the width
-specified, it is truncated.
-
-As an alternate form of right justification, you may also use "#"
-characters (with an optional ".") to specify a numeric field. This way
-you can line up the decimal points. With a "0" (zero) instead of the
-first "#", the formatted number will be padded with leading zeroes if
-necessary. If any value supplied for these fields contains a newline,
-only the text up to the newline is printed. Finally, the special field
-"@*" can be used for printing multi-line, nontruncated values; it
-should appear by itself on a line.
-
-The values are specified on the following line in the same order as
-the picture fields. The expressions providing the values should be
-separated by commas. The expressions are all evaluated in a list context
+Picture lines contain output field definitions, intermingled with
+literal text. These lines do not undergo any kind of variable interpolation.
+Field definitions are made up from a set of characters, for starting and
+extending a field to its desired width. This is the complete set of
+characters for field definitions:
+
+ @ start of regular field
+ ^ start of special field
+ < pad character for left adjustification
+ | pad character for centering
+ > pad character for right adjustificat
+ # pad character for a right justified numeric field
+ 0 instead of first #: pad number with leading zeroes
+ . decimal point within a numeric field
+ ... terminate a text field, show "..." as truncation evidence
+ @* variable width field for a multi-line value
+ ^* variable width field for next line of a multi-line value
+ ~ suppress line with all fields empty
+ ~~ repeat line until all fields are exhausted
+
+Each field in a picture line starts with either "@" (at) or "^" (caret),
+indicating what we'll call, respectively, a "regular" or "special" field.
+The choice of pad characters determines whether a field is textual or
+numeric. The tilde operators are not part of a field. Let's look at
+the various possibilities in detail.
+
+
+=head2 Text Fields
+
+The length of the field is supplied by padding out the field with multiple
+"E<lt>", "E<gt>", or "|" characters to specify a non-numeric field with,
+respectively, left justification, right justification, or centering.
+For a regular field, the value (up to the first newline) is taken and
+printed according to the selected justification, truncating excess characters.
+If you terminate a text field with "...", three dots will be shown if
+the value is truncated. A special text field may be used to do rudimentary
+multi-line text block filling; see L</Using Fill Mode> for details.
+
+ Example:
+ format STDOUT =
+ @<<<<<< @|||||| @>>>>>>
+ "left", "middle", "right"
+ .
+ Output:
+ left middle right
+
+
+=head2 Numeric Fields
+
+Using "#" as a padding character specifies a numeric field, with
+right justification. An optional "." defines the position of the
+decimal point. With a "0" (zero) instead of the first "#", the
+formatted number will be padded with leading zeroes if necessary.
+A special numeric field is blanked out if the value is undefined.
+If the resulting value would exceed the width specified the field is
+filled with "#" as overflow evidence.
+
+ Example:
+ format STDOUT =
+ @### @.### @##.### @### @### ^####
+ 42, 3.1415, undef, 0, 10000, undef
+ .
+ Output:
+ 42 3.142 0.000 0 ####
+
+
+=head2 The Field @* for Variable Width Multi-Line Text
+
+The field "@*" can be used for printing multi-line, nontruncated
+values; it should (but need not) appear by itself on a line. A final
+line feed is chomped off, but all other characters are emitted verbatim.
+
+
+=head2 The Field ^* for Variable Width One-line-at-a-time Text
+
+Like "@*", this is a variable width field. The value supplied must be a
+scalar variable. Perl puts the first line (up to the first "\n") of the
+text into the field, and then chops off the front of the string so that
+the next time the variable is referenced, more of the text can be printed.
+The variable will I<not> be restored.
+
+ Example:
+ $text = "line 1\nline 2\nline 3";
+ format STDOUT =
+ Text: ^*
+ $text
+ ~~ ^*
+ $text
+ .
+ Output:
+ Text: line 1
+ line 2
+ line 3
+
+
+=head2 Specifying Values
+
+The values are specified on the following format line in the same order as
+the picture fields. The expressions providing the values must be
+separated by commas. They are all evaluated in a list context
before the line is processed, so a single list expression could produce
multiple list elements. The expressions may be spread out to more than
one line if enclosed in braces. If so, the opening brace must be the first
@@ -83,29 +161,47 @@
German locale, "," will be used instead of the default ".". See
L<perllocale> and L<"WARNINGS"> for more information.

-Picture fields that begin with ^ rather than @ are treated specially.
-With a # field, the field is blanked out if the value is undefined. For
-other field types, the caret enables a kind of fill mode. Instead of an
-arbitrary expression, the value supplied must be a scalar variable name
-that contains a text string. Perl puts as much text as it can into the
-field, and then chops off the front of the string so that the next time
+
+=head2 Using Fill Mode
+
+On text fields the caret enables a kind of fill mode. Instead of an
+arbitrary expression, the value supplied must be a scalar variable
+that contains a text string. Perl puts the next portion of the text into
+the field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed. (Yes, this
means that the variable itself is altered during execution of the write()
-call, and is not returned.) Normally you would use a sequence of fields
-in a vertical stack to print out a block of text. You might wish to end
-the final field with the text "...", which will appear in the output if
-the text was too long to appear in its entirety. You can change which
-characters are legal to break on by changing the variable C<$:> (that's
-$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
+call, and is not restored.) The next portion of text is determined by
+a crude line breaking algorithm. You may use the carriage return character
+(C<\r>) to force a line break. You can change which characters are legal
+to break on by changing the variable C<$:> (that's
+$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
list of the desired characters.

-Using caret fields can produce variable length records. If the text
-to be formatted is short, you can suppress blank lines by putting a
-"~" (tilde) character anywhere in the line. The tilde will be translated
-to a space upon output. If you put a second tilde contiguous to the
-first, the line will be repeated until all the fields on the line are
-exhausted. (If you use a field of the at variety, the expression you
-supply had better not give the same value every time forever!)
+Normally you would use a sequence of fields in a vertical stack associated
+with the same scalar variable to print out a block of text. You might wish
+to end the final field with the text "...", which will appear in the output
+if the text was too long to appear in its entirety.
+
+
+=head2 Suppressing Lines Where All Fields Are Void
+
+Using caret fields can produce lines where all fields are blank. You can
+suppress such lines by putting a "~" (tilde) character anywhere in the
+line. The tilde will be translated to a space upon output.
+
+
+=head2 Repeating Format Lines
+
+If you put two contiguous tilde characters "~~" anywhere into a line,
+the line will be repeated until all the fields on the line are exhausted,
+i.e. undefined. For special (caret) text fields this will occur sooner or
+later, but if you use a text field of the at variety, the expression you
+supply had better not give the same value every time forever! (C<shift(@f)>
+is a simple example that would work.) Don't use a regular (at) numeric
+field in such lines, because it will never go blank.
+
+
+=head2 Top of Form Processing

Top-of-form processing is by default handled by a format with the
same name as the current filehandle with "_TOP" concatenated to it.
@@ -338,11 +434,8 @@
exist outside that block structure. See L<perllocale> for further
discussion of locale handling.

-Inside of an expression, the whitespace characters \n, \t and \f are
-considered to be equivalent to a single space. Thus, you could think
-of this filter being applied to each value in the format:
-
- $value =~ tr/\n\t\f/ /;
+Within strings that are to be displayed in a fixed length text field,
+each control character is substituted by a space. (But remember the
+special meaning of C<\r> when using fill mode.) This is done to avoid
+misalignment when control characters "disappear" on some output media.

-The remaining whitespace character, \r, forces the printing of a new
-line if allowed by the picture line.
--- pod/perldiag.pod.old Sat Oct 18 19:33:26 2003
+++ pod/perldiag.pod Sun Jan 4 09:43:54 2004
@@ -3204,6 +3204,12 @@
(P) A "can't happen" error, because safemalloc() should have caught it
earlier.

+=item Repeated format line will never terminate (~~ and @# incompatible)
+
+(F) Your format containes the ~~ repeat-until-blank sequence and a
+numeric field that will never go blank so that the repetition never
+terminates. You might use ^# instead. See L<perlform>.
+
=item Reversed %s= operator

(W syntax) You wrote your assignment operator backwards. The = must
--- t/op/write.t.old Tue Jan 6 11:31:35 2004
+++ t/op/write.t Sun Jan 11 10:41:57 2004
@@ -5,12 +5,59 @@
@INC = '../lib';
}

-print "1..50\n";
+#-- testing numeric fields in all variants (WL)
+
+sub swrite {
+ my $format = shift;
+ local $^A = ""; # don't litter, use a local bin
+ formline( $format, @_ );
+ return $^A;
+}
+
+my @NumTests = (
+ [ '@###', 0, 1, 9999.5, 9999.4999, -999.5, 1e100 ],
+ [ '@0##', 0, 1, 9999.5, -999.4999, -999.5, 1e100 ],
+ [ '^###', 0, undef ],
+ [ '^0##', 0, undef ],
+ [ '@###.', 0, 1, 9999.5, 9999.4999, -999.5 ],
+ [ '@##.##', 0, 1, 999.995, 999.99499, -100 ],
+ [ '@0#.##', 0, 1, 10, -0.0001 ],
+ );
+
+sub mkfmt($){
+ my $fmt = shift();
+ my $fieldwidth = length( $fmt );
+ my $leadzero = $fmt =~ /^.0/ ? "0" : "";
+ if( $fmt =~ /\.(#*)/ ){
+ my $fractwidth = length( $1 );
+ return "%#${leadzero}${fieldwidth}.${fractwidth}f"
+ } else {
+ return "%${leadzero}${fieldwidth}.0f"
+ }
+}
+
+my $num_tests = 0;
+for my $tref ( @NumTests ){
+ $num_tests += @$tref - 1;
+}
+#---------------------------------------------------------
+
+# number of tests in section 1
+my $bas_tests = 20;
+
+# number of tests in section 3
+my $hmb_tests = 36;
+
+printf "1..%d\n", $bas_tests + $num_tests + $hmb_tests;

my $CAT = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? 'type'
: ($^O eq 'MacOS') ? 'catenate'
: 'cat';

+############
+## Section 1
+############
+
format OUT =
the quick brown @<<
$fox
@@ -274,14 +321,17 @@

{
our $el;
- format STDOUT =
+ format OUT12 =
ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
$el
.
my %hash = (12 => 3);
+ open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
for $el (keys %hash) {
- write;
+ write(OUT12);
}
+ close OUT12 or die "Could not close: $!";
+ print `$CAT Op_write.tmp`;
}

{
@@ -300,22 +350,161 @@
print `$CAT Op_write.tmp`;
}

-{
- # Bug #24774 format without trailing \n failed assertion
- # but this must not compile because we'd get a ';' into the format
+{ # test 14
+ # Bug #24774 format without trailing \n failed assertion, but this
+ # must fail since we have a trailing ; in the eval'ed string (WL)
my @v = ('k');
eval "format OUT14 = \n@\n\@v";
print $@ ? "ok 14\n" : "not ok 14\n";
}

-#######################################
-# Easiest to add new tests above here #
+{ # test 15
+ # text lost in ^<<< field with \r in value (WL)
+ my $txt = "line 1\rline 2";
+ format OUT15 =
+^<<<<<<<<<<<<<<<<<<
+$txt
+^<<<<<<<<<<<<<<<<<<
+$txt
+.
+ open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
+ write(OUT15);
+ close OUT15 or die "Could not close: $!";
+ my $res = `$CAT Op_write.tmp`;
+ print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n";
+}
+
+{ # test 16: multiple use of a variable in same line with ^<
+ my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
+ format OUT16 =
+^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
+$txt, $txt
+^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
+$txt, $txt
+.
+ open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
+ write(OUT16);
+ close OUT16 or die "Could not close: $!";
+ my $res = `$CAT Op_write.tmp`;
+ print $res eq <<EOD ? "ok 16\n" : "not ok 16\n";
+this_is_block_1 this_is_block_2
+this_is_block_3 this_is_block_4
+EOD
+}
+
+{ # test 17: @* "should be on a line of its own", but it should work
+ # cleanly with literals before and after. (WL)
+
+ my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
+ format OUT17 =
+Here we go: @* That's all, folks!
+ $txt
+.
+ open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
+ write(OUT17);
+ close OUT17 or die "Could not close: $!";
+ my $res = `$CAT Op_write.tmp`;
+ chomp( $txt );
+ my $exp = <<EOD;
+Here we go: $txt That's all, folks!
+EOD
+ print $res eq $exp ? "ok 17\n" : "not ok 17\n";
+}
+
+{ # test 18: @# and ~~ would cause runaway format, but we now
+ # catch this while compiling (WL)
+
+ format OUT18 =
+@######## ~~
+10
+.
+ open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
+ eval { write(OUT18); };
+ print $@ ? "ok 18\n" : "not ok 18\n";
+ close OUT18 or die "Could not close: $!";
+}
+
+{ # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
+ my $v = 'gaga';
+ eval "format OUT19 = \n" .
+ '@<<<' . "\0\n" .
+ '$v' . "\n" .
+ '@<<<' . "\0\n" .
+ '$v' . "\n.\n";
+ open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
+ write(OUT19);
+ my $res = `$CAT Op_write.tmp`;
+ print $res eq <<EOD ? "ok 19\n" : "not ok 19\n";
+gaga\0
+gaga\0
+EOD
+}
+
+{ # test 20: hash accesses; single '}' must not terminate format '}' (WL)
+ my %h = ( xkey => 'xval', ykey => 'yval' );
+ format OUT20 =
+@>>>> @<<<< ~~
+each %h
+@>>>> @<<<<
+$h{xkey}, $h{ykey}
+@>>>> @<<<<
+{ $h{xkey}, $h{ykey}
+}
+}
+.
+ my $exp = '';
+ while( my( $k, $v ) = each( %h ) ){
+ $exp .= sprintf( "%5s %s\n", $k, $v );
+ }
+ $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
+ $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
+ $exp .= "}\n";
+ open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
+ write(OUT20);
+ my $res = `$CAT Op_write.tmp`;
+ print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n";
+
+EOD
+}
+
+
+#####################
+## Section 2
+## numeric formatting
+#####################
+
+my $nt = $bas_tests;
+for my $tref ( @NumTests ){
+ my $writefmt = shift( @$tref );
+ my $printfmt = mkfmt( $writefmt );
+ my $blank_when_undef = substr( $writefmt, 0, 1 ) eq '^';
+ for my $val ( @$tref ){
+ my $writeres = swrite( $writefmt, $val );
+ my $printres;
+ if( $blank_when_undef && ! defined($val) ){
+ $printres = ' ' x length( $writefmt );
+ } else {
+ $printres = sprintf( $printfmt, $val || 0 );
+ if( length($printres) > length( $writefmt ) ){
+ $printres = '#' x length( $writefmt );
+ }
+ }
+ $nt++;
+
+ print $printres eq $writeres ? "ok $nt\n" : "not ok $nt\n";
+ }
+}
+
+
+#####################################
+## Section 3
+## Easiest to add new tests above here
#######################################

-# 15..50: scary format testing from Merijn H. Brand
+# scary format testing from H.Merijn Brand

-my $test = 15;
-my $tests = 50;
+my $test = $bas_tests + $num_tests + 1;
+my $tests = $bas_tests + $num_tests + $hmb_tests;

if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
($^O eq 'os2' and not eval '$OS2::can_fork')) {

H.Merijn Brand

unread,
Jan 16, 2004, 4:32:21 AM1/16/04
to LAUN Wolfgang, Perl 5 Porters, rgarci...@free.fr
On Tue 13 Jan 2004 12:28, LAUN Wolfgang <wolfga...@alcatel.at> wrote:
> One bugfix added (according to a note from H.Merijn Brand):
> A single '}' doesn't terminate a format any more. I guess that's it. Well,
> maybe...
> Regards
> Wolfgang

One more small thing, for which I don't know if we should only document it, or
actually solve it:

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

my @list = qw( ape bear cat giraffe maroon black policlinic tea perl );
my $list = join ", " => @list;

format STDOUT =
^<<<<<<<<<<<<<<<<<<<<< ~~
join ", ", @list
.

write;
-->8---

a5:/tmp 103 > perl test.pl
Runaway format at test.pl line 11.
Exit 255
a5:/tmp 104 >

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

my @list = qw( ape bear cat giraffe maroon black policlinic tea perl );
my $list = join ", " => @list;

format STDOUT =
^<<<<<<<<<<<<<<<<<<<<< ~
join ", ", @list
.

write;
-->8---

a5:/tmp 104 > perl test.pl
ape, bear, cat,
a5:/tmp 105 >

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

my @list = qw( ape bear cat giraffe maroon black policlinic tea perl );
my $list = join ", " => @list;

format STDOUT =
^<<<<<<<<<<<<<<<<<<<<< ~
$list
.

write;
-->8---


a5:/tmp 105 > perl test.pl
ape, bear, cat,
a5:/tmp 106 >

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

my @list = qw( ape bear cat giraffe maroon black policlinic tea perl );
my $list = join ", " => @list;

format STDOUT =
^<<<<<<<<<<<<<<<<<<<<< ~~
$list
.

write;
-->8---

a5:/tmp 106 > perl test.pl
ape, bear, cat,
giraffe, maroon,
black, policlinic,
tea, perl
a5:/tmp 107 >


Obviously, formats are not capable of re-using dynamically generated strings
to exhaustion. I cannot modify a dynamic string, even though it is supported
from the syntax view of it


--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

my @list = qw( ape bear cat giraffe maroon black policlinic tea perl );
my $list = join ", " => @list;

format STDOUT =
^<<<<<<<<<<<<<<<<<<<<< ~~
"@{[join ', ' => @list]}"
.

write;
-->8---

a5:/tmp 107 > perl test.pl
Runaway format at test.pl line 11.
Exit 255
a5:/tmp 108 >


--
H.Merijn Brand Amsterdam Perl Mongers (http://amsterdam.pm.org/)
using perl-5.6.1, 5.8.0, & 5.9.x, and 806 on HP-UX 10.20 & 11.00, 11i,
AIX 4.3, SuSE 8.2, and Win2k. http://www.cmve.net/~merijn/
http://archives.develooper.com/daily...@perl.org/ per...@perl.org
send smoke reports to: smokers...@perl.org, QA: http://qa.perl.org

Laun Wolfgang

unread,
Jan 16, 2004, 5:09:27 AM1/16/04
to H.Merijn Brand, Perl 5 Porters, rgarci...@free.fr
> -----Original Message-----
> From: H.Merijn Brand [mailto:h.m....@hccnet.nl]
> Sent: Friday, January 16, 2004 10:32 AM
> To: LAUN Wolfgang
> Cc: Perl 5 Porters; rgarci...@free.fr
> Subject: Re: [PATCH] format/write (version 2)
>
> [snip]

> One more small thing, for which I don't know if we should only document it, or
> actually solve it:

I think it's documented, cf. perlform.pod (new version, but most of this text
is taken from the old version):

=head2 Using Fill Mode

On text fields the caret enables a kind of fill mode. Instead of an
arbitrary expression, the value supplied must be a scalar variable
that contains a text string. Perl puts the next portion of the text into
the field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed. (Yes, this
means that the variable itself is altered during execution of the write()
call, and is not restored.)

I don't think you can "solve" that in a way that doesn't break clever
(ab)uses. Actually, "...must be a scalar variable..." isn't quite true.
Consider:

--8<---
$ cat try.pl
my $mail = "foo bar blech hum ho ding dong";
sub quotemail {
$mail = "> " . $mail if length $mail;
return \$mail;
}

format STDOUT =
^<<<<<<<<<<<<<< ~~

${quotemail()}
.
write;
$ perl try.pl
> foo bar blech
> hum ho ding
> dong
$
-->8---

Wolfgang


> [snip]
> --8<--- test.pl


> my @list = qw( ape bear cat giraffe maroon black policlinic
> tea perl );
> my $list = join ", " => @list;
>
> format STDOUT =
> ^<<<<<<<<<<<<<<<<<<<<< ~~
> join ", ", @list
> .
>
> write;
> -->8---
>
> a5:/tmp 103 > perl test.pl
> Runaway format at test.pl line 11.
> Exit 255
> a5:/tmp 104 >
>

> [snip]

H.Merijn Brand

unread,
Jan 16, 2004, 5:16:24 AM1/16/04
to LAUN Wolfgang, Perl 5 Porters, rgarci...@free.fr
On Fri 16 Jan 2004 11:09, LAUN Wolfgang <wolfga...@alcatel.at> wrote:
> > -----Original Message-----
> > From: H.Merijn Brand [mailto:h.m....@hccnet.nl]
> > Sent: Friday, January 16, 2004 10:32 AM
> > To: LAUN Wolfgang
> > Cc: Perl 5 Porters; rgarci...@free.fr
> > Subject: Re: [PATCH] format/write (version 2)
> >
> > [snip]
> > One more small thing, for which I don't know if we should only document it, or
> > actually solve it:
>
> I think it's documented, cf. perlform.pod (new version, but most of this text
> is taken from the old version):
>
> =head2 Using Fill Mode
>
> On text fields the caret enables a kind of fill mode. Instead of an
> arbitrary expression, the value supplied must be a scalar variable

I never interpreted this as a _declared_ scalar variable, but always as "must
be of _type_ scalar, meaning that it could also be the return code of a sub or
a dynamic generated string.

Why can't it be a temporary mortal?

--

Laun Wolfgang

unread,
Jan 16, 2004, 5:57:36 AM1/16/04
to H.Merijn Brand, Perl 5 Porters, rgarci...@free.fr

Given this exemplum malum:

my @text = (
"Quamvis sint sub aqua, sub aqua maledicere temptant",
"Sosehr sie unter Wasser sind, versuchen sie doch zu schimpfen"
);
my $i = 1;

format STDOUT =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~

$text[$i=1-$i]
.
write;
----------------
Quamvis sint sub aqua, sub aqua
Sosehr sie unter Wasser sind,
maledicere temptant
versuchen sie doch zu schimpfen

what would you have put in that temporary mortal?
-W

H.Merijn Brand

unread,
Jan 16, 2004, 6:08:52 AM1/16/04
to LAUN Wolfgang, Perl 5 Porters, rgarci...@free.fr
On Fri 16 Jan 2004 11:57, LAUN Wolfgang <wolfga...@alcatel.at> wrote:
> > Why can't it be a temporary mortal?
> >
>
> Given this exemplum malum:
>
> my @text = (
> "Quamvis sint sub aqua, sub aqua maledicere temptant",
> "Sosehr sie unter Wasser sind, versuchen sie doch zu schimpfen"
> );
> my $i = 1;
>
> format STDOUT =
> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
> $text[$i=1-$i]
> .
> write;
> ----------------
> Quamvis sint sub aqua, sub aqua
> Sosehr sie unter Wasser sind,
> maledicere temptant
> versuchen sie doch zu schimpfen
>
> what would you have put in that temporary mortal?

You _are_ evil :)

FWIW I would have expected - given the temp mortal would have worked -

$text[0]
thus

Quamvis sint sub aqua, sub aqua
maledicere temptant

because that is what the value is afer the fist pass to format, and that is
what it should work with

Based on the - maybe evenly malicious - example below:

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

my @text = (


"Quamvis sint sub aqua, sub aqua maledicere temptant",
"Sosehr sie unter Wasser sind, versuchen sie doch zu schimpfen"
);
my $i = 1;

my $xxx;

format XXX =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
$xxx
.

sub xxx ($)
{
local $~ = "XXX";
$xxx = shift;
write;
"";
} # xxx

format STDOUT =
@* ~
"@{[xxx $text[$i = 1 - $i]]}"
.
write;
-->8---

a5:/tmp 105 > perl test.pl


Quamvis sint sub aqua, sub aqua
maledicere temptant

a5:/tmp 106 >


> -W

Dave Mitchell

unread,
Jan 16, 2004, 8:56:48 AM1/16/04
to LAUN Wolfgang, perl5-...@perl.org
On Tue, Jan 13, 2004 at 12:28:31PM +0100, LAUN Wolfgang wrote:
> One bugfix added (according to a note from H.Merijn Brand): A single '}' doesn't terminate a format any more. I guess that's it. Well, maybe...
> Regards
> This patch eliminates a few bugs in format/write and adds a couple of
> new features. It's against 5.8.2, with patches #22005 and #22055

Thanks, applied as change 22161, with the following slight provisos:

The entry in embed.fnc for num_overflow was wrong: the args need to be
separated with |'s, not commas. Also, I added the 'n' flag to the entry,
since the function doesn't delare (nor use) an implicit interpreter arg
(otherwise the build faies on threaded Perls).

If possible, could you supply any future patches against bleedperl rather
than 5.8.x, thanks. (I want integrating between bleed and maint to be
Nick's neadache, not mine :-)

Finally, I backed out the following change, since it seems to have started
failing again:

---------------------------------------------------------------------------
Change 22075 by rgs@sixop on 2004/01/06 07:56:25

This TODO test seems to pass now.

Affected files ...

... //depot/perl/t/op/write.t#33 edit

Differences ...

==== //depot/perl/t/op/write.t#33 (xtext) ====

@@ -351,7 +351,7 @@
? "ok $test\n" : "not ok $test # TODO \$- = $- instead of 9\n";
$test++;
print $^ ne "Comment_TOP"
- ? "ok $test\n" : "not ok $test # TODO \$^ = $^ instead of 'STDOUT_TOP'\n";
+ ? "ok $test\n" : "not ok $test\n# \$^ = $^ instead of 'STDOUT_TOP'\n";
$test++;
}
---------------------------------------------------------------------------

Dave.

--
The optimist believes that he lives in the best of all possible worlds.
As does the pessimist.

Rafael Garcia-Suarez

unread,
Jan 16, 2004, 8:59:37 AM1/16/04
to Dave Mitchell, wolfga...@alcatel.at, perl5-...@perl.org
Dave Mitchell wrote:
> Finally, I backed out the following change, since it seems to have started
> failing again:

Thanks -- I commited this mostly to see what was going to happen.
However the original test had a bug : '# TODO' isn't printed for the 'ok'
result, so t/harness doesn't report it as "having unexpectedly succeeded."

Laun Wolfgang

unread,
Jan 16, 2004, 9:26:16 AM1/16/04
to Dave Mitchell, perl5-...@perl.org
> -----Original Message-----
> From: Dave Mitchell [mailto:da...@fdisolutions.com]
> Sent: Friday, January 16, 2004 2:57 PM
> To: LAUN Wolfgang
> Cc: 'perl5-...@perl.org'
> Subject: Re: [PATCH] format/write (version 2)
>
>
> On Tue, Jan 13, 2004 at 12:28:31PM +0100, LAUN Wolfgang wrote:
> > One bugfix added (according to a note from H.Merijn Brand): A single '}'
> > doesn't terminate a format any more. I guess that's it. Well, maybe...
> > Regards
> > This patch eliminates a few bugs in format/write and adds a couple of
> > new features. It's against 5.8.2, with patches #22005 and #22055
>
> Thanks, applied as change 22161, with the following slight provisos:
>
> The entry in embed.fnc for num_overflow was wrong: the args need to be
> separated with |'s, not commas. Also, I added the 'n' flag to the entry,
> since the function doesn't delare (nor use) an implicit interpreter arg
> (otherwise the build faies on threaded Perls).
>
> If possible, could you supply any future patches against bleedperl rather
> than 5.8.x, thanks. (I want integrating between bleed and maint to be
> Nick's neadache, not mine :-)

Sorry. You could have told me to fix it myself, though. (I'll rework the patch
intended to fix [perl #24898] which is against 5.8.2.)

Wolfgang

Dave Mitchell

unread,
Jan 16, 2004, 12:07:42 PM1/16/04
to Rafael Garcia-Suarez, wolfga...@alcatel.at, perl5-...@perl.org, h.m....@hccnet.nl

I've now fixed the code (and the test!) as Change 22162. The test
starting working beause someone happened to have added an earlier test
that writes to the STDOUT format, and it stopped working again after tests
were moved around. I've now stopped the test from relying on the state of
STDOUT

In the second TODO test shown above,

? "ok $test\n" : "not ok $test # TODO \$- = $- instead of 9\n";

I'm not so sure that it is a bug. it was reported by Merijn as bug #8698,
but judging by the code, $- is explicitly set to 100000000 when there is
no corresponding foo_TOP format in pp_leavewrite:

#!/usr/bin/perl -w

$test = 1;

format STDOUT =
ok @<<<<<
$test
.
write;
print "stdout: \$-=",$-, "\n";

$ ./perl /tmp/p
ok 1
stdout: $-=99999999
$


--
In the 70's we wore flares because we didn't know any better.
What possible excuse does the current generation have?

H.Merijn Brand

unread,
Jan 16, 2004, 12:13:46 PM1/16/04
to Dave Mitchell, Perl 5 Porters
On Fri 16 Jan 2004 18:07, Dave Mitchell <da...@fdisolutions.com> wrote:
> On Fri, Jan 16, 2004 at 02:59:37PM +0100, Rafael Garcia-Suarez wrote:
> I've now fixed the code (and the test!) as Change 22162. The test
> starting working beause someone happened to have added an earlier test
> that writes to the STDOUT format, and it stopped working again after tests
> were moved around. I've now stopped the test from relying on the state of
> STDOUT

Good work!

> In the second TODO test shown above,
>
> ? "ok $test\n" : "not ok $test # TODO \$- = $- instead of 9\n";
>
> I'm not so sure that it is a bug. it was reported by Merijn as bug #8698,
> but judging by the code, $- is explicitly set to 100000000 when there is
> no corresponding foo_TOP format in pp_leavewrite:

That would be an arguable decision. Why does

format FOO =
.

_need_ a

format FOO_TOP =
.

???

> #!/usr/bin/perl -w
>
> $test = 1;
>
> format STDOUT =
> ok @<<<<<
> $test
> .
> write;
> print "stdout: \$-=",$-, "\n";
>
> $ ./perl /tmp/p
> ok 1
> stdout: $-=99999999

documented is that $= defaults to 64, which means that if I have printed only
1 line to STDOUT, $- should be 63, and not 99999999, having STDOUT_TOP or not

> $

Wolfgang Laun

unread,
Jan 16, 2004, 12:44:29 PM1/16/04
to Dave Mitchell, perl5-...@perl.org, h.m....@hccnet.nl
Dave Mitchell wrote:
> I've now fixed the code (and the test!) as Change 22162. The test
> starting working beause someone happened to have added an earlier test
> that writes to the STDOUT format, and it stopped working again after tests
> were moved around. I've now stopped the test from relying on the state of
> STDOUT
>
> In the second TODO test shown above,
>
> ? "ok $test\n" : "not ok $test # TODO \$- = $- instead of 9\n";
>
> I'm not so sure that it is a bug. it was reported by Merijn as bug #8698,
> but judging by the code, $- is explicitly set to 100000000 when there is
> no corresponding foo_TOP format in pp_leavewrite:
>

FWIW, I think that it depends what one thinks a format/write page ought
to be when there is no header (i.e *_TOP format) defined. Does it mean
you have just one page of a zillion lines?

On the other hand, you can simply define an empty *_TOP format and the
line counter will behave nicely.

format STDOUT =
@###
$-
.
format STDOUT_TOP =
.
$= = 5;
write for (1..100);

prints
0
4
3
2
1
0
4
3
and so on.

Wolfgang

0 new messages