Alias-Any

 view release on metacpan or  search on metacpan

lib/Alias/Any.pm  view on Meta::CPAN

package Alias::Any;

use 5.012;
use warnings;

our $VERSION = '0.000007';

use Keyword::Simple;

# Make 'keyword' and 'unkeyword' no-ops, unless Keyword::Declare is later loaded...
BEGIN {
    sub _delete_source { my $ref = shift; $$ref =~ s{ \A .*? [#]END_OF_ALIAS }{}xms; }
    Keyword::Simple::define(   keyword => \&_delete_source );
    Keyword::Simple::define( unkeyword => \&_delete_source );
}

sub import {
    # Below 5.22, alias keyword just injects Data::Alias...
    if ($^V <  5.022) {
        Keyword::Simple::define(
            alias => sub {
                my $ref = shift;
                $$ref = q{use Data::Alias; Data::Alias::alias} . $$ref;
            }
        );
    }

    # Above 5.22, alias keyword replaced with built-in syntax...
    use if $^V >= 5.022, 'Keyword::Declare';

    keyword alias (Variable|VariableDeclaration $variable, '=', Expr $expr) {{{
        use feature 'refaliasing';
        no warnings 'experimental::refaliasing';
        \<{$variable}> = \<{$expr}>
    }}} #END_OF_ALIAS
}

sub unimport {
    # Below 5.22, alias keyword is undefined by Keyword::Simple...
    if ($^V < 5.022) {
        Keyword::Simple::undefine('alias');
        if ($Keyword::Simple::VERSION < 0.04) {
            $^H{'Keyword::Simple/keywords'} =~ s{ alias:(?:\d+|-\d*)}{}g;
        }
    }

    # Above 5.22, alias keyword is unkeyworded by Keyword::Declare...
    use if $^V >= 5.022, 'Keyword::Declare';
    unkeyword alias; #END_OF_ALIAS
}


1; # Magic true value required at end of module
__END__

=head1 NAME

Alias::Any - Create lexical aliases under different versions of Perl


=head1 VERSION

This document describes Alias::Any version 0.000007


=head1 SYNOPSIS

    use v5.12;
    use Alias::Any;

    alias my $scalar_alias = $scalar_var;

    alias my @array_alias = @array_var;

    alias my %hash_alias = %hash_var;


    no Alias::Any;

    alias my $var = $ref;    # Syntax error


=head1 DESCRIPTION

This module is simply a convenient wrapper around the various mechanisms
by which aliases can be defined under different versions of Perl.

Under Perl 5.12 to 5.18, the module uses the 'alias' function
from the Data::Alias module to create the requested alias.

But previous releases of Data::Alias didn't work under Perl 5.24 or
later, and from Perl 5.22 there is a more robust built-in aliasing
mechanism available anyway. So under Perl 5.22 or later, this module
uses the built-in mechanism in preference to Data::Alias.

This means you can define simple aliases using a consistent syntax,
without needing to worry which version of Perl (or Data::Alias)
your code will vebe running under.


=head1 INTERFACE

The module exports a single keyword (C<alias>) that constructs the



( run in 1.849 second using v1.01-cache-2.11-cpan-5b529ec07f3 )