REDROOM
PHP 8.2.30
Path:
Logout
Edit File
Size: 4.40 KB
Close
//usr/local/lib64/perl5/Template/Plugin/Datafile.pm
Text
Base64
#============================================================= -*-Perl-*- # # Template::Plugin::Datafile # # DESCRIPTION # Template Toolkit Plugin which reads a datafile and constructs a # list object containing hashes representing records in the file. # # AUTHOR # Andy Wardley <abw@wardley.org> # # COPYRIGHT # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. # # This module is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # #============================================================================ package Template::Plugin::Datafile; use strict; use warnings; use base 'Template::Plugin'; our $VERSION = '3.100'; sub new { my ($class, $context, $filename, $params) = @_; my ($delim, $encoding, $line, @fields, @data, @results); my $self = [ ]; local *FD; local $/ = "\n"; $params ||= { }; $delim = $params->{'delim'} || ':'; $delim = quotemeta($delim); $encoding = defined $params->{'encoding'} ? ':encoding('.$params->{'encoding'}.')' : ''; return $class->error("No filename specified") unless $filename; open(FD, '<'.$encoding, $filename) || return $class->error("$filename: $!"); # first line of file should contain field definitions while (! $line || $line =~ /^#/) { $line = <FD>; chomp $line; $line =~ s/\r$//; } (@fields = split(/\s*$delim\s*/, $line)) || return $class->error("first line of file must contain field names"); # read each line of the file while (<FD>) { chomp; s/\r$//; # ignore comments and blank lines next if /^#/ || /^\s*$/; # split line into fields @data = split(/\s*$delim\s*/); # create hash record to represent data my %record; @record{ @fields } = @data; push(@$self, \%record); } # return $self; bless $self, $class; } sub as_list { return $_[0]; } 1; __END__ =head1 NAME Template::Plugin::Datafile - Plugin to construct records from a simple data file =head1 SYNOPSIS [% USE mydata = datafile('/path/to/datafile') %] [% USE mydata = datafile('/path/to/datafile', delim = '|') %] [% USE mydata = datafile('/path/to/datafile', encoding = 'UTF-8') %] [% FOREACH record = mydata %] [% record.this %] [% record.that %] [% END %] =head1 DESCRIPTION This plugin provides a simple facility to construct a list of hash references, each of which represents a data record of known structure, from a data file. [% USE datafile(filename) %] A absolute filename must be specified (for this initial implementation at least - in a future version it might also use the C<INCLUDE_PATH>). An optional C<delim> parameter may also be provided to specify an alternate delimiter character. The optional C<encoding> parameter may be used to specify the input file encoding. [% USE userlist = datafile('/path/to/file/users') %] [% USE things = datafile('items', delim = '|') %] The format of the file is intentionally simple. The first line defines the field names, delimited by colons with optional surrounding whitespace. Subsequent lines then defines records containing data items, also delimited by colons. e.g. id : name : email : tel abw : Andy Wardley : abw@tt2.org : 555-1234 sam : Simon Matthews : sam@tt2.org : 555-9876 Each line is read, split into composite fields, and then used to initialise a hash array containing the field names as relevant keys. The plugin returns a blessed list reference containing the hash references in the order as defined in the file. [% FOREACH user = userlist %] [% user.id %]: [% user.name %] [% END %] The first line of the file B<must> contain the field definitions. After the first line, blank lines will be ignored, along with comment line which start with a 'C<#>'. =head1 BUGS Should handle file names relative to C<INCLUDE_PATH>. Doesn't permit use of 'C<:>' in a field. Some escaping mechanism is required. =head1 AUTHOR Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/> =head1 COPYRIGHT Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L<Template::Plugin> =cut # Local Variables: # mode: perl # perl-indent-level: 4 # indent-tabs-mode: nil # End: # # vim: expandtab shiftwidth=4:
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 20
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
Assert.pm
3.43 KB
lr--r--r--
2022-07-26 06:42:11
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Datafile.pm
4.40 KB
lr--r--r--
2022-07-26 06:42:21
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Date.pm
11.22 KB
lr--r--r--
2022-07-26 07:09:42
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Directory.pm
10.90 KB
lr--r--r--
2022-07-26 06:43:06
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Dumper.pm
3.66 KB
lr--r--r--
2022-07-25 18:57:15
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
File.pm
10.92 KB
lr--r--r--
2022-07-26 07:10:13
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Filter.pm
9.63 KB
lr--r--r--
2022-07-26 06:43:36
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Format.pm
1.79 KB
lr--r--r--
2022-07-26 06:43:45
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
HTML.pm
5.87 KB
lr--r--r--
2022-07-26 06:43:59
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Image.pm
11.43 KB
lr--r--r--
2022-07-26 07:10:56
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Iterator.pm
1.95 KB
lr--r--r--
2022-07-26 06:44:16
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Math.pm
3.99 KB
lr--r--r--
2022-07-26 06:44:36
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Pod.pm
1.61 KB
lr--r--r--
2022-07-26 06:44:44
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Procedural.pm
3.59 KB
lr--r--r--
2022-07-25 18:57:15
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Scalar.pm
3.67 KB
lr--r--r--
2022-07-26 06:45:02
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
String.pm
17.69 KB
lr--r--r--
2022-07-26 06:45:13
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Table.pm
12.41 KB
lr--r--r--
2022-07-26 07:13:22
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
URL.pm
5.67 KB
lr--r--r--
2022-07-26 07:13:38
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
View.pm
2.40 KB
lr--r--r--
2022-07-26 06:45:43
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Wrap.pm
3.18 KB
lr--r--r--
2022-07-26 06:45:52
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).