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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call subroutines from a data file? 1

Status
Not open for further replies.

Porshe

Programmer
Jun 5, 2001
38
US
I have a very long script. I broke each function up with subroutines. I have another file that's going to call only the subroutine that's needed and pass it the data file. How can I do it as the first line in the data file?
I also read in the data into an array then close the file. Will I have to chomp or do something to the first line of the data so that it doesn't read in my request for the subroutine? OR should it?
I'm new to perl so I hope this make sense???

Thanks,
Porshe
 
Are you saying that you want the first record in the data file to contain the name of the subroutine to be called?
If that is the case, you could do something like this (after the data is loaded into the array):
Code:
# Remove the subroutine name from the data array
$subname = shift @array;
# Remove the end-of-line char from the name
chomp $subname;
# Call the subroutine
&$subname();
You could also pass a reference to the data array instead of relying on it being a global variable. But that would require recoding your subroutines. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I have a sub called list_by_acl and I want to put it as the first line in my data file like this:

list_by_acl(101, \%acl,$t_results)
Monmouth,2000-05-20 00:00:09-04,192.35.75.69,138,192.100.255,66,2,101,1234

So, I have this in my main script:
my $datafile = 'silly.dat'; #file for data
my $t_results = 't_results.html'; #file for generated mainreport
#my $t_results= $read.$acl_number;
open DFILE, $datafile or warn "Can't open $datafile: $!\n";
my @data=<DFILE>; #read data into an array

# Remove the subroutine name from the data array
$list_by_acl = shift @array;
# Remove the end-of-line char from the name
chomp $list_by_acl;
# Call the subroutine
&$list_by_acl();
close (DFILE);

sub list_by_acl { #then my routine script is here}
But i get an error:
Undefined subroutine &main:: called at new2.pl line 19, <DFILE> line 9.
#my sub has 3 arguments passed in it.
 
Try replacing this line:
Code:
&$list_by_acl();
With this line:
Code:
eval($list_by_acl);
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top