Variables (not just in Perl, but in pretty much every programming language) have something called a scope. This is an area of the program where that variable is "visible". Accessing a variable from outside its scope is impossible in normal circumstances, and often generates a warning or error.

{ # braces open and close scopes my $variable = "value\n"; # This works, because $variable is visible. print $variable; } # This doesn't work, because we've left the scope # where $variable was originally declared. # Under "use strict" it will generate an error # message. Without "use strict", it will fail # silently. print $variable;

The granddaddy of all scopes is the file scope. If a lexical variable (one declared using my) is declared outside of any braces, then its scope is the file itself. This means that the variables you declared in your example are invisible outside def_variables.pl.

To cross file scopes, you need to use global variables - that is, variables which have a scope of "the entire program". Apart from a few pre-defined variables (like $_ and %ENV - see perlvar for a full list), global variables in Perl are always namespace-qualified. In other words, they always have names which include "::". For example:

Because typing out namespace-qualified variable names is annoying, Perl offers a shortcut to take a namespace-qualified global variable, and start using it as if it were a lexical variable.

{ package Web::Server; # Within this scope, we can use @ISA as a # shortcut for @Web::Server::ISA our @ISA = ('Server'); }

package by the way, is how Perl determines what namespace we're currently in. If you don't explicitly say which package to use, the default is main.

Getting back to your example, here's a rewritten def_variable.pl:

package MyConfig; our $inputdir = "/cygsrive/d/acec/CDRe/data/input/$swname"; our $arbordir = "/cygsrive/d/acec/CDRe/data/output/ARBOR/$swname"; our $arborcsvdir = "/cygsrive/d/acec/CDRe/data/output/ARBORCSV/$swnam +e"; our $fmsdir = "/cygsrive/d/acec/CDRe/data/output/FMS/$swname"; our $inbilldir = "/cygsrive/d/acec/CDRe/data/output/INBILL/$swname" +; our $errordir = "/cygsrive/d/acec/CDRe/data/error/$swname"; our $filterdir = "/cygsrive/d/acec/CDRe/data/filter/$swname"; our $archivedir = "/cygsrive/d/acec/CDRe/data/archive/$swname"; our $duplicatedir = "/cygsrive/d/acec/CDRe/data/duplicate/$swname"; our $baddir = "/cygsrive/d/acec/CDRe/data/bad/$swname";

And here's a rewritten main.pl:

#!/usr/bin/perl require "./def_variable.pl"; print "INPUT : $MyConfig::inputdir\n";
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Help me how to use require.. by tobyink
in thread Help me how to use require.. by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.