> -----Original Message----- > From: Rob Dixon [mailto:[EMAIL PROTECTED] > Sent: 23 July 2008 20:46 > To: beginners@perl.org > Cc: [EMAIL PROTECTED] > Subject: Re: Request for advice/suggestions.... > > > [EMAIL PROTECTED] wrote: > > > > I'm very new with PERL and have been given a task of doing some maint. > on an > > existing really big PERL program. What I'd like to ask is if the changes > I > > want to implement will work and ask for suggestions on how to make one > major > > change working with subroutines. > > > > The application makes use of a number of arrays that contain categories > and > > subcategories. What I'd like to do is to move these into a separate file > > (extras.pl) and reference it with a "require". These structures were in > the > > mainline program. Adding new categories involved editing the mainline > code, > > so my thought was it would be easier and less trouble- prone to put them > in a > > separate file to which new ones could be added easily. What I want to > add to > > the external "required" file are these objects. Can I do this pretty > > transparently and easily? Will I run into any serious problems by moving > them > > to a separate file? > > > > ------------------------------------------------------------------------ > > use vars qw(%config %category %paper %records); > > > > %category = ( > > p01 => 'PAPER ITEMS GENERAL', > > p02 => 'Diaries and Journals', > > p03 => 'Indentures', > > p04 => 'Letters', > > p05 => 'Certificates', > > p10 => 'Other Paper Items', > > > > r01 => 'RECORDS GENERAL', > > r02 => 'Birth and Death', > > r03 => 'Marriage ', > > r04 => 'Wills ', > > r05 => 'Census', > > r06 => 'Court and Probate', > > r07 => 'Immigration and Ship Lists', > > r08 => 'Military', > > r09 => 'Maps', > > r10 => 'Other Records', > > ); > > > > %paper = ( > > p01 => 'PAPER ITEMS GENERAL', > > p02 => 'Diaries and Journals', > > p03 => 'Indentures', > > p04 => 'Letters', > > p05 => 'Certificates', > > p10 => 'Other Paper Items', > > ); > > > > %records = ( > > r01 => 'RECORDS GENERAL', > > r02 => 'Birth and Death', > > r03 => 'Marriage ', > > r04 => 'Wills ', > > r05 => 'Census', > > r06 => 'Court and Probate', > > r07 => 'Immigration and Ship Lists', > > r08 => 'Military', > > r09 => 'Maps', > > r10 => 'Other Records', > > ); > > > > ------------------------------------------------------------------------ > > > > Secondly, when additional categories are added, the program is coded > such > > that a new subroutine has to be inserted dealing with that category. For > > example, there would be a subroutine added for "paper" and one for > "records". > > In each subroutine there are only 3 "references" to the item. For > example, > > in the "paper" subroutine there are only these 3 places where "paper > stuff" > > is mentioned. Sample is below (with non-relevant code removed). So, if > we had > > 10 categories there would be ten subroutines - one for each category - > > differing only in the 3 places where the category topic is mentioned. My > > question - is it possible to create only one "generic" subroutine that > could > > have the topic name plugged in rather than having ten almost-alike > > subroutines? If so, can you recommend a sample code snippet that I can > model > > the procedure after? > > > > These subroutines are accessed by : > > > > elsif ($form{'action'} eq 'paper') { &paper; } #Paper Items Category > > elsif ($form{'action'} eq 'records') { &records; } #Records Category > > > > > > sub paper { > > &chkclose; > > print "<p align=center><b><font color=$config{'colortablebody'} > > face=Arial size=2>Paper Categories</font></b></td></tr>"; > > my $key; > > foreach $key (sort keys %paper) { > > }} > > > > > > sub records { > > &chkclose; > > print "<p align=center><b><font color=$config{'colortablebody'} > > face=Arial size=2>Records Categories</font></b></td></tr>"; > > my $key; > > foreach $key (sort keys %records) { > > }} > > I recommend that you modularize this and write a Categories.pm file that > defines > the data, and then access it with > > use Categories; > > in your main code. It would be nice if you could make this a bit object- > oriented > and make the hashes private to the Categories module, with exported > subroutines > that let the main code do what it wanted with them. But without seeing how > your > program is structured I can't say whether that's feasible or not. > > Your module would look like this. As it seemed that %category was just an > amalgamation of %paper and %records I've written it that way. > > > > > package Categories; > > use strict; > use warnings; > > require Exporter; > our @ISA = qw/Exporter/; > > our (%config, %category, %paper, %records); > our @EXPORT = qw(%config %category %paper %records); > > %paper = ( > p01 => 'PAPER ITEMS GENERAL', > p02 => 'Diaries and Journals', > p03 => 'Indentures', > p04 => 'Letters', > p05 => 'Certificates', > p10 => 'Other Paper Items', > ); > > %records = ( > r01 => 'RECORDS GENERAL', > r02 => 'Birth and Death', > r03 => 'Marriage ', > r04 => 'Wills ', > r05 => 'Census', > r06 => 'Court and Probate', > r07 => 'Immigration and Ship Lists', > r08 => 'Military', > r09 => 'Maps', > r10 => 'Other Records', > ); > > %category = (%paper, %records); > > 1; > > > > > As for generalizing your category subroutines, again a lot depends on the > scope > of the data and subroutines involved ( chkclose(), for instance, and > anything > else you've edited out. But you could write something like this. > > > > > sub _process_category { > > my ($hash, $text) = @_; > > chkclose(); > > print <<HTML; > <p align=center> > <b> > <font color=$config{colortablebody} face=Arial size=2> > $text > </font> > </b> > </td> > </tr> > HTML > > foreach my $key (sort keys %$hash) { > } > } > > sub paper { > _process_category(\%paper, 'Paper Categories'); > } > > sub records { > _process_category(\%records, 'Records Categories'); > } > > > > > But once again it would be best if these subroutines were exported from > the > Categories module and the data itself was kept private. Only you know what > is > possible here. > > HTH, > [Stewart Anderson] May be a bit late for this but, you could also use Config::Simple to achieve this. I know its intended for config files but it would lend itself quite well to doing what you want. The real benefit is that you would not need to edit Perl at all to add new entries to categories or entire categories, you could just edit your config file.
Couple of simple examples below. The Config::Simple docs describe lots more functionality that you could use. Stick the commented section into categories.cfg (uncommented of course) #! /usr/bin/perl use warnings; use strict ; use Data::Dumper; use Config::Simple; my $catcfg = new Config::Simple('categories.cfg'); print "P01:\t " . $catcfg->param('paper.p01') . "\n"; Config::Simple->new('categories.cfg')->import_names(); print "r01:\t " . our $RECORDS_R01 . "\n"; #[paper] #p01 = PAPER ITEMS GENERAL #p02 = Diaries and Journals #p03 = Indentures #p04 = Letters #p05 = Certificates #p10 = Other Paper Items # # #[records] #r01 = RECORDS GENERAL #r02 = Birth and Death #r03 = Marriage #r04 = Wills #r05 = Census #r06 = Court and Probate #r07 = Immigration and Ship Lists #r08 = Military #r09 = Maps #r10 = Other Records Information in this email including any attachments may be privileged, confidential and is intended exclusively for the addressee. The views expressed may not be official policy, but the personal views of the originator. If you have received it in error, please notify the sender by return e-mail and delete it from your system. You should not reproduce, distribute, store, retransmit, use or disclose its contents to anyone. Please note we reserve the right to monitor all e-mail communication through our internal and external networks. SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and are used under licence. British Sky Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are direct or indirect subsidiaries of British Sky Broadcasting Group plc (Registration No. 2247735). All of the companies mentioned in this paragraph are incorporated in England and Wales and share the same registered office at Grant Way, Isleworth, Middlesex TW7 5QD. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/