Best way to handle using a report template for non-programmers..

J

Johnny Google

I would like to create a report template and I am considering using one
of the modules already created to do this... I've been browsing through
the docs on CPAN and I see a few with different advantages. Here is my
specific case:

1) I don't really need the embedded portions to be actual perl because
I would rather it be more readable to someone who does not know perl.
2) I need to be able to handle looping (i.e. a parts list for an
assembly)
3) I need to be able to specifiy exact column position formatting ...
meaning I want to start info on at an exact position on a particular
line even after a variable on that same line has been resolved ...
For example:

012345678901234567890123456789012345678901234567890123456789
[DATE] [ENGINEER] [ASSEMBLY NAME]

I need [ASSEMBLY NAME] to start at the same position no matter
what the length of ENGINEER resolves to.


If I were to code this in perl -- I would just ust formatted printing
.... but I want to make this format changeable by a non-programmer - and
have it living in a template file. I have only browsed the TEXT
TEMPLATE/MAGIC modules at this point but it looks to me that the
formatting would not handle starting a variable at a certain position
down-the-line from a previously resolved variable.
Any hints?

Thanks,

John
 
J

Johnny Google

Another note: when I use formatted printing in my code .. I use printf,
sprintf, %this and %that, etc.
I have never used the bult-in perl FORMAT mechanism .... so perhaps
this could be included as something I should compare to using a module?
Not sure.... will it handle looping? Thanks for sharing any hints...
 
T

Tad McClellan

Johnny Google said:
I have never used the bult-in perl FORMAT mechanism


You won't be missing much if you keep it that way.

There are modules that are almost always better than formats anyway.
 
J

James Willmore

Johnny Google wrote:
1) I don't really need the embedded portions to be actual perl because
I would rather it be more readable to someone who does not know perl.
2) I need to be able to handle looping (i.e. a parts list for an
assembly)
3) I need to be able to specifiy exact column position formatting ...
meaning I want to start info on at an exact position on a particular
line even after a variable on that same line has been resolved ...
For example:

012345678901234567890123456789012345678901234567890123456789
[DATE] [ENGINEER] [ASSEMBLY NAME]

I need [ASSEMBLY NAME] to start at the same position no matter
what the length of ENGINEER resolves to.


If I were to code this in perl -- I would just ust formatted printing
.... but I want to make this format changeable by a non-programmer - and
have it living in a template file. I have only browsed the TEXT
TEMPLATE/MAGIC modules at this point but it looks to me that the
formatting would not handle starting a variable at a certain position
down-the-line from a previously resolved variable.
Any hints?

Not sure if this would help you, but you could try using 'pack' to
format the output.

for example (using the /etc/passwd file as input)
#!/usr/bin/perl

use strict;
use warnings;

open(PASS, '/etc/passwd')
or die "Can't open: $!\n";

while(<PASS>) {
chomp;
my @fields = (split /:/);
my $output = pack 'A10 A2 A6 A6 A20 A15 A15', @fields;
print "$output\n";
}


produces something like (output edited for security, brevity and
formatting in email client):
postfix x xx xx
/var/spool/post/sbin/nologin
mailman x xx xx GNU Mailing List Man/var/mailman
/sbin/nologin
amanda x xx xx Amanda user
/var/lib/amanda/bin/bash


'pack' is a Perl function, so someone who doesn't know Perl won't
know what 'pack' does. However, it is a basic Perl function, so
it's not hard to find information out about how to use it (perldoc
-f pack). It handles looping and the output remains constant (which
is what you wanted in requirements 2 and 3). Although, it does
truncate data and *you* have to be specific about what you want -
meaning, you have to adjust for spaces between columns of information.

Again, may not be what you're looking for, but it's just AWTDI.

HTH

Jim
 
J

James Willmore

James said:
Johnny Google wrote:
Not sure if this would help you, but you could try using 'pack' to
format the output.

for example (using the /etc/passwd file as input)
#!/usr/bin/perl

use strict;
use warnings;

open(PASS, '/etc/passwd')
or die "Can't open: $!\n";

while(<PASS>) {
chomp;
my @fields = (split /:/);
my $output = pack 'A10 A2 A6 A6 A20 A15 A15', @fields;
print "$output\n";
}
Again, may not be what you're looking for, but it's just AWTDI.

Just to expand on the example above and to separate out the "look
and feel" aspect from the "business logic" aspect, below may be more
along the lines of what you were looking for ... maybe.

First, define a "template" to use (which is nothing more than field
descriptions and what pack format to use):

--start--
username A10
password A9
uid A6
gid A6
ecos A20
home A15
shell A15
--end--

and then use the following script to apply the "template" to the
input file:

--start--
#!/usr/bin/perl

use strict;
use warnings;

#define your template and input file
#use the file 'template' and '/etc/passwd' as defaults
my $template = shift || 'template';
my $input = shift || '/etc/passwd';

#open tenplate file and read its contents

open(TEMPLATE, $template)
or die "Can't open template file: $!\n";

my @description;
my $packFormat;
while(<TEMPLATE>) {
chomp;
my($desc, $form) = (split);
push @description, $desc;
$packFormat .= "$form ";
}

close TEMPLATE;

#now open the actual file, read its contents and
#print according to the pack format

open(PASS, $input)
or die "Can't open password file: $!\n";

my $heading = pack $packFormat, @description;
print "$heading\n";

while(<PASS>) {
chomp;
my @fields = (split /:/);
my $output = pack $packFormat, @fields;
print "$output\n";
}

close PASS;
--end--

The above code needs more work (like using one of the Getopts
modules to parse the command line options, better handling of the
template file, etc.), but may be more along the lines of what you're
looking for.

Most modules I've seen requires *some* understanding of Perl.
However, the above *might* be the "poor man's" way of doing it so
anyone can edit the template file and not be too concerned with the
inner workings of Perl.

HTH

Jim
 
B

Brad Baxter

my($desc, $form) = (split);
my @fields = (split /:/);

Small nit to pick: you do know that the parens around split aren't needed?
They would be if you wanted to slice things up, but not otherwise.

1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 $_ = "ABC 123 UME";
6 my($desc, $form) = split;
7 print "($desc, $form)\n";
8
9 tr/ /:/;
10 my @fields = split /:/;
11 print "@fields\n";
12
13 @fields = (split /:/)[0,1]; # parens needed here
14 print "@fields\n";
15
16 __END__
17 (ABC, 123)
18 ABC 123 UME
19 ABC 123

Regards,

Brad
 
J

Johnny Google

Ok - perhaps I am missing something here -- what I was asking was a way
to handle the actual template ... i am not quite sure how this code
would help with generating formatted ascii reports with a template?
What is a sample Template file in your case?


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top