Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPC 0022 Metaprogramming API #25

Merged
merged 11 commits into from
Dec 7, 2023
Prev Previous commit
Next Next commit
No need for separate get_variable / get_subroutine; just have a singl…
…e for_reference()
  • Loading branch information
leonerd committed Aug 30, 2023
commit c10ed8b59849a63da3fad10982b2f689ff0fb5ba
18 changes: 5 additions & 13 deletions ppcs/ppc0022-metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,17 @@ Returns a list of sub-packages within the given base package name, each as a met

Some less common functions that may still be useful in some situations. These functions can be used to obtain meta-programming object instances in ways other than walking the symbol table for given names.

#### `get_variable`
#### `for_reference`

```perl
$metavar = meta::get_variable($varref);
$metavar = meta::for_reference($ref);
```

Given a reference to a package variable, returns a meta-variable object instance to represent it. If `$varref` is not a SCALAR, ARRAY or HASH reference, an exception is thrown.
Given a reference to a package variable or subroutine, returns a meta-object instance to represent it. If `$varref` is not a SCALAR, ARRAY, HASH or CODE reference, an exception is thrown.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You allow get_symbol to return a GLOB, but don't allow a globref to be passed in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahyes; might as well allow GLOBrefs too. Fixed in 387d25b


*Note:* It is currently unspecified what happens if the reference refers to a lexical or anonymous variable, rather than a package one. This PPC does not specify any useful behaviour for such instances, but it's possible that future abilities may be added that become useful on lexical variables or anonymous containers.

#### `get_subroutine`
Anonymous subroutines should be supported; at least in order to obtain a meta-object to represent them so that methods like `set_subname` can be called on them.

```perl
$metasub = meta::get_subroutine($subref);
```

Given a CODE reference, returns a meta-subroutine object instance to represent it. If `$subref` is not a CODE reference, an exception is thrown.

This is primarily useful for working with newly-constructed anonymous functions, in order to give them a valid name by calling `set_subname` for instance.
*Note:* It is currently unspecified what happens if the reference refers to a lexical or anonymous variable, rather than a package one. This PPC does not specify any useful behaviour for such instances, but it's possible that future abilities may be added that become useful on lexical variables or anonymous containers.

### Methods on Meta-Package Objects

Expand Down