Why should Python have something that Perl doesn't have?
I wondered so myself after reading The Very::Long::Class::Name problem and all its responses.
broquaint's response (using Package::Alias' knowledge about aliasing stashes at compile time) and bart's response (which is the simple assignment of @ISA) made me come up with YAS (Yet Another Solution). Now I'm wondering whether I should upload this to CPAN or not.
- SYNOPSIS
use as;
use Very::Long::Module::Name as Foo;
my $foo = Foo->new;
package as;
# Make sure we have version info for this module
# Make sure we do everything by the book from now on
$VERSION = '0.01';
use strict;
# Make sure we can do a source filter
use Filter::Util::Call ();
# Satisfy -require-
1;
#---------------------------------------------------------------------
+------
# Methods needed by Perl
#---------------------------------------------------------------------
+------
# IN: 1 class (ignored)
sub import {
# Initialize the fixer upper subroutine
# Obtain parameters
# Create the alias specification if appropriate
# Return possibly adapted string
my $fix = sub {
my ($package,$rest) = @_;
my $alias = $rest =~ s#\bas\s*((?:\w|\w::\w)+)## ?
qq(BEGIN {*{"${1}::"} = \\*{"${package}::"}}) : ''; # Package
+::Alias approach
# qq(BEGIN {\@${1}::ISA = '$package'}) : ''; # bart's version
"${alias}use $package$rest";
}; #$fix
# Install the filter as an anonymous sub
# Initialize status
Filter::Util::Call::filter_add( sub {
my $status;
# If there are still lines to read
# Convert the line if it appears to contain a use statement
# Return the status
if (($status = Filter::Util::Call::filter_read()) > 0) {
s#\buse\s+((?:\w|\w::\w)+)([^;]*)#$fix->( $1,$2 )#eg;
}
$status;
} );
} #import
#---------------------------------------------------------------------
+------
__END__
=head1 NAME
as - load OO module under another name
=head1 SYNOPSIS
use as;
use Very::Long::Module::Name as Foo;
my $foo = Foo->new;
=head1 DESCRIPTION
This module implements a source filter that allows using of modules wi
+th a
very long name to be used under another name. This is basically achie
+ved
by creating a dummy module with the short name that inherits from the
+module
with the large name and which therefore means that there is a run-time
penalty for this feature.
=head1 INSPIRATION
Inspired by bart's response (http://www.perlmonks.org/index.pl?node_id
+=299082)
to a thread about long module names on Perl Monks.
=head1 AUTHOR
Elizabeth Mattijsen, <liz@dijkmat.nl>.
Please report bugs to <perlbugs@dijkmat.nl>.
=head1 COPYRIGHT
Copyright (c) 2003 Elizabeth Mattijsen <liz@dijkmat.nl>. All rights
reserved. This program is free software; you can redistribute it and/
+or
modify it under the same terms as Perl itself.
=cut
Comments welcome. Particularly about whether the stash alias approach, or the @ISA approach is best. And if you think this would be a waste of CPAN space ;-)
Liz
Update:
In this response I describe why this proposal, as well as any similarly functioning module such as Package::Alias, suffer from a serious flaw because they change the global namespace. Needless to say, I won;t be uploading this module to CPAN. Everybody can relax again now ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|