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

Re: Isn't Perl5 ready for C<my sub>?

by merlyn (Sage)
on Sep 07, 2005 at 14:55 UTC ( [id://489882]=note: print w/replies, xml ) Need Help??


in reply to Isn't Perl5 ready for C<my sub>?

sub invert { my ($n, $p)=@_; # Trusting $p to be a prime... local *expp = sub { my ($n, $m)=@_; return 1 unless $m; ($m%2 ? $n : 1) * expp($n, $m >> 1)**2 % $p; }; expp($n, $p-2); }
(With all the caveats of local and symboltable hacking, of course.)

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Isn't Perl5 ready for C<my sub>?
by friedo (Prior) on Sep 07, 2005 at 16:48 UTC
    (With all the caveats of local and symboltable hacking, of course.)

    The main problem I see with this approach is that it doesn't behave the way we would expect a true lexical sub too. For example, I would expect the following to work:

    sub foo { my sub bar { print "42\n"; } return sub { bar() }; } my $s = foo(); $s->();

    Of course that won't work with the local trick since you can't have a lexical closure over a locally modified symbol table (as far as I know.) But it would work fine if you used my $bar = sub { print "42\n" }.

    I suppose a source filter to convert my sub bar { } to my $bar = sub { } would do the trick, but then you'd have to scan the rest of the block to change bar to $bar->(). Sounds like a real PITA.

Re^2: Isn't Perl5 ready for C<my sub>?
by blazar (Canon) on Sep 07, 2005 at 15:26 UTC
    (With all the caveats of local and symboltable hacking, of course.)
    Indeed. In fact this is a hack. Granted, a nice hack, but as I wrote, not that it is impossible to work around the lack of lexically scoped named subs, it's possible and even easy. We still simply lack them and I would feel comfortable having them instead. I would just be interested to know whether there's some major technical difficulty in this sense...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found