RFC eq {Request For Comments} --update:finally uploaded to cpan on Mon Nov 3 03:35:54 2003
# I basically took vars.pm, and turned it into vars/i.pm package vars::i; $vars::i::VERSION = '1.01'; # yuck =for IT DOESN'T WORK nor is it important (try perl -Mstrict -Mwarnings +=all -We"use vars q[$A];") BEGIN { eval q{ use warnings::register; }; eval q{ sub warnings::enabled { return $^W; } } if $@; } =cut use strict qw(vars subs); sub import { return if @_ < 2; my( $pack, $var, @value ) = @_; my $callpack = caller; my %stuff; if( not @value ){ if(ref $var ){ %stuff = @$var; } else { return; } } else { %stuff = ( $var, [@value] ); } for my $k( keys %stuff ){ $var = $k; if( ref $stuff{$k} eq 'ARRAY' ){ @value = @{ $stuff{$k} }; } elsif( ref $stuff{$k} eq 'HASH' ){ @value = %{ $stuff{$k} }; } else { @value = $stuff{$k}; } if( my( $ch, $sym ) = $var =~ /^([\$\@\%\*\&])(.+)/ ){ if( $sym =~ /\W/ ){ # time for a more-detailed check-up if( $sym =~ /^\w+[[{].*[]}]$/ ){ require Carp; Carp::croak("Can't declare individual elements of +hash or array"); } =for IT DOESN'T WORK nor is it important elsif( warnings::enabled() and length($sym) == 1 and $ +sym !~ tr/a-zA-Z// ){#$! require Carp; Carp::carp("No need to declare built-in vars"); #warnings::warn("No need to declare built-in vars" +); } =cut elsif( ($^H &= strict::bits('vars')) ){ require Carp; Carp::croak("'$var' is not a valid variable name u +nder strict vars"); } } $sym = "${callpack}::$sym" unless $sym =~ /::/; if( $ch eq '$' ){ *{$sym} = \$$sym; (${$sym}) = @value; } elsif( $ch eq '@' ){ *{$sym} = \@$sym; (@{$sym}) = @value; } elsif( $ch eq '%' ){ *{$sym} = \%$sym; (%{$sym}) = @value; } elsif( $ch eq '*' ){ *{$sym} = \*$sym; (*{$sym}) = shift @value; } elsif( $ch eq '&' ){ *{$sym} = shift @value; } else { require Carp; Carp::croak("'$var' is not a valid variable name"); } } else { require Carp; Carp::croak("'$var' is not a valid variable name"); } } }; 1; __END__ =head1 NAME vars::i - Perl pragma to declare and simultaneously initialize global +variables. =head1 SYNOPSIS use Data::Dumper; $Data::Dumper::Deparse = 1; # use vars::i '$VERSION' => 3.44; use vars::i '@BORG' => 6 .. 6; use vars::i '%BORD' => 1 .. 10; use vars::i '&VERSION' => sub(){rand 20}; use vars::i '*SOUTH' => *STDOUT; # BEGIN { print SOUTH Dumper [ $VERSION, \@BORG, \%BORD, \&VERSION ]; } # use vars::i [ # has the same affect as the 5 use statements above '$VERSION' => 3.66, '@BORG' => [6 .. 6], '%BORD' => {1 .. 10}, '&VERSION' => sub(){rand 20}, '*SOUTH' => *STDOUT, ]; # print SOUTH Dumper [ $VERSION, \@BORG, \%BORD, \&VERSION ]; # __END__ =head1 DESCRIPTION For whatever reason, I once had to write something like BEGIN { use vars '$VERSION'; $VERSION = 3; } and I really didn't like typing that much. Also, I like being able to say use vars::i '$VERSION' => sprintf("%d.%02d", q$Revision: 1.3 $ =~ +/: (\d+)\.(\d+)/); use vars::i [ '$VERSION' => sprintf("%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.( +\d+)/), '$REVISION'=> '$Id: GENERIC.pm,v 1.3 2002/06/02 11:12:38 _ Exp $' +, ]; Like with C<use vars;>, there is no need to fully qualify the variable + name. =head1 SEE ALSO See L<vars>, L<perldoc/"our">, L<perlmodlib/Pragmatic Modules>. =begin FOR LATER ON CPAN =head1 AUTHOR D.H aka PodMaster Please use http://rt.cpan.org/ to report bugs (there shouldn't be any +;p). Just go to http://rt.cpan.org/NoAuth/Bugs.html?Dist=vars-i to see a bug list and/or report new ones. =end FOR LATER ON CPAN =head1 LICENSE Copyright (c) 2003 by D.H. aka PodMaster. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. If you don't know what this means +, visit http://perl.com/ or http://cpan.org/. =cut


MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
** The Third rule of perl club is a statement of fact: pod is sexy.

  • Comment on RFC: vars::i - Perl pragma to declare and simultaneously initialize global variables.
  • Download Code

Replies are listed 'Best First'.
Re: RFC: vars::i - Perl pragma to declare and simultaneously initialize global variables.
by Juerd (Abbot) on Jun 08, 2003 at 13:05 UTC

    use vars::i '$VERSION' => 3.44; use vars::i '@BORG' => 6 .. 6; use vars::i '%BORD' => 1 .. 10; use vars::i '&VERSION' => sub(){rand 20}; use vars::i '*SOUTH' => *STDOUT;

    our $VERSION = 3.44; our @BORG = (6); our %BORD = 1 .. 10; our *SOUTH = *STDOUT; sub VERSION () { rand 20 }

    Are you still living in 5.00x-land, or do you for some reason dislike our (in which case please explain (I think lexical declaration is a Good Thing))?

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      For the same reasons people still use vars (think of it as an extension of vars).


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        For the same reasons people still use vars (think of it as an extension of vars).

        And what is the reason for using vars? Is it 5.00x-compatibility or is it because people dislike our? (In which case I'd still like an explanation :)

        Whatever the reason is, I think you should document it.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: RFC: vars::i - Perl pragma to declare and simultaneously initialize global variables.
by shotgunefx (Parson) on Jun 08, 2003 at 23:39 UTC
    PodMaster++
    I've always been a little irked about having to split declaring and assigning with vars.

    -Lee

    "To be civilized is to deny one's nature."
Re: RFC: vars::i - Perl pragma to declare and simultaneously initialize global variables.
by data64 (Chaplain) on Jun 09, 2003 at 00:48 UTC

    Neat. ++PodMaster

    Is the long term plan to submit it as a patch/replacement for vars.pm ? It is backwards compatible with vars.pm, is it not ?

    data64
      Is the long term plan to submit it as a patch/replacement for vars.pm ?
      No. Theoretically it is possible to incorporate the 2nd form of usage, but I doubt the p5p would accept that patch, and frankly I would rather not deal with p5p. If vars.pm was patched, people would be forced to write stuff like use vars 1.02 [ '$VERSION' => 4.4 ]; and the p5p would be in charge of maintenance. Sure, much like ExtUtils::MakeMaker I *could* distribute vars.pm like a regular CPAN distribution, but i'd rather not do that (at least for now).
      It is backwards compatible with vars.pm, is it not ?
      It is not backwards compatible with vars.pm. If you look at the documentation you will see how the usage differs.


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.