[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,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to