From ed-pm at s5h.net Tue Nov 13 14:05:43 2007 From: ed-pm at s5h.net (ed) Date: Tue, 13 Nov 2007 22:05:43 +0000 Subject: [Thamesvalley-pm] perl subroutine refs Message-ID: <20071113220543.62b81bcd@workstation> Hi, I'm trying to do something to compare scriptable plugin methods between php and perl, but I have a problem in that I cannot figure out how to do it with strict: #!/usr/bin/perl use warnings; use strict; my %subs; sub test { print( "Tested\n" ); } my $l = "test"; $subs{'plugname'} = &$l; $subs{'plugname'}; The error message insists that this is not possible while using strict refs, but I'd like to be able to continue using everything that strict offers, is there a 'safe' way to do this? -- The T1 to Radvision is A.F.U. because of a glitch. RedHat is watching IP theater. :: http://www.s5h.net/ :: http://www.s5h.net/gpg -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.pm.org/pipermail/thamesvalley-pm/attachments/20071113/335e5218/attachment.bin From stephenca at ls26.net Tue Nov 13 14:19:36 2007 From: stephenca at ls26.net (Stephen Cardie) Date: Tue, 13 Nov 2007 22:19:36 +0000 Subject: [Thamesvalley-pm] perl subroutine refs In-Reply-To: <20071113220543.62b81bcd@workstation> References: <20071113220543.62b81bcd@workstation> Message-ID: <473A22F8.1060808@ls26.net> ed wrote: > Hi, > > I'm trying to do something to compare scriptable plugin methods between > php and perl, but I have a problem in that I cannot figure out how to > do it with strict: > > #!/usr/bin/perl > > use warnings; > use strict; > > my %subs; > > sub test { > print( "Tested\n" ); > } > > my $l = "test"; > > $subs{'plugname'} = &$l; > $subs{'plugname'}; > > The error message insists that this is not possible while using strict > refs, but I'd like to be able to continue using everything that strict > offers, is there a 'safe' way to do this? > ed, This won't work under strict() because you are attempting to create a symbolic reference (see perldoc perlref). If you really, really want to do this, you need no strict qw(refs); But you don't want to do that ... One "safe" way to do this is with a despatch table; basically, a hash of anonymous subs. my(%despatch) = ( test => sub { print "Tested\n" }, ); # later ... my(%subs); $subs{plugname} = 'test'; $despatch{$subs{plugname}}->(); # ... __END__ Cheers, Steve From abw at wardley.org Tue Nov 13 23:36:21 2007 From: abw at wardley.org (Andy Wardley) Date: Wed, 14 Nov 2007 07:36:21 +0000 Subject: [Thamesvalley-pm] perl subroutine refs In-Reply-To: <20071113220543.62b81bcd@workstation> References: <20071113220543.62b81bcd@workstation> Message-ID: <473AA575.30800@wardley.org> ed wrote: > $subs{'plugname'} = &$l; $subs{'plugname'} = \&test; HTH A