Created
December 29, 2021 16:28
-
-
Save trwyant/2070ccbd7765f3d0b54ab1f5786199bf to your computer and use it in GitHub Desktop.
Sample Perl lexical pragma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.010; | |
use strict; | |
use warnings; | |
package Noise; | |
BEGIN { | |
# This trick prevents 'use Noise ...' from trying to find and load | |
# Noise.pm. | |
$INC{'Noise.pm'} = $0; | |
} | |
# The convention for naming %^H keys is "name-space/key-name". | |
use constant NOISE => join '/', __PACKAGE__, 'noise'; | |
# This gets called implicitly by 'use Noise ...' | |
sub import { | |
$^H{ +NOISE } = $_[1]; # $_[0] is the package name. | |
return; | |
} | |
sub noise { | |
my $hints_hash = ( caller 0 )[10]; | |
return $hints_hash->{ +NOISE }; | |
} | |
sub call_import { | |
my ( $invocant, @args ) = @_; | |
return $invocant->import( @args ); | |
} | |
package main; | |
use Noise 'The dog barks'; | |
say Noise->noise(); # The dog barks | |
{ | |
use Noise 'The cat meows'; | |
say Noise->noise(); # The cat meows | |
} | |
# The previous value is restored on scope exit | |
say Noise->noise(); # The dog barks | |
BEGIN { | |
Noise->import( 'The cow moos' ); | |
} | |
# A BEGIN block is good enough. No magic specific to 'use' | |
say Noise->noise(); # The cow moos | |
BEGIN { | |
{ | |
Noise->call_import( 'The giraffe says nothing' ); | |
} | |
} | |
# Scopes, or even call frames, in a BEGIN do not matter when setting %^H | |
say Noise->noise(); # The giraffe says nothing | |
# ex: set textwidth=72 : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So THAT'S how that works! I've been Perling since 1997 and yet I still manage to find out new things. Thank you.