JockoHelios has asked for the wisdom of the Perl Monks concerning the following question:

I'm using strict. I have a set of 11 defined values which are used by the main script, and all subroutines. The values don't change. They're mnemonics to specify the array location of fields. Example:

my $Date = 0; my $Time = 1;

I'm currently declaring this block of variables with slightly different names in each of the subroutine modules. Example from a module:

my $SRE_Date = 0; my $SRE_Time = 1;

I'd like to declare these globally in the main script, but avoid using :: notation, since this would be as verbose as my subroutine-specific naming scheme. Either way seems clunky, so I checked here for nodes on how to declare these globally. The clearest I could find was:
http://www.perlmonks.org/?node_id=22782
I then tried several different ways, and the best I can come up with is this:

use vars ( $Date, $Time );
$Date = 0; $Time = 1;

Placing that in my main script, I still get error messages like

Global symbol "$Date" requires explicit package name at Master.Pl line 13.

Putting it in a seperate module to use, I have run into this message:

Use of uninitialized value $_ in pattern match (m//) at C:/Perl/lib/vars.pm

I know I'm missing something which I'll likely spend an inordinate amount of time trying to decipher. If you've seen this before, or know how to declare variables globally using strict, I'd appreciate your input.

Dyslexics Untie !!!

Replies are listed 'Best First'.
Re: use vars - how to use this
by 2teez (Vicar) on Jun 07, 2013 at 01:50 UTC

    Hi JockoHelios,
    what about using our?.
    Does the following below script clear somethings for you?

    use strict; use warnings; { package Foo; use vars qw($bar); our $foo = 20; $bar =40; 1; } { package Bar; my $bar; $bar = 50; print $Foo::bar,$/; print $Foo::foo,$/; print $bar; 1; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: use vars - how to use this
by NetWallah (Canon) on Jun 07, 2013 at 00:12 UTC
    How about using "sub" to create read-only variables:
    sub Date{0}; sub Time{1}; My $var=$SomeGloBal[Date];

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: use vars - how to use this (copy paste)
by Anonymous Monk on Jun 07, 2013 at 00:33 UTC

    You found an example, but you didn't copy/paste

    The docs for vars also has example

    Did you notice the difference? qw are quotes

    use vars qw/ $foo $bar /; is the same as use vars '$foo', '$bar';

    see perlop#Quote and Quote like Operators

      SheBang !!! That was the problem. My variables weren't in single quotes, or declared with qw. I did notice the qw in the example from the other node, but didn't understand that I needed to qoute my variables if I declare them the other way.

      I was correct on something, though. I would have spent hours trying to ferret out my error. Thanks for your help !

      Dyslexics Untie !!!