Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Help me how to use require..

by bh_perl (Monk)
on Mar 28, 2012 at 07:30 UTC ( #962089=perlquestion: print w/ replies, xml ) Need Help??
bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

This is my first script called This script have list of variables that will be used on others script later.
my $inputdir = "/cygsrive/d/acec/CDRe/data/input/$swname"; my $arbordir = "/cygsrive/d/acec/CDRe/data/output/ARBOR/$swname"; my $arborcsvdir = "/cygsrive/d/acec/CDRe/data/output/ARBORCSV/$swname" +; my $fmsdir = "/cygsrive/d/acec/CDRe/data/output/FMS/$swname"; my $inbilldir = "/cygsrive/d/acec/CDRe/data/output/INBILL/$swname" +; my $errordir = "/cygsrive/d/acec/CDRe/data/error/$swname"; my $filterdir = "/cygsrive/d/acec/CDRe/data/filter/$swname"; my $archivedir = "/cygsrive/d/acec/CDRe/data/archive/$swname"; my $duplicatedir= "/cygsrive/d/acec/CDRe/data/duplicate/$swname"; my $baddir = "/cygsrive/d/acec/CDRe/data/bad/$swname"
This is my main program called This program will called script to print all the defined variables.
#!/usr/bin/perl require "./"; print "INPUT : $inputdir\n";
But, i can't print out the value.. where is the mistake.. please help me...

Comment on Help me how to use require..
Select or Download Code
Replies are listed 'Best First'.
Re: Help me how to use require..
by tobyink (Abbot) on Mar 28, 2012 at 09:11 UTC

    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

    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:

    • $main::foo
    • @Web::Server::ISA

    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

    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

    #!/usr/bin/perl require "./"; print "INPUT : $MyConfig::inputdir\n";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Help me how to use require..
by Happy-the-monk (Abbot) on Mar 28, 2012 at 07:59 UTC

    One way of doing it is to replace the my functions with our.

    Cheers, Sören

Re: Help me how to use require..
by kcott (Canon) on Mar 29, 2012 at 07:38 UTC

    Unrelated to your scope issue, I think you possibly want the pathnames to start with /cygdrive/d instead of /cygsrive/d. I'm assuming you're using Cygwin and files are on your D: drive.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://962089]
Approved by BrowserUk
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2015-11-27 04:50 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (718 votes), past polls