Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by blazar (Canon)
on Sep 07, 2005 at 14:48 UTC ( #489881=perlquestion: print w/ replies, xml ) Need Help??
blazar has asked for the wisdom of the Perl Monks concerning the following question:

I would like to premise that I'm completely ignorant of current perls' internals. However in informal discussions people with much more experience and knowledge about it told me that the lexical pad is not much different from a common package. Now I wonder if it has a code slot too, or if one could easily be added if not, so as to allow for lexically scoped named subs too:
my sub foo { # ... }
Now, I'm sure some one will mention that I can do
my $foo = sub { ... };
instead. And yes, of course I knew, but even if we don't feel an extreme need for lexically scoped named subs it wouldn't be too bad to have them either. Maybe they may go into 5.10, couldn't they?

Thus we may have something like this:

sub invert { my ($n, $p)=@_; # Trusting $p to be a prime... my sub expp { my ($n, $m)=@_; return 1 unless $m; ($m%2 ? $n : 1) * expp($n, $m >> 1)**2 % $p; } expp($n, $p-2); }
This is of course an oversimplified and probably not extremely appropriate example, but I hope it gives an idea...

Comment on Isn't Perl5 ready for C<my sub>?
Select or Download Code
Re: Isn't Perl5 ready for C<my sub>?
by merlyn (Sage) on Sep 07, 2005 at 14:55 UTC
    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.

      (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...
      (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: Isn't Perl5 ready for C<my sub>?
by dave_the_m (Parson) on Sep 07, 2005 at 16:00 UTC
    The perl5 parser has had a placeholder for lexical subs for quite a while:
    $ perl -we 'my sub foo {}' "my sub" not yet implemented at -e line 1. $
    and a pad can quite happily accomodate them.
    The main things holding them up has been trying to agree their semantics, then someone - probably me - finding the time to implement them :-(.

    Dave.

      Let me guess... since in Perl5 an assignment to the declaration of a lexical variable can't contain the same lexical variable in the rhs (well, poor phrasing, but no doubt it's clear what I mean!) there's not yet full consensus on what should the name of such a sub refer to within its own body. Got it?
        well, that's one thing. Then there's things like: does the sub get re-created on each scope entry (so it captures external lexical vars anew) and does it affect method resolution?

        Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://489881]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-12-29 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls