You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@perl.apache.org by "Philippe M. Chiasson" <go...@cpan.org> on 2003/06/19 07:06:13 UTC

[mp2 Patch] ModPerl::Util::undef(coderef)

Following the discussion on Apache::Reload and Apache::Symbol, I've
taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
into ModPerl::Util::undef for mod_perl 2.

Works wonders when you want to undefine subroutines, constant or not,
with or without prototypes. Very usefull for Apache::Reload (can get rid
of that $SIG{__WARN__} trap).

# $Id: ModPerl-Util-undef.patch,v 1.1 2003/06/19 05:02:53 gozer Exp $

Index: t/response/TestModperl/util.pm
===================================================================
RCS file: t/response/TestModperl/util.pm
diff -N t/response/TestModperl/util.pm
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ t/response/TestModperl/util.pm	19 Jun 2003 05:01:32 -0000
@@ -0,0 +1,83 @@
+package TestModperl::util;
+                                                                                                                                                     
+use strict;
+use warnings FATAL => 'all';
+                                                                                                                                                     
+use Apache::Test;
+use Apache::TestUtil;
+                                                                                                                                                     
+use Apache::Const -compile => 'OK';
+
+use ModPerl::Util;
+
+sub ModPerlUtilTestConst { 42 }
+sub ModPerlUtilTest { return $_[0] * 2 }
+
+sub handler {
+    my $r = shift;
+                                                                                                                                                     
+    plan $r, tests => 14;
+    
+    ok &ModPerlUtilTestConst == 42;
+    
+    ModPerl::Util::undef(\&ModPerlUtilTestConst);
+    
+    ok ! eval { 
+        &ModPerlUtilTestConst == 42;
+    };
+    
+    ok $@;
+    
+    eval 'sub ModPerlUtilTestConst { 84 }';
+    
+    ok !$@;
+    
+    ok &ModPerlUtilTestConst == 84;
+    
+    ok ModPerlUtilTest(42) == 84;
+    
+    ModPerl::Util::undef(\&ModPerlUtilTest);
+     
+    ok ! eval { 
+        &ModPerlUtilTest(42) == 84;
+    };
+    
+    ok $@;
+    
+    eval 'sub ModPerlUtilTest { return $_[0] / 2 }';
+    
+    ok !$@;
+    
+    ok ModPerlUtilTest(84) == 42;
+    
+    {
+        my $warning;
+
+        local $SIG{__WARN__} = sub { $warning = shift; };
+
+        undef $warning;
+        eval {
+            ModPerl::Util::undef({ foo => 'bar'});
+        };
+        
+        ok $warning;
+
+        undef $warning;
+        eval {
+            ModPerl::Util::undef("foo");
+        };
+        
+        ok $warning;
+        
+        undef $warning;
+        eval {
+            ModPerl::Util::undef(sub { "foo"; });
+        };
+        
+        ok !$warning;
+    }
+    
+    Apache::OK;
+}
+
+1;
Index: xs/ModPerl/Util/ModPerl__Util.h
===================================================================
RCS file: /home/cvs/modperl-2.0/xs/ModPerl/Util/ModPerl__Util.h,v
retrieving revision 1.4
diff -u -I$Id -r1.4 ModPerl__Util.h
--- xs/ModPerl/Util/ModPerl__Util.h	17 Feb 2003 09:03:17 -0000	1.4
+++ xs/ModPerl/Util/ModPerl__Util.h	19 Jun 2003 05:01:33 -0000
@@ -14,4 +14,44 @@
 #define mpxs_Apache_current_callback modperl_callback_current_callback_get
 
 
+static MP_INLINE void mpxs_ModPerl__Util_undef(pTHX_ SV *ref)
+{
+    GV *gv;
+    SV *sv;
+    CV *cv;
+    I32 has_proto=FALSE;
+
+    if (SvROK(ref)) {
+        sv = SvRV(ref);
+    }
+    else {
+        warn("undef called without a reference!");
+        return;
+    }
+
+    switch (SvTYPE(sv)) {
+        case SVt_PVCV:
+        cv = (CV*)sv;
+        if (!CvXSUB(cv) && CvROOT(cv) && CvDEPTH(cv)) {
+            return;         /* subroutine is active */
+        }
+
+        gv = (GV*)SvREFCNT_inc(CvGV(cv));
+        if(SvPOK(cv)) {
+            has_proto = TRUE;
+        }
+
+        cv_undef(cv);
+        CvGV(cv) = gv;   /* let user-undef'd sub keep its identity */
+        if(has_proto) {
+            SvPOK_on(cv); /* otherwise we get `Prototype mismatch:' */
+        }
+
+        break;
+ 
+    default:
+        warn("undef called without a CODE reference!\n");
+    }
+}
+
 
Index: xs/maps/modperl_functions.map
===================================================================
RCS file: /home/cvs/modperl-2.0/xs/maps/modperl_functions.map,v
retrieving revision 1.57
diff -u -I$Id -r1.57 modperl_functions.map
--- xs/maps/modperl_functions.map	30 May 2003 12:55:14 -0000	1.57
+++ xs/maps/modperl_functions.map	19 Jun 2003 05:01:33 -0000
@@ -2,6 +2,7 @@
 
 MODULE=ModPerl::Util
  mpxs_ModPerl__Util_untaint | | ...
+ mpxs_ModPerl__Util_undef
  DEFINE_exit | | int:status=0
 
 PACKAGE=Apache
Index: xs/tables/current/ModPerl/FunctionTable.pm
===================================================================
RCS file: /home/cvs/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
retrieving revision 1.116
diff -u -I$Id -r1.116 FunctionTable.pm
--- xs/tables/current/ModPerl/FunctionTable.pm	4 Jun 2003 16:50:38 -0000	1.116
+++ xs/tables/current/ModPerl/FunctionTable.pm	19 Jun 2003 05:01:33 -0000
@@ -3675,6 +3675,20 @@
       },
     ],
   },
+    {
+    'return_type' => 'void',
+    'name' => 'mpxs_ModPerl__Util_undef',
+    'args' => [
+      {
+        'type' => 'PerlInterpreter *',
+        'name' => 'my_perl'
+      },
+      {
+        'type' => 'SV *',
+        'name' => 'ref'
+      },
+    ],
+  },
   {
     'return_type' => 'HE *',
     'name' => 'modperl_perl_hv_fetch_he',


-- 
--------------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:
> Following the discussion on Apache::Reload and Apache::Symbol, I've
> taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
> into ModPerl::Util::undef for mod_perl 2.
> 
> Works wonders when you want to undefine subroutines, constant or not,
> with or without prototypes. Very usefull for Apache::Reload (can get rid
> of that $SIG{__WARN__} trap).

Philippe, can we put some fresh air into this patch. I have spent quite a lot 
of time, fighting with flush_namespace in perlrun (see my recent 
ModPerl-Registry tests patch) and I'm not quite happy about the way things are 
with the perl API to undef things. Next I'd like to try your stuff in C. Next 
we can finish off the Apache::Reload thread:
http://marc.theaimsgroup.com/?t=105545915700001&r=1&w=2

The original thread I'm replying to (from Jul 2003) is here:
http://marc.theaimsgroup.com/?t=105599929900002&r=1&w=2

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Thu, 2003-06-19 at 15:50, Stas Bekman wrote:
> Philippe M. Chiasson wrote:
> > On Thu, 2003-06-19 at 14:21, Stas Bekman wrote:
> > 
> >>Philippe M. Chiasson wrote:
> >>
> >>>Following the discussion on Apache::Reload and Apache::Symbol, I've
> >>>taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
> >>>into ModPerl::Util::undef for mod_perl 2.
> >>>
> >>>Works wonders when you want to undefine subroutines, constant or not,
> >>>with or without prototypes. Very usefull for Apache::Reload (can get rid
> >>>of that $SIG{__WARN__} trap).
> >>
> >>As suggested in the other thread, ModPerl::Util::undef is not the best choice 
> >>for the name. Me thinking either call ModPerl::Util::symbol_undef or have 
> >>further subclassing: ModPerl::Util::Symbol::undef
> > 
> > 
> > How about like I said before ModPerl::Util::undef_code since it works
> > only on CODE refs?
> 
> but we might have others as well, so a more generic name is better, no? 
> symbol_undef_code? just in case?

Sure, after all, it's just a name ;-) I am fine with calling it
symbol_undef_code. 

s/undef/symbol_undef_code/ && commit ?

> >>Otherwise +1, but again please first ask Doug why hasn't he ported this code 
> >>in first place.
> > 
> > 
> > Will do ;-)
> > 
> > 
> >>__________________________________________________________________
> >>Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> >>http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> >>mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> >>http://modperlbook.org http://apache.org   http://ticketmaster.com
-- 
--------------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:
> On Thu, 2003-06-19 at 14:21, Stas Bekman wrote:
> 
>>Philippe M. Chiasson wrote:
>>
>>>Following the discussion on Apache::Reload and Apache::Symbol, I've
>>>taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
>>>into ModPerl::Util::undef for mod_perl 2.
>>>
>>>Works wonders when you want to undefine subroutines, constant or not,
>>>with or without prototypes. Very usefull for Apache::Reload (can get rid
>>>of that $SIG{__WARN__} trap).
>>
>>As suggested in the other thread, ModPerl::Util::undef is not the best choice 
>>for the name. Me thinking either call ModPerl::Util::symbol_undef or have 
>>further subclassing: ModPerl::Util::Symbol::undef
> 
> 
> How about like I said before ModPerl::Util::undef_code since it works
> only on CODE refs?

but we might have others as well, so a more generic name is better, no? 
symbol_undef_code? just in case?

>>Otherwise +1, but again please first ask Doug why hasn't he ported this code 
>>in first place.
> 
> 
> Will do ;-)
> 
> 
>>__________________________________________________________________
>>Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
>>http://stason.org/     mod_perl Guide ---> http://perl.apache.org
>>mailto:stas@stason.org http://use.perl.org http://apacheweek.com
>>http://modperlbook.org http://apache.org   http://ticketmaster.com


-- 


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Thu, 2003-06-19 at 14:21, Stas Bekman wrote:
> Philippe M. Chiasson wrote:
> > Following the discussion on Apache::Reload and Apache::Symbol, I've
> > taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
> > into ModPerl::Util::undef for mod_perl 2.
> > 
> > Works wonders when you want to undefine subroutines, constant or not,
> > with or without prototypes. Very usefull for Apache::Reload (can get rid
> > of that $SIG{__WARN__} trap).
> 
> As suggested in the other thread, ModPerl::Util::undef is not the best choice 
> for the name. Me thinking either call ModPerl::Util::symbol_undef or have 
> further subclassing: ModPerl::Util::Symbol::undef

How about like I said before ModPerl::Util::undef_code since it works
only on CODE refs?

> Otherwise +1, but again please first ask Doug why hasn't he ported this code 
> in first place.

Will do ;-)

> __________________________________________________________________
> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org   http://ticketmaster.com
-- 
--------------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:
> Following the discussion on Apache::Reload and Apache::Symbol, I've
> taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
> into ModPerl::Util::undef for mod_perl 2.
> 
> Works wonders when you want to undefine subroutines, constant or not,
> with or without prototypes. Very usefull for Apache::Reload (can get rid
> of that $SIG{__WARN__} trap).

As suggested in the other thread, ModPerl::Util::undef is not the best choice 
for the name. Me thinking either call ModPerl::Util::symbol_undef or have 
further subclassing: ModPerl::Util::Symbol::undef

Otherwise +1, but again please first ask Doug why hasn't he ported this code 
in first place.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Thu, 2003-06-19 at 19:31, Geoffrey Young wrote:
> Philippe M. Chiasson wrote:
> > Following the discussion on Apache::Reload and Apache::Symbol, I've
> > taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
> > into ModPerl::Util::undef for mod_perl 2.
> 
> is this really required anymore?
> 
> Apache::Symbol::undef is there explicitly to keep our valid redefines from 
> throwing mandatory errors, but the mandatory error behavior was removed by 
> 5.6 - it's now covered separately through the warnings pragma and can be 
> turned off via $^W=0.
> 
> see
> 
> http://marc.theaimsgroup.com/?l=apache-modperl&m=96599355325073&w=2
> 
> I was only able to find a snippet of Doug's original comments wrt 2.0 - it 
> seems that none of the dev@ archives are complete that far back.  but see 
> the very bottom of
> 
> http://mathforum.org/epigone/modperl-dev/chalcrungwex/A14A9ADE3BEAD311AF95009027B6FBE02B864E@corpex.laserlink.net
> 
> for his quoted thoughts.

Well, that's true expect for what Stas uncovered already. Constant
subroutines will generate a _mandatory_ warning when redefined, even
with no warnings and bleedperl ;-(

#> perl -le 'no warnings; $^W=0; use constant PI => 3.14; undef &PI; sub PI() { }'
Constant subroutine PI redefined at -e line 1.

So I guess we _still_ could make use of such functionnality

> HTH
> 
> --Geoff
> 
> 
-- 
--------------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'


Re: [mp2 Patch] ModPerl::Util::undef(coderef)

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Philippe M. Chiasson wrote:
> Following the discussion on Apache::Reload and Apache::Symbol, I've
> taken out Apache::Symbol::undef from mp1, massaged it a bit and made it
> into ModPerl::Util::undef for mod_perl 2.

is this really required anymore?

Apache::Symbol::undef is there explicitly to keep our valid redefines from 
throwing mandatory errors, but the mandatory error behavior was removed by 
5.6 - it's now covered separately through the warnings pragma and can be 
turned off via $^W=0.

see

http://marc.theaimsgroup.com/?l=apache-modperl&m=96599355325073&w=2

I was only able to find a snippet of Doug's original comments wrt 2.0 - it 
seems that none of the dev@ archives are complete that far back.  but see 
the very bottom of

http://mathforum.org/epigone/modperl-dev/chalcrungwex/A14A9ADE3BEAD311AF95009027B6FBE02B864E@corpex.laserlink.net

for his quoted thoughts.

HTH

--Geoff




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org