Skip to content

Commit e7c5525

Browse files
dougmChip Salzenberg
authored and
Chip Salzenberg
committed
Support PRINTF for tied handles
A mod_perl user just asked why "print ..." is sent to the browser but "printf ..." goes to the term window. Sorry this is coming in late, this question has been asked a few times in the past, but I forgot about it :-( p5p-msgid: 199704202226.SAA08032@postman.osf.org
1 parent 10a676f commit e7c5525

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

pod/perldelta.pod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,19 @@ the print function.
490490
return print join( $, => map {uc} @_), $\;
491491
}
492492

493+
=item PRINTF this, LIST
494+
495+
This method will be triggered every time the tied handle is printed to
496+
with the C<printf()> function.
497+
Beyond its self reference it also expects the format and list that was
498+
passed to the printf function.
499+
500+
sub PRINTF {
501+
shift;
502+
my $fmt = shift;
503+
print sprintf($fmt, @_)."\n";
504+
}
505+
493506
=item READ this LIST
494507

495508
This method will be called when the handle is read from via the C<read>

pod/perltie.pod

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ use the each() function to iterate over such. Example:
611611
This is partially implemented now.
612612

613613
A class implementing a tied filehandle should define the following
614-
methods: TIEHANDLE, at least one of PRINT, READLINE, GETC, or READ,
614+
methods: TIEHANDLE, at least one of PRINT, PRINTF, READLINE, GETC, or READ,
615615
and possibly DESTROY.
616616

617617
It is especially useful when perl is embedded in some other program,
@@ -634,12 +634,26 @@ hold some internal information.
634634

635635
=item PRINT this, LIST
636636

637-
This method will be triggered every time the tied handle is printed to.
637+
This method will be triggered every time the tied handle is printed to
638+
with the C<print()> function.
638639
Beyond its self reference it also expects the list that was passed to
639640
the print function.
640641

641642
sub PRINT { $r = shift; $$r++; print join($,,map(uc($_),@_)),$\ }
642643

644+
=item PRINTF this, LIST
645+
646+
This method will be triggered every time the tied handle is printed to
647+
with the C<printf()> function.
648+
Beyond its self reference it also expects the format and list that was
649+
passed to the printf function.
650+
651+
sub PRINTF {
652+
shift;
653+
my $fmt = shift;
654+
print sprintf($fmt, @_)."\n";
655+
}
656+
643657
=item READ this LIST
644658

645659
This method will be called when the handle is read from via the C<read>
@@ -832,4 +846,4 @@ source code to MLDBM.
832846

833847
Tom Christiansen
834848

835-
TIEHANDLE by Sven Verdoolaege <F<skimo@dns.ufsia.ac.be>>
849+
TIEHANDLE by Sven Verdoolaege <F<skimo@dns.ufsia.ac.be>> and Doug MacEachern <F<dougm@osf.org>>

pp_sys.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,33 @@ PP(pp_prtf)
10721072
IO *io;
10731073
PerlIO *fp;
10741074
SV *sv = NEWSV(0,0);
1075+
MAGIC *mg;
10751076

10761077
if (op->op_flags & OPf_STACKED)
10771078
gv = (GV*)*++MARK;
10781079
else
10791080
gv = defoutgv;
1081+
1082+
if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
1083+
if (MARK == ORIGMARK) {
1084+
EXTEND(SP, 1);
1085+
++MARK;
1086+
Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1087+
++SP;
1088+
}
1089+
PUSHMARK(MARK - 1);
1090+
*MARK = mg->mg_obj;
1091+
PUTBACK;
1092+
ENTER;
1093+
perl_call_method("PRINTF", G_SCALAR);
1094+
LEAVE;
1095+
SPAGAIN;
1096+
MARK = ORIGMARK + 1;
1097+
*MARK = *SP;
1098+
SP = MARK;
1099+
RETURN;
1100+
}
1101+
10801102
if (!(io = GvIO(gv))) {
10811103
if (dowarn) {
10821104
gv_fullname3(sv, gv, Nullch);

t/op/misc.t

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ BEGIN failed--compilation aborted at - line 1.
196196
shift;
197197
print join(' ', reverse @_)."\n";
198198
}
199+
sub PRINTF {
200+
shift;
201+
my $fmt = shift;
202+
print sprintf($fmt, @_)."\n";
203+
}
199204
sub TIEHANDLE {
200205
bless {}, shift;
201206
}
@@ -226,12 +231,14 @@ BEGIN failed--compilation aborted at - line 1.
226231
$len = 10; $offset = 1;
227232
read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
228233
getc(FOO) eq "a" or die "foo->GETC failed";
234+
printf "%s is number %d\n", "Perl", 1;
229235
}
230236
EXPECT
231237
This is a reversed sentence.
232238
-- Out of inspiration --
233239
foo->can(READ)(string 10 1)
234240
Don't GETC, Get Perl
241+
Perl is number 1
235242
and destroyed as well
236243
########
237244
my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"

0 commit comments

Comments
 (0)