tags:

views:

86

answers:

2

I have two scripts and two conf file (actually perl scripts too):

conf1.pl

@some_array = ({name =>"orange", deny = > "yes"},
               {name =>"apple", deny = > "no"});

conf2.pl

@some_array = ({name =>"male", deny = > "yes"},
               {name =>"female", deny = > "no"});

script.pl

#!/usr/bin/perl -w
use strict;
our %deny = ();
call_another_script.pl_somehow_with_param conf1.pl
call_another_script.pl_somehow_with_param conf2.pl
foreach my $key (%deny) {
    print $deny{$key},"\n";
}

another_script.pl

#!/usr/bin/perl -w
my $conf_file = shift;
do $conf_file;
foreach my $item (@some_array) {
    print $item->{name},"\n";
    if (defined $deny) {
       $deny{$item{name}}++ if $item{deny} eq "yes";
    }
}

I would like to call another_script.pl with conf filenames from script.pl so %deny will be visible in another_script.pl. And I dont wanna use Perl modules and I want to have scripts in separate files. For example

./another_script.pl conf2.pl

and

./script

+4  A: 

This problem is what modules are designed to solve. What you are asking is similar to "how do I conditionally execute code with out if?". We can tell you how to do it, but it isn't a good idea.

conf1.pl

#!/usr/bin/perl

use strict;
use warnings;

our @a = (1 .. 10);

conf2.pl

#!/usr/bin/perl

use strict;
use warnings;

our @a = ("a" .. "j");

master.pl

#!/usr/bin/perl

use strict;
use warnings;

our %deny;
do "conf1.pl";
do "child.pl";
do "conf2.pl";
do "child.pl";

use Data::Dumper;
print Dumper \%deny;

child.pl

#!/usr/bin/perl

use strict;
use warnings;

our %deny;
our @a;

for my $item (@a) {
    $deny{$item}++;
}
Chas. Owens
The problem with rewards for answering what they ask comes up when they ask for the wrong thing. This is so nasty, why would you show him this?
masonk
@masonk I was going to show him how to do it correctly as well so he could see the difference, but I haven't had the time yet to go back and fix it.
Chas. Owens
I just wanted to do as in bash: write one script, another script - each works independently and takes parameters.And just to call one script from another when when it is necessary to solve more general problem.Without headaches with the scopes and modules. And each script can be executed independently.Like:master#!/bin/shVARIABLE=valueexport VARIABLE./childchild#!/bin/shecho $VARIABLE
gapsf
@gapsf Then use `bash`. Perl is not `bash`. If you try to force Perl to be like `bash` you will miss out on all of the stuff that makes Perl better than `bash`.
Chas. Owens
A: 

From http://www.serverwatch.com/tutorials/article.php/1128981/The-Perl-Basics-You-Need-To-Know.htm

Making Variables Global With Strict Pragma On First you use:

use strict;

Then you use:

use vars qw( %hash @array);

This declares the named variables as package globals in the current package. They may be referred to within the same file and package with their unqualified names; and in different files/packages with their fully qualified names. That's all that I was needed!

gapsf