| 1 | package your; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 1 | require 5.004; | |||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict qw(vars subs); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 6 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 7 | 1 | $VERSION = '1.00'; | |||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||
| 11 | your - Perl pragma to declare use of other package's variables | ||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||
| 15 | use your qw($Foo::bar @Moo::you %This::that); | ||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||
| 19 | You should use variables from other packages with care, but as long as | ||||||||||||||||||||||||||
| 20 | you're going to, it doesn't hurt to predeclare it. | ||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | Currently, if you use another package's variables and only use it | ||||||||||||||||||||||||||
| 23 | once, Perl will throw a "variable used only once" warning. vars.pm | ||||||||||||||||||||||||||
| 24 | won't allow you to declare other package's variables, and there are | ||||||||||||||||||||||||||
| 25 | various hack to get around that. This package lets you declare "Yes, | ||||||||||||||||||||||||||
| 26 | I am going to use someone else's variables!" | ||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||
| 28 | use Foo; | ||||||||||||||||||||||||||
| 29 | use your qw($Foo::magic); | ||||||||||||||||||||||||||
| 30 | print $Foo::magic; | ||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | sub import { | ||||||||||||||||||||||||
|
Subroutine
import: called CC=11 SCAR=36.8
| |||||||||||||||||||||||||||
| 36 | 2 | my $caller = caller; | |||||||||||||||||||||||||
| 37 | 2 | my($class, @imports) = @_; | |||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||
| 39 | 2 | foreach my $sym (@imports) { | |||||||||||||||||||||||||
| 40 | 3 | my($type, $name) = unpack('a1a*', $sym); | |||||||||||||||||||||||||
| 41 | 3 | ▶ | unless( $name =~ /::/ ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 42 | 1 | require Carp; | |||||||||||||||||||||||||
| 43 | 1 | Carp::croak("Can only declare other package's variables"); | |||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||
| 45 | 2 | ▶ | if( $name =~ tr/A-Za-z_0-9://c ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 46 | 0 | ▶ | if( $sym =~ /^\w+[\[{].*[\]}]$/ ) { | ||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||
| 47 | 0 | require Carp; | |||||||||||||||||||||||||
| 48 | 0 | Carp::croak("Can't declare individual elements of a hash or array"); | |||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||
| 50 | elsif ( $^H &= strict::bits('vars') ) { | ||||||||||||||||||||||||||
| 51 | 0 | require Carp; | |||||||||||||||||||||||||
| 52 | 0 | Carp::croak("'$sym' is not a valid variable name under strict vars"); | |||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||
| 55 | *$name = | ||||||||||||||||||||||||||
| 56 | ( $type eq "\$" ? \$$name | ||||||||||||||||||||||||||
| 57 | : $type eq "\@" ? \@$name | ||||||||||||||||||||||||||
| 58 | : $type eq "\%" ? \%$name | ||||||||||||||||||||||||||
| 59 | : $type eq "\*" ? \*$name | ||||||||||||||||||||||||||
| 60 | : $type eq "\&" ? \&$name | ||||||||||||||||||||||||||
| 61 | 2 | ▶ | : do { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 62 | 0 | require Carp; | |||||||||||||||||||||||||
| 63 | 0 | Carp::croak("'$sym' is not a valid variable name"); | |||||||||||||||||||||||||
| 64 | }); | ||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||
| 69 | =head1 AUTHOR | ||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||
| 71 | Michael G Schwern <schwern@pobox.com> | ||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||
| 73 | =head1 COPYRIGHT | ||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||
| 75 | Copyright 2001, 2007 by Michael G Schwern E<lt>schwern@pobox.comE<gt>. | ||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||
| 77 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||
| 78 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||
| 80 | See F<http://dev.perl.org/licenses/> | ||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||
| 82 | =head1 SEE ALSO | ||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||
| 84 | L<vars>, L<perlfunc/our>, L<perlfunc/my>, L<strict> | ||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||
| 88 | 1 | 1; | |||||||||||||||||||||||||