| 1 |
NAME |
| 2 |
|
| 3 |
meta - meta-programming API |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
use v5.14; |
| 8 |
use meta; |
| 9 |
|
| 10 |
my $metapkg = meta::get_package( "MyApp::Some::Package" ); |
| 11 |
|
| 12 |
$metapkg->add_symbol( |
| 13 |
'&a_function' => sub { say "New function was created" } |
| 14 |
); |
| 15 |
|
| 16 |
MyApp::Some::Package::a_function(); |
| 17 |
|
| 18 |
DESCRIPTION |
| 19 |
|
| 20 |
This package provides an API for metaprogramming; that is, allowing |
| 21 |
code to inspect or manipulate parts of its own program structure. Parts |
| 22 |
of the perl interpreter itself can be accessed by means of |
| 23 |
"meta"-objects provided by this package. Methods on these objects allow |
| 24 |
inspection of details, as well as creating new items or removing |
| 25 |
existing ones. |
| 26 |
|
| 27 |
The intention of this API is to provide a nicer replacement for |
| 28 |
existing tricks such as no strict 'refs' and using globrefs, and also |
| 29 |
to be a more consistent place to add new abilities, such as more APIs |
| 30 |
for inspection and alteration of internal structures, metaprogramming |
| 31 |
around the new 'class' feature, and other such uses. |
| 32 |
|
| 33 |
This module should be considered experimental; no API stability |
| 34 |
guarantees are made at this time. Behaviour may be added, altered, or |
| 35 |
removed in later versions. Once a workable API shape has been found, it |
| 36 |
is hoped that this module will eventually become dual-life and shipped |
| 37 |
as part of Perl core, as the implementation for PPC 0022. See the link |
| 38 |
in the "SEE ALSO" section. |
| 39 |
|
| 40 |
This module attempts to find a balance between accurately representing |
| 41 |
low-level concepts within the current implementation of the Perl |
| 42 |
interpreter, while also providing higher-level abstractions that |
| 43 |
provide useful behaviour for code that uses it. One place this can be |
| 44 |
seen is the lower-level "list_globs" method, which directly maps to the |
| 45 |
way that GVs are stored in symbol table stashes but requires the user |
| 46 |
to be aware of the GV-less optimisised storage of CVs, as compared to |
| 47 |
the higher-level "list_symbols" method which provides an abstraction |
| 48 |
over this complication and presents the more useful but less accurate |
| 49 |
impression of separately named symbols that neatly map to their values. |
| 50 |
|
| 51 |
Since version 0.003_002 all the entry-point functions and constructors |
| 52 |
in this module will provoke warnings in the meta::experimental |
| 53 |
category. They can be silenced by |
| 54 |
|
| 55 |
use meta; |
| 56 |
no warnings 'meta::experimental'; |
| 57 |
|
| 58 |
Since version 0.005 the various can_...-prefixed variant accessor |
| 59 |
methods print deprecation warnings. They are likely to be removed soon. |
| 60 |
|
| 61 |
FUNCTIONS |
| 62 |
|
| 63 |
get_package |
| 64 |
|
| 65 |
$metapkg = meta::get_package( $pkgname ); |
| 66 |
|
| 67 |
Returns a metapackage reference representing the given package name, |
| 68 |
creating it if it did not previously exist. |
| 69 |
|
| 70 |
An alternative to meta::package->get in a plain function style. |
| 71 |
|
| 72 |
get_this_package |
| 73 |
|
| 74 |
$metapkg = meta::get_this_package; |
| 75 |
|
| 76 |
Since version 0.002. |
| 77 |
|
| 78 |
Returns a metapackage reference representing the package of the code |
| 79 |
that called the function. |
| 80 |
|
| 81 |
Useful for performing meta-programming on the contents of a module |
| 82 |
during its BEGIN or loading time. Equivalent to but more efficient than |
| 83 |
the following: |
| 84 |
|
| 85 |
meta::get_package(__PACKAGE__) |
| 86 |
|
| 87 |
for_reference |
| 88 |
|
| 89 |
$metasym = meta::for_reference( $ref ); |
| 90 |
|
| 91 |
Since version 0.007. |
| 92 |
|
| 93 |
Returns a metasymbol reference representing the glob, variable or |
| 94 |
subroutine that is pointed to by the given reference. |
| 95 |
|
| 96 |
Note that passing in a reference to a symbol table hash ("stash") does |
| 97 |
not result in a metapackage. For that you will have to call |
| 98 |
"get_package" or similar. |
| 99 |
|
| 100 |
METHODS ON meta::package |
| 101 |
|
| 102 |
get |
| 103 |
|
| 104 |
$metapkg = meta::package->get( $pkgname ); |
| 105 |
|
| 106 |
Since version 0.003_001. |
| 107 |
|
| 108 |
Returns a metapackage reference representing the given package name, |
| 109 |
creating it if it did not previously exist. |
| 110 |
|
| 111 |
An alternative to meta::get_package in an object constructor style. |
| 112 |
|
| 113 |
is_class |
| 114 |
|
| 115 |
$bool = $metapkg->is_class; |
| 116 |
|
| 117 |
Since version 0.009. |
| 118 |
|
| 119 |
Returns true if on a version of Perl that supports use feature 'class', |
| 120 |
and the package being represented is a real class created by that |
| 121 |
feature. False for regular packages, and always false on earlier |
| 122 |
versions of Perl before that feature was introduced. |
| 123 |
|
| 124 |
name |
| 125 |
|
| 126 |
$name = $metapkg->name; |
| 127 |
|
| 128 |
Returns the name of the package being represented. |
| 129 |
|
| 130 |
get_glob |
| 131 |
|
| 132 |
$metaglob = $metapkg->get_glob( $name ); |
| 133 |
|
| 134 |
Returns a metaglob reference representing the given symbol name within |
| 135 |
the package, if it exists. Throws an exception if not. |
| 136 |
|
| 137 |
try_get_glob, can_glob |
| 138 |
|
| 139 |
$metaglob = $metapkg->try_get_glob( $name ); |
| 140 |
$metaglob = $metapkg->can_glob( $name ); |
| 141 |
|
| 142 |
Similar to "get_glob" but returns undef if the glob does not exist. |
| 143 |
|
| 144 |
list_globs |
| 145 |
|
| 146 |
@metaglobs = $metapkg->list_globs; |
| 147 |
|
| 148 |
Since version 0.005. |
| 149 |
|
| 150 |
Returns a list of all the globs in the package that may refer to |
| 151 |
symbols (i.e. not subpackages). They are returned in no particular |
| 152 |
order. |
| 153 |
|
| 154 |
For a more convenient return value form, see also "list_symbols". |
| 155 |
|
| 156 |
list_subpackage_globs |
| 157 |
|
| 158 |
list_all_globs |
| 159 |
|
| 160 |
@metaglobs = $metapkg->list_subpackage_globs; |
| 161 |
@metaglobs = $metapkg->list_all_globs; |
| 162 |
|
| 163 |
Since version 0.005. |
| 164 |
|
| 165 |
Returns a list of all the globs in the package that refer to |
| 166 |
subpackages, or all globs, including subpackages. They are returned in |
| 167 |
no particular order. |
| 168 |
|
| 169 |
For a more convenient return value form, see also "list_subpackages". |
| 170 |
|
| 171 |
get_symbol |
| 172 |
|
| 173 |
$metasym = $metapkg->get_symbol( $name ); |
| 174 |
|
| 175 |
Returns a metasymbol reference representing the given symbol name |
| 176 |
within the package. The symbol name should include the leading sigil; |
| 177 |
one of the characters *, $, @, % or &. Throws an exception if the |
| 178 |
symbol does not exist. |
| 179 |
|
| 180 |
try_get_symbol, can_symbol |
| 181 |
|
| 182 |
$metasym = $metapkg->try_get_symbol( $name ); |
| 183 |
$metasym = $metapkg->can_symbol( $name ); |
| 184 |
|
| 185 |
Similar to "get_symbol" but returns undef if the symbol does not exist. |
| 186 |
|
| 187 |
add_symbol |
| 188 |
|
| 189 |
$metasym = $metapkg->add_symbol( $name, $valueref ); |
| 190 |
|
| 191 |
Creates a new symbol of the given name in the given package. The new |
| 192 |
symbol will refer to the item given by reference, whose type must match |
| 193 |
the sigil of the symbol name. Returns a metasymbol reference as per |
| 194 |
"get_symbol". If a symbol already existed of the given name then an |
| 195 |
exception is thrown. |
| 196 |
|
| 197 |
Note that this does not create a copy of a variable, but stores an |
| 198 |
alias to the referred item itself within the symbol table. |
| 199 |
|
| 200 |
$metapkg->add_symbol( '@things', \my @array ); |
| 201 |
|
| 202 |
push @array, "more", "values"; |
| 203 |
# these values are now visible in the @things array |
| 204 |
|
| 205 |
If adding a scalar, array or hash variable, the $valueref argument is |
| 206 |
optional. If not provided then a new, blank variable of the correct |
| 207 |
type will be created. |
| 208 |
|
| 209 |
get_or_add_symbol |
| 210 |
|
| 211 |
$metasym = $metapkg->get_or_add_symbol( $name, $valueref ); |
| 212 |
|
| 213 |
Since version 0.003_003. |
| 214 |
|
| 215 |
Similar to "get_symbol" but creates a new symbol if it didn't already |
| 216 |
exist as per "add_symbol". |
| 217 |
|
| 218 |
Note that if the symbol did already exist it is returned and $valueref |
| 219 |
will be ignored. The symbol will not be modified in that case to point |
| 220 |
to the value referred to instead. |
| 221 |
|
| 222 |
remove_symbol |
| 223 |
|
| 224 |
$metapkg->remove_symbol( $name ); |
| 225 |
|
| 226 |
Removes a symbol of the given name from the given package. If the |
| 227 |
symbol was the last item in the glob then the glob too is removed from |
| 228 |
the package. If the named symbol did not previously exist then an |
| 229 |
exception is thrown. |
| 230 |
|
| 231 |
To only conditionally remove a symbol if it already exists, test for it |
| 232 |
first by using "try_get_symbol": |
| 233 |
|
| 234 |
$metapkg->try_get_symbol( '$variable' ) and |
| 235 |
$metapkg->remove_symbol( '$variable' ); |
| 236 |
|
| 237 |
list_symbols |
| 238 |
|
| 239 |
%sub_metasyms = $metapkg->list_symbols; |
| 240 |
%sub_metasyms = $metapkg->list_symbols( sigils => $filter ); |
| 241 |
|
| 242 |
Since version 0.006. |
| 243 |
|
| 244 |
Returns an even-sized key/value list containing the symbols within the |
| 245 |
given package instance. Each symbol is returned as a pair, with its |
| 246 |
sigil-prefixed basename first, followed by a metasymbol instance |
| 247 |
representing it. Since the sigil-prefixed names must be unique, it is |
| 248 |
convenient to assign this list into a hash. The symbols are returned in |
| 249 |
no particular order. |
| 250 |
|
| 251 |
If the optional sigils named parameter is given, it should be a string |
| 252 |
of possible symbol sigils (the characters $, @, % or &). In this case, |
| 253 |
only symbols whose sigil is present in this string will be returned. |
| 254 |
|
| 255 |
list_subpackages |
| 256 |
|
| 257 |
%sub_metapkgs = $metapkg->list_subpackages; |
| 258 |
|
| 259 |
Since version 0.006. |
| 260 |
|
| 261 |
Returns an even-sized key/value list containing the immediate |
| 262 |
sub-packages of the given package instance. Each sub-package is |
| 263 |
returned as a pair, with its basename first (minus the "::" suffix), |
| 264 |
followed by a metapackage instance representing it. Since the names of |
| 265 |
each sub-package must be unique, it is convenient to assign this list |
| 266 |
into a hash. The sub-packages are returned in no particular order. |
| 267 |
|
| 268 |
add_named_sub |
| 269 |
|
| 270 |
$metasub = $metapkg->add_named_sub( $name, $code ); |
| 271 |
|
| 272 |
Since version 0.008. |
| 273 |
|
| 274 |
A convenient shortcut for adding a subroutine symbol and setting the |
| 275 |
subname of the newly-added sub. Equivalent to calling "add_symbol" and |
| 276 |
then "set_subname" on its result, but more efficient as it does not |
| 277 |
have to create a separate fake GV to store the subname in. |
| 278 |
|
| 279 |
Note that $name should be given as a barename, without the leading & |
| 280 |
sigil. |
| 281 |
|
| 282 |
METHODS ON METASYMBOLS |
| 283 |
|
| 284 |
is_glob, is_scalar, ... |
| 285 |
|
| 286 |
$bool = $metasym->is_glob; |
| 287 |
$bool = $metasym->is_scalar; |
| 288 |
$bool = $metasym->is_array; |
| 289 |
$bool = $metasym->is_hash; |
| 290 |
$bool = $metasym->is_subroutine; |
| 291 |
|
| 292 |
Returns true if the symbol being referred to is of the given type, or |
| 293 |
false if not. |
| 294 |
|
| 295 |
reference |
| 296 |
|
| 297 |
$ref = $metasym->reference; |
| 298 |
|
| 299 |
Returns a regular Perl reference to the symbol being represented. |
| 300 |
|
| 301 |
METHODS ON meta::glob |
| 302 |
|
| 303 |
get |
| 304 |
|
| 305 |
$metaglob = meta::glob->get( $globname ); |
| 306 |
|
| 307 |
Since version 0.003_001. |
| 308 |
|
| 309 |
Returns a metaglob reference representing the given symbol from the |
| 310 |
symbol table from a fully-qualified name, if it exists. Throws an |
| 311 |
exception if not. |
| 312 |
|
| 313 |
try_get |
| 314 |
|
| 315 |
$metaglob = meta::glob->try_get( $globname ); |
| 316 |
|
| 317 |
Since version 0.003_003. |
| 318 |
|
| 319 |
Similar to "get" but returns undef if the given symbol does not exist. |
| 320 |
|
| 321 |
get_or_add |
| 322 |
|
| 323 |
$metaglob = meta::glob->get_or_add( $globname ); |
| 324 |
|
| 325 |
Since version 0.003_003. |
| 326 |
|
| 327 |
Similar to "get" but creates the symbol if it didn't already exist. |
| 328 |
|
| 329 |
basename |
| 330 |
|
| 331 |
$name = $metaglob->basename; |
| 332 |
|
| 333 |
Returns the name of the glob within its package. |
| 334 |
|
| 335 |
get_scalar, get_array, ... |
| 336 |
|
| 337 |
$metasym = $metaglob->get_scalar; |
| 338 |
$metasym = $metaglob->get_array; |
| 339 |
$metasym = $metaglob->get_hash; |
| 340 |
$metasym = $metaglob->get_code; |
| 341 |
|
| 342 |
Returns a metasymbol reference representing the symbol in the given |
| 343 |
slot of the glob, if it exists. Throws an exception if not. |
| 344 |
|
| 345 |
try_get_scalar, try_get_array, ... |
| 346 |
|
| 347 |
Similar to "get_scalar", "get_array", etc... but returns undef if the |
| 348 |
given slot does not exist. |
| 349 |
|
| 350 |
METHODS ON METAVARIABLES |
| 351 |
|
| 352 |
value |
| 353 |
|
| 354 |
$scalar = $metavar->value; |
| 355 |
@array = $metavar->value; |
| 356 |
%hash = $metavar->value; |
| 357 |
|
| 358 |
$count = scalar $metavar->value; |
| 359 |
|
| 360 |
Returns the current value of the variable, as if it appeared in regular |
| 361 |
Perl code. |
| 362 |
|
| 363 |
METHODS ON METASUBROUTINES |
| 364 |
|
| 365 |
is_method |
| 366 |
|
| 367 |
$bool = $metasub->is_method; |
| 368 |
|
| 369 |
Since version 0.009. |
| 370 |
|
| 371 |
Returns true if on a version of Perl that supports use feature 'class', |
| 372 |
and the subroutine being represented is a real method created by that |
| 373 |
feature. False for regular sub-based subroutines, and always false on |
| 374 |
earlier versions of Perl before that feature was introduced. |
| 375 |
|
| 376 |
subname |
| 377 |
|
| 378 |
$name = $metasub->subname; |
| 379 |
|
| 380 |
Returns the (fully-qualified) name of the subroutine. |
| 381 |
|
| 382 |
set_subname |
| 383 |
|
| 384 |
$metasub = $metasub->set_subname( $name ); |
| 385 |
|
| 386 |
Since version 0.007. |
| 387 |
|
| 388 |
Sets a new name for the subroutine. |
| 389 |
|
| 390 |
If $name is not fully-qualified (i.e. does not contain a :: sequence), |
| 391 |
then the package name of the caller is used to create the |
| 392 |
fully-qualified name to be stored. |
| 393 |
|
| 394 |
prototype |
| 395 |
|
| 396 |
$proto = $metasub->prototype; |
| 397 |
|
| 398 |
Returns the prototype of the subroutine. |
| 399 |
|
| 400 |
set_prototype |
| 401 |
|
| 402 |
$metasub = $metasub->set_prototype( $proto ); |
| 403 |
|
| 404 |
Since version 0.007. |
| 405 |
|
| 406 |
Sets a new prototype for the subroutine. |
| 407 |
|
| 408 |
Returns the $metasub instance itself to allow for easy chaining. |
| 409 |
|
| 410 |
signature |
| 411 |
|
| 412 |
$metasig = $metasub->signature; |
| 413 |
|
| 414 |
Since version 0.010. |
| 415 |
|
| 416 |
If on Perl version 5.26 or above and the subroutine has a signature, |
| 417 |
returns an object reference representing details about the signature. |
| 418 |
This can be queried using the methods below. If the subroutine does not |
| 419 |
use a signature (or on Perl versions before 5.26) returns undef. |
| 420 |
|
| 421 |
METHODS ON SUBROUTINE METASIGNATURES |
| 422 |
|
| 423 |
mandatory_params |
| 424 |
|
| 425 |
$n = $metasig->mandatory_params; |
| 426 |
|
| 427 |
Returns the number of parameters that are mandatory (i.e. do not have a |
| 428 |
defaulting expression). This is the minimum number of argument values |
| 429 |
that must be passed to any call of this function and does not count a |
| 430 |
final slurpy parameter. |
| 431 |
|
| 432 |
Note that the implicit $self parameter to a method subroutine is |
| 433 |
included in this count. This count will always be at least 1 on such a |
| 434 |
method. |
| 435 |
|
| 436 |
optional_params |
| 437 |
|
| 438 |
$n = $metasig->optional_params; |
| 439 |
|
| 440 |
Returns the number of parameters that are optional (i.e. have a |
| 441 |
defaulting expression). |
| 442 |
|
| 443 |
named_param |
| 444 |
|
| 445 |
%params = $metasig->named_param; |
| 446 |
|
| 447 |
Since version 0.015. |
| 448 |
|
| 449 |
If the subroutine defines any named parameters (as provided by Perl |
| 450 |
version 5.43.5 or later), returns a list of name/value pairs containing |
| 451 |
them. This list is returned in no particular order. Each gives the name |
| 452 |
of one parameter, and its value is a structure object responding to the |
| 453 |
following methods: |
| 454 |
|
| 455 |
$named_param->name; |
| 456 |
$named_param->is_required; |
| 457 |
|
| 458 |
On Perls older than when signature named parameters were added, this |
| 459 |
method always simply returns an empty list. |
| 460 |
|
| 461 |
slurpy |
| 462 |
|
| 463 |
$slurpy = $metasig->slurpy; |
| 464 |
|
| 465 |
Returns the sigil character associated with the final slurpy parameter |
| 466 |
if it exists (i.e. % or @), or undef if no slurpy parameter is defined. |
| 467 |
|
| 468 |
min_args |
| 469 |
|
| 470 |
max_args |
| 471 |
|
| 472 |
$n = $metasig->min_args; |
| 473 |
|
| 474 |
$n = $metasig->max_args; |
| 475 |
|
| 476 |
Returns the minimum or maximum number of argument values that can be |
| 477 |
passed to a call to this function. In many situations there is no upper |
| 478 |
bound, so max_args may return undef. These are simply the boundaries of |
| 479 |
argument count beyond which the function is guaranteed to complain. It |
| 480 |
may of course raise an error during arugment processing for a variety |
| 481 |
of other reaosns. |
| 482 |
|
| 483 |
In the absence of named parameters, min_args is the same as |
| 484 |
mandatory_params, and max_args is either undef if there is a slurpy |
| 485 |
parameter, or the sum of mandatory_params and optional_params if not. |
| 486 |
|
| 487 |
When named parameters (on Perl 5.43.5 or above) are being used, |
| 488 |
min_args counts 1 for every mandatory positional parameter, and 2 for |
| 489 |
every mandatory named parameter. max_args will always be undef when |
| 490 |
named parameters are being used, because Perl always accepts multiple |
| 491 |
duplicate values for any given name; the final one having the effective |
| 492 |
value. |
| 493 |
|
| 494 |
TODO |
| 495 |
|
| 496 |
* Access to the new parts of API introduced by Perl 5.38 to deal with |
| 497 |
classes, methods, fields. |
| 498 |
|
| 499 |
SEE ALSO |
| 500 |
|
| 501 |
PPC 0022 "metaprogramming" |
| 502 |
<https://github.com/Perl/PPCs/blob/main/ppcs/ppc0022-metaprogramming.md> |
| 503 |
|
| 504 |
AUTHOR |
| 505 |
|
| 506 |
Paul Evans <[email protected]> |
| 507 |
|