Why can't my Perl script see our () variables defined by me in another file?

I have a question related to Perl and scoping. I have a shared file with many different variables. I need a shared file in my main script, but I cannot access the variables; they seem to be outside its scope. I assumed that the ad will ourovercome this problem, but it doesn't seem to work.

Script 1: common.pl

#!/usr/bin/perl

our $var1 = "something";
our $var2 = "somethingelse";

Script 2: ftp.pl

#!/usr/bin/perl

use strict;
use warnings;

require('common.pl');

print $var1; 

I get an error:

Global symbol "$ var1" requires explicit package name
+5
source share
5 answers

require, . , our , . package, main. , script $main::var1, .

Exporter. , , script class, .

+8

.

: MyConfig.pm

package MyConfig;
require Exporter;
use strict;

our @ISA                = qw(Exporter);
our @EXPORT             = qw( getconfig ); 

my %confighash = ( 
            thisone => 'one',
            thatone => 2,
            somthingelse => 'froboz',
                   );



sub getconfig {
 return  %confighash;
 }

1;

:

#!/usr/bin/perl
use strict;
use warnings;

use MyConfig;

my %config = getconfig();

print $config{ somthingelse };

froboz

+7

, . , , -. CPAN , .

, , our use vars. PBP .:) our , , .

+6

() - , . - , , ( ). , -. $main:var1 ( ) $var1, our $var1 .

, , , .

+2

. Perl, , script,

#!/usr/bin/perl

$var1 = "something";
$var2 = "somethingelse";
Script 2: ftp.pl

#!/usr/bin/perl

use strict;
use warnings;

our $var1;
our $var2;

require('common.pl');

print $var1;
+1

All Articles