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

Re: Dual-life perl 5-or-7 code and prototypes - impossible?

Thread Previous | Thread Next
From:
Salvador Fandiño
Date:
June 29, 2020 16:40
Subject:
Re: Dual-life perl 5-or-7 code and prototypes - impossible?
Message ID:
2593beb3-dfb3-3b4d-7c32-475ab8901493@gmail.com
On 29/6/20 17:22, Paul "LeoNerd" Evans wrote:
 > On Sat, 27 Jun 2020 19:29:42 +0100
 > ilmari@ilmari.org (Dagfinn Ilmari Mannsåker) wrote:
 >
 >> The B::CompilerPhase::Hook module lets you programmatically enqueue
 >> phasers: https://metacpan.org/pod/B::CompilerPhase::Hook.
 >>
 >> Sticking the above code in a Sub::Attribute::Prototype::import() and
 >> replacing UNITCHECK { ... } with enqueue_UNICHECK(sub { ...}) works.
 >
 > Ahyes :)
 >
 > So armed with this knowledge I have now created
 >
 >    https://metacpan.org/pod/Sub::Attribute::Prototype
 >
 >> Hoewver, another issue is that because the prototype isn't set until
 >> UNITCHECK time, it will not apply to calls in the same unit as the
 >> prototyped sub is defined in, so this is really only useful for
 >> modules that export subs, not scripts.
 >
 > Ahyes, an annoying caveat. That and the other two are noted in the docs:
 >
 >    https://metacpan.org/pod/Sub::Attribute::Prototype#Caveats
 >
 > Because of these limitations, I'm probably not even going to convert my
 > own modules (e.g. List::UtilsBy) into using this one yet. I'd welcome
 > anyone trying to help improve the situation and get rid of these
 > problems, but currently it seems I shall have to leave List::UtilsBy in
 > a 7-unsafe state.
 >

Aren't you too focused in supporting the new subroutine attribute in 
older perls instead of just providing its functionality in some way?

For instance, it shouldn't be to difficult to write a package, say for 
instance, "sub::prototype", that sets the function prototype and works 
both under p5 and p7.

   sub foo {
      ...
   }
   use sub::prototype foo => "$";

   sub bar {
      ...
   }
   use sub::prototype bar => "\@"


And in sub/prototype.pm

   ...
   sub import {
     my ($pkg, $sub_name, $proto) = @_;
     my $sub = get_reference_to_sub_in_some_way($sub_name);
     Sub::Util::set_prototype($sub, $proto);
   }

Thread Previous | Thread Next


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