develooper Front page | perl.perl5.porters | Postings from June 2016

[perl #128450] __SUB__ in format behaves inconsistently

From:
Father Chrysostomos
Date:
June 22, 2016 01:49
Subject:
[perl #128450] __SUB__ in format behaves inconsistently
Message ID:
rt-4.0.18-29241-1466560140-1897.128450-75-0@perl.org
# New Ticket Created by  Father Chrysostomos 
# Please include the string:  [perl #128450]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=128450 >


The behaviour of __SUB__ inside a format is an accident of the implementation.

As ‘one’-liners, for copy-and-paste:

$ ./perl -Ilib -Eformat\ = -e'@<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<' -e'eval q<__SUB__//"undef", *STDOUT{FORMAT}>' -e. -e'write for 1..5'
undef                   FORMAT(0x7fc18102ae40)
undef                   FORMAT(0x7fc18102ae40)
undef                   FORMAT(0x7fc18102ae40)
undef                   FORMAT(0x7fc18102ae40)
undef                   FORMAT(0x7fc18102ae40)
 ./perl -Ilib -Eformat\ = -e'@<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<' -e'__SUB__//"undef", *STDOUT{FORMAT}' -e. -e'write for 1..5'
FORMAT(0x7f93ec02ae40)  FORMAT(0x7f93ec02ae40)
FORMAT(0x7f93ec02ae40)  FORMAT(0x7f93ec02ae40)
FORMAT(0x7f93ec02ae40)  FORMAT(0x7f93ec02ae40)
FORMAT(0x7f93ec02ae40)  FORMAT(0x7f93ec02ae40)
FORMAT(0x7f93ec02ae40)  FORMAT(0x7f93ec02ae40)

More readable versions:

use v5.16;
format =
@<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<
eval q<__SUB__//"undef", *STDOUT{FORMAT}>
.
write for 1..5
__END__

use v5.16;
format =
@<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<
__SUB__//"undef", *STDOUT{FORMAT}
.
write for 1..5
__END__

Notice that whether we have a string eval inside the format determines the output of __SUB__.  Notice also that when __SUB__ is defined it is the same as the format originally invoked, even though the currently running format is a clone of it (though that is perhaps an implementation detail that should not be leaked).

What *should* __SUB__ produce inside a format?  Maybe it should always just be undef.

(For those interested, the difference in behaviour has to do with the optimisation of the runcv op to a constant at compile time.  Though now I don’t remember why string eval needs to affect that optimisation.  I may have copied and pasted the logic from elsewhere.)

-- 

Father Chrysostomos




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About