Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Most learned monks,

I'm currently working on my port of autodie (a lexical pragma for do-or-die semantics) to Perl 5.8. I've got a rather clever trick that allows me to change how perl resolves subroutines, but I'm starting to hit problems with its scalability.

The trick looks like this:

use strict; open(my $fh, '<', $0); # Calls CORE::open BEGIN { *{main::open} = sub { print "Hi\n"; }; use subs 'open'; } open(my $fh2, '<', $0); # Prints Hi BEGIN { delete $main::{open}; } BEGIN { *{main::open} = sub { print "Ho\n"; }; use subs 'open'; } open(my $fh4, '<', $0); # Prints Ho! BEGIN { delete $main::{open}; } open(my $fh5, '<', $0); # Calls CORE::open __END__ Hi Ho

The code above works wonderfully, and maps very well to what I'm doing in autodie when I'm either inserting a subroutine, or deleting a subroutine.

Where it fails is when I'm trying to replace a subroutine that's already there. The reason is that each use of autodie maps to a single BEGIN block, and merging the two middle BEGIN blocks causes the trick to fail:

use strict; open(my $fh, '<', $0); # Calls CORE::open BEGIN { *{main::open} = sub { print "Hi\n"; }; use subs 'open'; } open(my $fh2, '<', $0); # Prints Hi BEGIN { # Deleting and remaking the sub fails! ;( delete $main::{open}; *{main::open} = sub { print "Ho\n"; }; use subs 'open'; } open(my $fh4, '<', $0); # Prints Ho! BEGIN { delete $main::{open}; } open(my $fh5, '<', $0); # Calls CORE::open __END__ Ambiguous use of *{main::open} resolved to *main::open at examples/bla line 17. Subroutine main::open redefined at examples/ line 17. Ho

Unfortunately for me, this means that any operation that needs to replace a subroutine fails, since that involves a delete and an insert. Note that just trying to replace the sub without the magic delete trick doesn't help (Perl never resolves to the first sub declared).

Nesting the BEGIN blocks works fine, but isn't useful for my purposes, since I'm playing with subroutine resolution at every import(), and a BEGIN inside my import() will only run once.

So, does anyone have a trick, that works on Perl 5.8, which allows a subroutine to be replaced with limited scope (as per my first example), but involves only a single BEGIN to do that replacement? This isn't something which comes up every day. ;)

For those interested in learning more about autodie, I've been blogging about it, as well as having made a lightning talk about the idea. The code on CPAN currently works for all my core criteria, but it's the edges cases that are giving me the biggest headaches.

Many many thanks in advance for all your help,

In reply to Using delete package::{subroutine} to change resolution of subroutines by pjf

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-06-23 20:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.