Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Inline subs?

by broquaint (Abbot)
on May 02, 2003 at 01:12 UTC ( [id://254865]=note: print w/replies, xml ) Need Help??


in reply to Inline subs?

Dominus' Memoize is probably going to be the way to go, but here's a source filter implementation anyways
package Inline::Subs; use strict; use Regexp::Common; use Filter::Simple; my $re = qr< sub ( [_a-zA-Z](?:[\w_]+) ) \s* : \s* inline \s* ( $RE{balanced}{-parens => '{}' ) >x; FILTER_ONLY code => sub { while(my($subname, $code) = /$re/) { s/$re// and s/\b$subname\b/$code/g; } } ; q[ and the rest is silence ];
Brutish I know, but hopefully any subs declared sub funcname : inline will be inlined into the code. If not, that code (and the regexes from the Sub::Lexical source) should give you a head start :)
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Inline subs?
by Limbic~Region (Chancellor) on May 02, 2003 at 01:19 UTC
    broquaint,
    Memoize was also mentioned in the CB, but would not help for my case:

    Do not memoize a function whose behavior depends on program state other than its own arguments, such as global variables, the time of day, or file input. These functions will not produce correct results when memoized.

    My millions of iterations were from file input. I will check your source filter out tomorrow to see if I can grok it - Thanks!

    L~R

Re2: Inline subs?
by dragonchild (Archbishop) on May 02, 2003 at 13:36 UTC
    Why doesn't this already exist?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Why doesn't this already exist?
      Because it's not as simple as it looks e.g
      sub foo : inline { my %args = @_; ... return $something; }
      This would require not just parsing out the sub but also the any munging of @_ and any exitting of the sub (e.g return, last etc). While I guess this could be done with a bit of op tree trickery or XS, a simple filter would only be suitable for dropping literal blocks of code (which would also have to comply with strictures). Having said that I probably will make a module, but with many, many disclaimers ;)
      HTH

      _________
      broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-16 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found