Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to pass variable values between perl scripts ?

by bh_perl (Monk)
on May 12, 2012 at 02:58 UTC ( #970127=perlquestion: print w/ replies, xml ) Need Help??
bh_perl has asked for the wisdom of the Perl Monks concerning the following question:


Hi

This is my first script called main.pl
#!/usr/bin/perl require "sub_program_a.pl"; print "CELCOM INPUT DIRECTORY : $INPUTCEL\n"; require "sub_program_b.pl"; print "AFFIN INPUT DIRECTORY : $INPUTAFF\n";

This is my first sub program called sub_program_a.pl
#!/usr/bin/perl our $INPUTCEL = "/home/ftpcelcom/data"; 1;

This is my second sub program called sub_program_b.pl
#!/usr/bin/perl our $INPUTAFF = "/home/ftpaffin/data"; 1;

I plan to:

1. Create another sub program called sub_program_c.pl

2. Pass the variable $INPUTAFF and $INPUTCEL into sub_program_c.pl

3. Changes the variables values as below:

- $INPUTAFF = "/home/ftpaffin/data/archive"

- $INPUTCEL = "/home/ftpcelcom/data/archive"

4. Return the values back to main.pl

Please help me how to do that, I have tried used require but its look failed might be I am not good enough.

Hopefully you are understand about my question and Please help me..

Comment on How to pass variable values between perl scripts ?
Select or Download Code
Re: How to pass variable values between perl scripts ?
by NetWallah (Abbot) on May 12, 2012 at 04:27 UTC
    Your program works fine for me as it is.

    What error are you getting?
    You can also use "do", instead of "require".

                 I hope life isn't a big joke, because I don't get it.
                       -SNL


      Yes, you are right there are no error.. but how could i pass the variable values into another perl script ?

      Below is my main program called main_program.pl and it was export values from 2.configuration_files.pl script.

      But, how could i pass the $SWITCHNAME into another script called open_file.pl
      ############################################################ #FIRST script: main_program.pl #!/usr/bin/perl use Getopt::Long; use Time::ParseDate qw(parsedate); use Time::Local; use POSIX qw(strftime); our ($GSX, $ASX, $TRACE, $SWITCHNAME, $FILENAME); GetOptions ( "g" => \$GSX, "a" => \$ASX, "t" => \$TRACE, "s=s" => \$SWITCHNAME, "f=s" => \$FILENAME ) or USAGE (); sub USAGE { print "USAGE: $0 -[ag] -f <filename>\n\n"; print "DESCRIPTION:\n"; print "\t -a : Defined ASX file type\n"; print "\t -g : Defined GSX file type\n"; print "\t -f : Assigned input file\n\n"; print "EXAMPLE:\n"; print "\t # $0 -a -f 1000001.ACT\n"; print "\t - Processed file name 10000001.ACT with ASX file type.\n +"; print "\n"; print "\t # $0 -g -f 1000001.ACT\n"; print "\t - Processed file name 10000001.ACT with GSX file type.\n +"; exit; } $SWITCHNAME = uc($SWITCHNAME); require "2.configuration_file.pl";
      ############################################################ #SECOND script: configuration_file.pl #!/usr/bin/perl use strict; use warnings; our $INPUTDIR = "/data/input"; our $ARBORCSTDIR = "/data/output/ARBOR"; our $ARBORCSVDIR = "/data/output/ARBORCSV"; our $RAWCSVDIR = "/data/output/RAWCSV"; our $FMSDIR = "/data/output/FMS"; our $INBILLDIR = "/data/output/INBILL"; our $ERRORDIR = "/data/error"; our $FILTERDIR = "/data/filter"; our $ARCHIVEDIR = "/data/archive"; our $DUPLICATEDIR = "/data/duplicate"; our $WORKDIR = "/data/work"; our $LOOKUPDIR = "/data/lookup";
      ############################################################ #THIRD script: open_file.pl #!/usr/bin/perl use strict; use warnings; sub CREATE_FILTER_HEADER { print (FILTER "FILTER CODE,STATUS,ANUM,BNUM,STARTDATE,DURATION,MBI +\n"); } sub CREATE_ERROR_HEADER { print (ERROR "ERROR CODE,STATUS,ANUM,BNUM,STARTDATE,DURATION,MBI\n +"); } sub CREATE INBILL_HEADER { my ($tmp); print (INBILL "VERSION = 2\n"); printf (INBILL "SWI_ID = %s\n", $swname); $tmp = substr($fname,1,6); $tmp =~ s/\0+//g; printf (INBILL "FILE_SEQ_NBR = %06s\n", hextoint($tmp)); printf (INBILL "TOTAL_RECS = %s\n", $total_records); print (INBILL "FIRST_DTTM = $firstdate\n"); print (INBILL "LAST_DTTM = $lastdate\n"); printf (INBILL "ORIG_FILE_NAME = %s\n", $inbill_fname); printf (INBILL "ORIG_FILE_VERSION = %s\n", "UNKNOWN"); printf (INBILL "CONVERTED_REC_STATS = %s:$total_records\n", "UNCAT +EGORIZED"); printf (INBILL "REJECTED_REC_STATS = %s:0\n", "UNCATEGORIZED"); printf (INBILL "DISCARDED_REC_STATS = %s:0\n", "UNCATEGORIZED"); printf (INBILL "ADDED_REC_STATS = %s:0\n", "UNCATEGORIZED"); printf (INBILL "ADD_DUP_CHK_KEY = %06s\n", hextoint($tmp)); printf (INBILL "GEN_FIELD_NAME_1 = %s\n", "C_PTY_NBR"); printf (INBILL "GEN_FIELD_NAME_2 = %s\n", "C_PTY_NBR_TYPE_ID"); printf (INBILL "GEN_FIELD_NAME_3 = %s\n", "ST_CODE"); printf (INBILL "GEN_FIELD_NAME_4 = %s\n", "MBI"); print (INBILL "\"REC_KEY\",\"ORIG_REC_KEY\",\"REC_TYPE_ID\",\"DUR_ +SPAN_ID\",\"CALL_START_DTTM\",\"PRE_CONV_DUR\","); print (INBILL "\"CONV_DUR\",\"A_PTY_NBR\",\"A_PTY_NBR_TYPE_ID\",\" +A_PTY_CELL_ID\",\"A_PTY_IMSI\",\"A_PTY_MSRN\","); print (INBILL "\"B_PTY_NBR\",\"B_PTY_NBR_TYPE_ID\",\"B_PTY_CELL_ID +\",\"B_PTY_IMSI\",\"B_PTY_MSRN\",\"IN_TRG_ID\","); print (INBILL "\"OUT_TRG_ID\",\"CALL_STATUS_ID\",\"SUPP_SVC_ID\",\ +"PROV_CHG_AMT\",\"GEN_FIELD_1\",\"GEN_FIELD_2\","); print (INBILL "\"GEN_FIELD_3\",\"GEN_FIELD_4\",\"GEN_FIELD_5\"\n") +; } sub CREATE_ARBOR_HEADER { my ($recType, $creation_dt, $rec_size, $num_record, $application, +$version, $data_record, $default); $recType = "HDR"; $creation_dt = strftime "%Y%m%d%H%M%S", localtime; $rec_size = "0000"; $num_record = "0000000"; $application = "NV2:MCS R3.0"; $version = "01.00"; $data_record = "TE3"; printf (ARBORCSV "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", $recType, $creati +on_dt, $rec_size, $num_record, $application, $version, $data_record, +" ", " "); $recType = sprintf("%3s","HDR"); $creation_dt = sprintf ("%14s", strftime "%Y%m%d%H%M%S", localtime +); $rec_size = sprintf ("%s","0000"); $num_record = sprintf ("%s", "0000000"); $version = sprintf ("%5s", "01.00"); $data_record = sprintf ("%3s", "TE3"); $default = sprintf ("%12s", " "); printf (ARBORCST pack 'A3 A14 A4 A7 A16 A5 A3 A12 h1', $recType,$c +reation_dt,$rec_size,$num_record,"NV2:MCS R3.0",$version,$data_record +,$default,"A"); } sub CREATE_OUTPUT_FILE { my ($creation_dt, $tmpfile, $sw, $dt, $yy, $mm, $dd, $hh, $mi, $ss +); ($sw,$dt) = (split(/_/, $fname))[1,2]; ($yy,$mm,$dd,$hh,$mi,$ss) = (split(/-/, $dt))[0,1,2,3,4,5]; $creation_dt = "${yy}${mm}${dd}${hh}${mi}${ss}"; open (INPUT, "$INPUTDIR/$SWITCHNAME/$fname"); open (RAWCDR, "> $WORKDIR/$fname.csv"); $tmpfile = "${sw}.${creation_dt}.filter"; open (FILTER, "> $FILTERDIR/$SWITCHNAME$tmpfile"); CREATE_FILTER_HEADER(); $tmpfile = "${sw}.${creation_dt}.error"; open (ERROR, "> $ERRORDIR/$SWITCHNAME$tmpfile"); CREATE_ERROR_HEADER(); $tmpfile = sprintf ("%s.%s", $sw, $creation_dt); open (INBILL, "> $INBILLDIR/$SWITCHNAME$tmpfile"); CREATE_INBIL_HEADER(); $tmpfile = "${sw}.${creation_dt}.csa"; open (RAWCSV, "> $RAWCSVDIR/$SWITCHNAME$tmpfile"); $tmpfile = "${sw}.${creation_dt}.cst"; open (ARBORCSV, "> $ARBORCSV/$SWITCHNAME$tmpfile"); $tmpfile = "${sw}.${creation_dt}.data"; open (ARBORCST, "> $ARBORCST/$SWITCHNAME$tmpfile"); CREATE_ARBOR_HEADER(); $tmpfile = sprintf("CDR_${creation_dt}_${sw}_${creation_dt}.txt", +renameFMS($swname)); open (FMS, "> $FMSDIR/$SWITCHNAME$tmpfile"); } CREATE_OUTPUT_FILE();
        Please consider using a module like Config::Settings to store/load config data.

        If you are comfortable with YAML or JSON, config modules for those are available as well.

        The settings can be consumed by different programs you write.

        Since you seem to be on linux, installing the module is simply "cpan i Config::Settings" (You may need to elevate privileges before doing that).

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970127]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-08-30 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls