Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1958272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:17:26+00:00 2026-05-17T08:17:26+00:00

I’d like to inspect and manipulate code of arbitrary Perl procedures (got by coderefs)

  • 0

I’d like to inspect and manipulate code of arbitrary Perl procedures (got by coderefs) in Perl. Is there a tool/module/library for that? Something similar to B::Concise, except that B::Concise prints the code on output, but I’d like to inspect it programmatically.

I’d like to use it like this. Given a coderef F, which is called eg. with 10 arguments:

$ret = &$F(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10);

I’d like to create a function F1, st.

&$F(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) == 
  &$F1(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)*
  &$C(x2, x3, x4, x5, x6, x7, x8, x9, x10)

that is to “factor” it into two parts, where the second doesn’t depend on x1 and the first is as simple as possible (I assume F is constructed as a huge product).

The application I want this for is optimization of Metropolis sampling algorithm – suppose I’m sampling the distribution p(x1 | x2 = X1, x3 = X3, ...) = f(x1, x2, x3, ...). The algorithm itself is invariant wrt. multiplicative constant factors, and other variables do not change through the algorithms, so the part not depending on x1 (ie. $c from above) need not be evaluated at all).

The joint probability might have eg. the following form:

  p(x1, x2, x3, x4, x5) = g1(x1, x2)*g2(x2, x3)*g3(x3, x4)*g4(x4, x5)*g5(x4, x1)*g6(x5, x1)

I also consider constructing p as an object consisting of the factors with annotations of which variables does a particular factor depend on. Even this would benefit from code introspection (determining the variables automatically).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T08:17:26+00:00Added an answer on May 17, 2026 at 8:17 am

    For introspection of optrees the B family of modules is usually used.

    Given an code reference $cv, first create a B object for that:

    my $b_cv = B::svref_2object($cv);
    

    Now you can call the various methods documented in B on that to retrieve various things from the optree.

    Using only optree introspection you can already achieve amazing things. See DBIx::Perlish for a pretty advanced example of this.

    There also happens to be a B::Generate module, that allows building new optrees that do whatever you want, or to manipulate existing optrees. However, B::Generate isn’t as mature as one would hope, and there’s a lot of missing features and quite a few bugs.

    Actual optree creation and manipulation is usually best done using perl’s C api, as documented in perlapi, perlguts, and perlhack, among others. You’ll probably have to learn some XS as well, to expose the optree manipulation functions you wrote back to perl space, but that’s the easy part really.

    Building optrees (not necessarily based on other existing optrees that are being introspected) seems to have become somewhat popular recently, especially since Syntax Plugins have been added to the core in perl 5.12.0. You can find various examples like Scope::Escape::Sugar on cpan.

    However, dealing with perl’s optrees is still somewhat fiddly and not exactly beginner-friendly. It shouldn’t be necessary for any of the most arcane things. Something like using B::Deparse->new->coderef2text($cv) and then maybe mangling very slightly with the evaluated source code is really as far as I would want to go with optree introspection from pure-perl space.

    You might want to step back a bit and explain the actual problem you’re trying to solve. Maybe there’s a much simpler solution that doesn’t involve messing with optrees at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
For some reason, after submitting a string like this Jack’s Spindle from a text
I have text I am displaying in SIlverlight that is coming from a CMS
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.