home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Datafile.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  5.1 KB  |  199 lines

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Template::Plugin::Datafile
  4. #
  5. # DESCRIPTION
  6. #
  7. #   Template Toolkit Plugin which reads a datafile and constructs a 
  8. #   list object containing hashes representing records in the file.
  9. #
  10. # AUTHOR
  11. #   Andy Wardley   <abw@kfs.org>
  12. #
  13. # COPYRIGHT
  14. #   Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
  15. #   Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
  16. #
  17. #   This module is free software; you can redistribute it and/or
  18. #   modify it under the same terms as Perl itself.
  19. #
  20. #----------------------------------------------------------------------------
  21. #
  22. # $Id: Datafile.pm,v 2.66 2004/01/13 16:20:38 abw Exp $
  23. #
  24. #============================================================================
  25.  
  26. package Template::Plugin::Datafile;
  27.  
  28. require 5.004;
  29.  
  30. use strict;
  31. use vars qw( @ISA $VERSION );
  32. use base qw( Template::Plugin );
  33. use Template::Plugin;
  34.  
  35. $VERSION = sprintf("%d.%02d", q$Revision: 2.66 $ =~ /(\d+)\.(\d+)/);
  36.  
  37. sub new {
  38.     my ($class, $context, $filename, $params) = @_;
  39.     my ($delim, $line, @fields, @data, @results);
  40.     my $self = [ ];
  41.     local *FD;
  42.     local $/ = "\n";
  43.  
  44.     $params ||= { };
  45.     $delim = $params->{'delim'} || ':';
  46.     $delim = quotemeta($delim);
  47.  
  48.     return $class->fail("No filename specified")
  49.     unless $filename;
  50.  
  51.     open(FD, $filename)
  52.     || return $class->fail("$filename: $!");
  53.  
  54.     # first line of file should contain field definitions
  55.     while (! $line || $line =~ /^#/) {
  56.     $line = <FD>;
  57.     chomp $line;
  58.         $line =~ s/\r$//;
  59.     }
  60.  
  61.     (@fields = split(/\s*$delim\s*/, $line)) 
  62.     || return $class->fail("first line of file must contain field names");
  63.  
  64.     # read each line of the file
  65.     while (<FD>) {
  66.     chomp;
  67.     s/\r$//;
  68.  
  69.     # ignore comments and blank lines
  70.     next if /^#/ || /^\s*$/;
  71.  
  72.     # split line into fields
  73.     @data = split(/\s*$delim\s*/);
  74.  
  75.     # create hash record to represent data
  76.     my %record;
  77.     @record{ @fields } = @data;
  78.  
  79.     push(@$self, \%record);
  80.     }
  81.  
  82. #    return $self;
  83.     bless $self, $class;
  84. }    
  85.  
  86.  
  87. sub as_list {
  88.     return $_[0];
  89. }
  90.  
  91.  
  92. 1;
  93.  
  94. __END__
  95.  
  96.  
  97. #------------------------------------------------------------------------
  98. # IMPORTANT NOTE
  99. #   This documentation is generated automatically from source
  100. #   templates.  Any changes you make here may be lost.
  101. #   The 'docsrc' documentation source bundle is available for download
  102. #   from http://www.template-toolkit.org/docs.html and contains all
  103. #   the source templates, XML files, scripts, etc., from which the
  104. #   documentation for the Template Toolkit is built.
  105. #------------------------------------------------------------------------
  106.  
  107. =head1 NAME
  108.  
  109. Template::Plugin::Datafile - Plugin to construct records from a simple data file
  110.  
  111. =head1 SYNOPSIS
  112.  
  113.     [% USE mydata = datafile('/path/to/datafile') %]
  114.     [% USE mydata = datafile('/path/to/datafile', delim = '|') %]
  115.    
  116.     [% FOREACH record = mydata %]
  117.        [% record.this %]  [% record.that %]
  118.     [% END %]
  119.  
  120. =head1 DESCRIPTION
  121.  
  122. This plugin provides a simple facility to construct a list of hash 
  123. references, each of which represents a data record of known structure,
  124. from a data file.
  125.  
  126.     [% USE datafile(filename) %]
  127.  
  128. A absolute filename must be specified (for this initial implementation at 
  129. least - in a future version it might also use the INCLUDE_PATH).  An 
  130. optional 'delim' parameter may also be provided to specify an alternate
  131. delimiter character.
  132.  
  133.     [% USE userlist = datafile('/path/to/file/users')     %]
  134.     [% USE things   = datafile('items', delim = '|') %]
  135.  
  136. The format of the file is intentionally simple.  The first line
  137. defines the field names, delimited by colons with optional surrounding
  138. whitespace.  Subsequent lines then defines records containing data
  139. items, also delimited by colons.  e.g.
  140.  
  141.     id : name : email : tel
  142.     abw : Andy Wardley : abw@cre.canon.co.uk : 555-1234
  143.     neilb : Neil Bowers : neilb@cre.canon.co.uk : 555-9876
  144.  
  145. Each line is read, split into composite fields, and then used to 
  146. initialise a hash array containing the field names as relevant keys.
  147. The plugin returns a blessed list reference containing the hash 
  148. references in the order as defined in the file.
  149.  
  150.     [% FOREACH user = userlist %]
  151.        [% user.id %]: [% user.name %]
  152.     [% END %]
  153.  
  154. The first line of the file B<must> contain the field definitions.
  155. After the first line, blank lines will be ignored, along with comment
  156. line which start with a '#'.
  157.  
  158. =head1 BUGS
  159.  
  160. Should handle file names relative to INCLUDE_PATH.
  161. Doesn't permit use of ':' in a field.  Some escaping mechanism is required.
  162.  
  163. =head1 AUTHOR
  164.  
  165. Andy Wardley E<lt>abw@andywardley.comE<gt>
  166.  
  167. L<http://www.andywardley.com/|http://www.andywardley.com/>
  168.  
  169.  
  170.  
  171.  
  172. =head1 VERSION
  173.  
  174. 2.66, distributed as part of the
  175. Template Toolkit version 2.13, released on 30 January 2004.
  176.  
  177. =head1 COPYRIGHT
  178.  
  179.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  180.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  181.  
  182. This module is free software; you can redistribute it and/or
  183. modify it under the same terms as Perl itself.
  184.  
  185. =head1 SEE ALSO
  186.  
  187. L<Template::Plugin|Template::Plugin>
  188.  
  189. =cut
  190.  
  191. # Local Variables:
  192. # mode: perl
  193. # perl-indent-level: 4
  194. # indent-tabs-mode: nil
  195. # End:
  196. #
  197. # vim: expandtab shiftwidth=4:
  198.