Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: interface.pm explained

by fergal (Chaplain)
on Aug 02, 2004 at 12:36 UTC ( [id://379265]=note: print w/replies, xml ) Need Help??


in reply to interface.pm explained

Interesting hack but it could actually have been implemented with import and INIT
package interface; my %classes; sub import { my $callerpackage = caller; $classes{$callerpackage} = [@_]; } INIT { while (my $class, $info) = each %classes) { check_a_class($class, $info); } }

To me that seems much cleaner than locks and evals. Especially as the real module forgets to check $@ to see if the code being eval'd died or not.

Replies are listed 'Best First'.
Re^2: interface.pm explained
by gaal (Parson) on Aug 02, 2004 at 12:49 UTC
    Hmmm, I'm not sure about this; did you test it? (I'm at work now so I can't check myself.)

    When would interface.pm's INIT block run? Will it not happen when *it* finishes compilation? In that case, your approach wouldn't work: it's still too early, happening before the calling class finishes loading.

    Also, it's not too dangerous ignoring $@ in the real module. If there are errors, surely they'll pop up again when flow returns to the caler. (No?)

      I didn't test it but INIT kicks in when everything is finished compiling, perlsub -
      "INIT" blocks are run just before the Perl runtime begins execution, in "first in, first out" (FIFO) order. For example, the code generators documented in perlcc make use of "INIT" blocks to initialize and resolve pointers to XSUBs.

      So various modules can register INIT blocks. The INIT blocks are all postponed until just before perl starts executing, at which point it runs all the INIT blocks in the order they were registered and then finally it goes to line 1 of your program.

      $@ is the only place where an error from an eval will show up, if you don't do something about it, the error will disappear forever, that's what makes it possible to ignore an error with eval. Of course if there is something wrong with the module and we ignore the error, the program will probably fail anyway however it will fail with a confusing error message. For example if mod.pm is

      package mod; sub foo{ print "hello world\n; # eek! missing a double quote } 1;
      and we do
      eval "use mod"; mod::foo();
      we get
      Undefined subroutine &mod::foo called at - line 2.
      because foo didn't compile however what we really want is
      eval "use mod"; die $@ if $@; mod::foo();
      Can't find string terminator '"' anywhere before EOF at mod.pm line 3.
      Compilation failed in require at (eval 1) line 2.
      BEGIN failed--compilation aborted at (eval 1) line 2.
      

        You'll want to name the code you called inside INIT/CHECK because mod_perl doesn't call those. This gives mod_perl-using code the ability to manually call your INIT-time code at the appropriate time.

        INIT { init(); } sub init { # INIT-time code goes here. mod_perl users are expected to call th +is function as appropriate for them. # perl will not automatically trigger this function for them. ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://379265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found