RE: Request for advice/suggestions....
- From: stewart.anderson@xxxxxxxxxxxxx (Stewart Anderson)
- Date: Thu, 24 Jul 2008 10:37:04 +0100
-----Original Message-----maint.
From: Rob Dixon [mailto:rob.dixon@xxxxxxx]
Sent: 23 July 2008 20:46
To: beginners@xxxxxxxx
Cc: Richard.Copits@xxxxxxxxxxxxxxx
Subject: Re: Request for advice/suggestions....
Richard.Copits@xxxxxxxxxxxxxxx wrote:
I'm very new with PERL and have been given a task of doing some
on anchanges
existing really big PERL program. What I'd like to ask is if the
Ione
want to implement will work and ask for suggestions on how to make
majorcategories
change working with subroutines.
The application makes use of a number of arrays that contain
andfile
subcategories. What I'd like to do is to move these into a separate
in(extras.pl) and reference it with a "require". These structures were
themainline
mainline program. Adding new categories involved editing the
code,them
so my thought was it would be easier and less trouble- prone to put
in ato
separate file to which new ones could be added easily. What I want
add tomoving
the external "required" file are these objects. Can I do this pretty
transparently and easily? Will I run into any serious problems by
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',
);
Forsuch
Secondly, when additional categories are added, the program is coded
that a new subroutine has to be inserted dealing with that category.
ifexample, there would be a subroutine added for "paper" and one for"records".
In each subroutine there are only 3 "references" to the item. Forexample,
in the "paper" subroutine there are only these 3 places where "paperstuff"
is mentioned. Sample is below (with non-relevant code removed). So,
we had-
10 categories there would be ten subroutines - one for each category
mentioned. Mydiffering only in the 3 places where the category topic is
thatquestion - is it possible to create only one "generic" subroutine
couldcan
have the topic name plugged in rather than having ten almost-alike
subroutines? If so, can you recommend a sample code snippet that I
modelcolor=$config{'colortablebody'}
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
thatface=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
definesobject-
the data, and then access it with
use Categories;
in your main code. It would be nice if you could make this a bit
orientedhow
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
youran
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
amalgamation of %paper and %records I've written it that way.the
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
scopefrom
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
thewhat
Categories module and the data itself was kept private. Only you know
is[Stewart Anderson]
possible here.
HTH,
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.
.
- References:
- Request for advice/suggestions....
- From: Richard Copits
- Re: Request for advice/suggestions....
- From: Rob Dixon
- Request for advice/suggestions....
- Prev by Date: Re: $hDATA1::YEAR -reg
- Next by Date: Re: XML::Simple Environment errors
- Previous by thread: RE: Request for advice/suggestions....
- Next by thread: How to convert the date ?
- Index(es):
Relevant Pages
|