indirect(3)

πŸ’… indirect(3) β€” the indirect pragma, bestie edition

okay so this is indirect(3), straight out of the User Contributed Perl Documentation crew, and it's literally a pragma that lexically warns you about using the indirect method call syntax. yes that syntax. the cursed one. buckle up πŸŽ€

✨ NAME ✨

indirect β€” lexically warn about using the indirect method call syntax. that's it, that's the whole gig πŸ’…

πŸ¦‹ VERSION πŸ¦‹

Version 0.39 β€” slay, we're on 0.39, no cap.

🌸 SYNOPSIS 🌸

okay first, in a script :

no indirect;               # lexically enables the pragma
my $x = new Apple 1, 2, 3; # warns
{
 use indirect;     # lexically disables the pragma
 my $y = new Pear; # legit, does not warn
 {
  # lexically specify an hook called for each indirect construct
  no indirect hook => sub {
   die "You really wanted $_[0]\->$_[1] at $_[2]:$_[3]"
  };
  my $z = new Pineapple 'fresh'; # croaks 'You really wanted...'
 }
}
try { ... }; # warns if try() hasn't been declared in this package

no indirect 'fatal';     # or ':fatal', 'FATAL', ':Fatal' ...
if (defied $foo) { ... } # croaks, note the typo

and then the global uses, which are lowkey the main character energy of this whole page :

# Globally enable the pragma from the command-line
perl -M-indirect=global -e 'my $x = new Banana;' # warns

# Globally enforce the pragma each time perl is executed
export PERL5OPT="-M-indirect=global,fatal"
perl -e 'my $y = new Coconut;' # croaks

πŸ‘‘ DESCRIPTION πŸ‘‘

when it's enabled, this pragma warns about indirect method calls that are present in your code. simple as that ✨

now, the indirect syntax is honestly considered harmful these days β€” its parsing has so many quirks and using it is straight up error prone. like, when the subroutine foo has not been declared in the current package, foo $x actually compiles to $x->foo, and foo { key => 1 } compiles to 'key'->foo(1). not me side-eyeing this syntax 😭 if you want the full tea, go peek at the "REFERENCES" section β€” there's a whole more complete list of reasons for avoiding this construct.

heads up: this pragma currently does not warn for core functions (print, say, exec or system). that may change in the future, or it may get added as optional features that would be enabled by passing options to unimport. so like, don't come for it yet πŸ’€

also just so we're clear β€” this module is not a source filter. periodt.

🫢 METHODS 🫢

unimport

no indirect;
no indirect 'fatal';
no indirect hook => sub { my ($obj, $name, $file, $line) = @_; ... };
no indirect 'global';
no indirect 'global, 'fatal';
no indirect 'global', hook => sub { ... };

this gets magically called when no indirect @opts is encountered β€” yes, magically, that's the technical term πŸ’« it turns the module on. the policy that gets applied depends on what is first found in @opts :

  • if it's a string that matches /^:?fatal$/i, the compilation will croak when the first indirect method call is found. this option is mutually exclusive with the 'hook' option, so pick one bestie πŸ™ˆ
  • if the key/value pair hook => $hook comes first, $hook will be called for each error, with a string representation of the object as $_[0], the method name as $_[1], the current file as $_[2] and the line number as $_[3]. and if and only if the object is actually a block, $_[0] is assured to start by '{'. this option is mutually exclusive with the 'fatal' option βœ‹
  • if none of fatal and hook are specified, a warning will be emitted for each indirect method call. the chill option πŸ’…
  • if @opts contains a string that matches /^:?global$/i, the pragma will be globally enabled for all code compiled after the current no indirect statement β€” except for code that's in the lexical scope of use indirect. this option may come indifferently before or after the fatal or hook options, in the case they're also passed to unimport ✨

the global policy applied is the one resulting from the fatal or hook options, so it defaults to a warning when none of those are specified :

no indirect 'global';                # warn for any indirect call
no indirect qw<global fatal>;        # die on any indirect call
no indirect 'global', hook => \&hook # custom global action
note that if another policy is installed by a no indirect statement further in the code, it will overrule the global policy πŸ‘€
no indirect 'global';  # warn globally
{
 no indirect 'fatal';  # throw exceptions for this lexical scope
 ...
 require Some::Module; # the global policy will apply for the
                       # compilation phase of this module
}

import

use indirect;

magically called at each use indirect, and it turns the module off 🚫

and as explained up in unimport's description, a use indirect statement will lexically override a global policy previously installed by no indirect 'global', ... β€” if there's one, that is.

🧚 FUNCTIONS 🧚

msg

my $msg = msg($object, $method, $file, $line);

returns the default error message that indirect generates when an indirect method call is reported. it's giving helpful internals πŸ’–

⭐ CONSTANTS ⭐

I_THREADSAFE

true iff the module could have been built with thread-safety features enabled ✨

I_FORKSAFE

true iff this module could have been built with fork-safety features enabled. this will always be true except on Windows, where it's false for perl 5.10.0 and below πŸ’€

πŸ”₯ DIAGNOSTICS πŸ”₯

Indirect call of method "%s" on object "%s" at %s line %d.

the default warning/exception message thrown when an indirect method call on an object is found. classic.

Indirect call of method "%s" on a block at %s line %d.

the default warning/exception message thrown when an indirect method call on a block is found. also classic 🌈

🌸 ENVIRONMENT 🌸

PERL_INDIRECT_PM_DISABLE

if this environment variable is set to true when the pragma is used for the first time, the XS code won't be loaded and β€” although the 'indirect' lexical hint will be set to true in the scope of use β€” the pragma itself won't do anything πŸ™ˆ in this case, the pragma will always be considered to be thread-safe, and as such I_THREADSAFE will be true. this is useful for disabling indirect in production environments, iykyk.

note that clearing this variable after indirect was loaded has no effect. if you want to re-enable the pragma later, you also need to reload it by deleting the 'indirect.pm' entry from %INC πŸ‘‘

πŸ’€ CAVEATS πŸ’€

the implementation was tweaked to work around several limitations of vanilla perl pragmas : it's thread safe, and it does not suffer from a "perl 5.8.x-5.10.0" bug that causes all pragmas to propagate into required scopes 🫢

before perl 5.12, meth $obj (no semicolon) at the end of a file is not seen as an indirect method call, although it is as soon as there's another token before the end (as in meth $obj; or meth $obj 1). if you use perl 5.12 or greater, those constructs are correctly reported ✨

with 5.8 perls, the pragma does not propagate into eval STRING. this is due to a shortcoming in the way perl handles the hints hash, which is addressed in perl 5.10.

and the big one: the search for indirect method calls happens before constant folding. hence my $x = new Class if 0 will be caught 😭 yes, even the dead code. it sees all.

πŸŽ€ REFERENCES πŸŽ€

numerous articles have been written about the quirks of the indirect object construct, and honestly, they ate :

πŸ“ DEPENDENCIES πŸ“

  • perl 5.8.1.
  • a C compiler. this module may happen to build with a C++ compiler as well, but don't rely on it, as no guarantee is made in this regard πŸ™ˆ
  • Carp (standard since perl 5), XSLoader (since perl 5.6.0).

πŸ’œ AUTHOR πŸ’œ

Vincent Pit <vpit@cpan.org>.

you can contact me by mail or on irc.perl.org (vincent) 🫢

πŸ¦‹ BUGS πŸ¦‹

please report any bugs or feature requests to bug-indirect at rt.cpan.org, or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=indirect>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes ✨

🌈 SUPPORT 🌈

you can find documentation for this module with the perldoc command.

perldoc indirect

🧚 ACKNOWLEDGEMENTS 🧚

  • Bram, for motivation and advices πŸ’–
  • Andrew Main and Florian Ragwitz, for testing on real-life code and reporting issues 🫢

Copyright 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2019 Vincent Pit, all rights reserved.

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page bestie πŸŽ€ sign-off time: it's perl v5.32.1, dated 2019-11-06, and it's the indirect(3) manpage β€” go forth and stop writing new Class fr fr πŸ’…βœ¨

πŸ“¦ where this page comes from