Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl with external subroutines 1

Status
Not open for further replies.

chibaru

Programmer
Aug 24, 2005
13
US
Hi,
I hope someone can help me as I am completely lost. The platform is Windows.
I have a main.pl that will read in records from a file and print the customer numbers. I created a separate .pl that has common subroutines that will be used other Perl scripts. One subroutine defines the record fields, the other parses the input record.
The fields defined in the called subroutines are not accessible to the main.pl. Also, I don't think the record input is accessible to the called parsing subroutine.
I tried reading about packages, export/import but cannot get a clear understanding of it.
Any assistance will be greatly appreciated!!

Below are the main, external module and execution error.

Main.pl:
#
#-----------------------------------------------------------
# Set variables
#-----------------------------------------------------------
#
use warnings;
use strict;

my $dir = 'e:\test';
my $INFILE = "$dir/INFILE";
my $rec;
require "./include/INCL_test.pl";

#-----------------------------------------------------------
# Open files
#-----------------------------------------------------------
#
open (INFILE, "< $INFILE") or die print "Cannot open input file \n";

#----------------------------------------#
# file fields
#----------------------------------------#
#
defn_fields;

#----------------------------------------#
# Main routine
#----------------------------------------#
#
while ($rec = <INFILE>) {
chomp $rec;
$rec=~s/\"//g;
parse_rec;
print "Customer number $CUST_NO \n";
}

close (INFILE);
print "END OF SCRIPT \n";
exit;
#
# end of job
#

This has the common subroutines:
INCL_test.pl:
#----------------------------------------#
# file fields
#----------------------------------------#
#
#there are other fields but just defined one until it works
sub defn_fields {
our $CUST_NO;
}

#-----------------------------------------------------------
# Parse thru input fields
#-----------------------------------------------------------
#
sub parse_rec {
$CUST_NO = substr($rec,0,19);
}

1;

ERRORS:
STEP_010 Execute
Global symbol "$CUST_NO" requires explicit package name at E:\test\subtest.pl line 36.
Bareword "defn_fields" not allowed while "strict subs" in use at E:\test\subtest.pl line 24.
Bareword "parse_fields" not allowed while "strict subs" in use at E:\test\subtest.pl line 34.
Execution of E:\test\subtest.pl aborted due to compilation errors.
STEP_010 Completed.

 
You need to parse your data in and out of your subroutines. Here's an example... not actually tested but should give you an idea. Now I used the same variable names inside and outside the subroutine.. but you don't have to do that.


my $teststuff = 1;
my $dataout = &testsub($teststuff);
if ($teststuff == 2) {
print "Blah Blah\n";
}

sub testub {
my $dataout;
my $teststuff = shift @_;
if ($teststuff == 1) { $teststuff = 2;}
return $teststuff;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Shucks...I was hoping there's a way to have it compiled into the main script like an include. Thank you anyway.
 
well you can do that.. but you just have to understand your data.

It would work if you removed strict.. but you shouldn't do that. You should get it to work.

We have subroutine files for all kinds of stuff that is common among all of our scripts (DB connections, little sorting techinques, etc). We just require the sub file and then do something like

my @sortedarray = superduperspecialsort(@olddata);



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travis is exactly right here - you can do it, but you shouldn't. The industry has spent the last thirty years moving away from languages that allow that sort of thing for a very good reason. Relying on a common set of global variables in a single namespace would be bad enough in one program. To consider doing it across a suite of programs linked by a set of common subroutines would be madness.

By using 'use strict' and passing the parameters in like Travis showed you, you protect yourself from all kinds of programming nastiness.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve

I actually had a type to.. so I corrected
Code:
my $teststuff = 1;
my $dataout = &testsub($teststuff);
if ($dataout == 2) {
 print "Blah Blah\n";
}

sub testsub {
 my $teststuff = shift @_;
 if ($teststuff == 1) { $teststuff = 2;}
 return $teststuff;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top