File Coverage

blib/lib/Spreadsheet/Edit/Preload.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod n/a
total 42 42 100.0


line stmt bran cond sub pod time code
1             # License: http://creativecommons.org/publicdomain/zero/1.0/
2             # (CC0 or Public Domain). To the extent possible under law, the author,
3             # Jim Avera (email jim.avera at gmail) has waived all copyright and
4             # related or neighboring rights to this document. Attribution is requested
5             # but not required.
6              
7 1     1   18031 use strict; use warnings FATAL => 'all'; use feature qw/say state/;
  1     1   3  
  1     1   40  
  1         7  
  1         1  
  1         50  
  1         6  
  1         2  
  1         109  
8 1     1   8 use utf8;
  1         2  
  1         9  
9              
10             package Spreadsheet::Edit::Preload;
11              
12             # Allow "use <thismodule. VERSION ..." in development sandbox to not bomb
13 1     1   80 { no strict 'refs'; ${__PACKAGE__."::VER"."SION"} = 998.999; }
  1         2  
  1         108  
14             our $VERSION = '1000.009'; # VERSION from Dist::Zilla::Plugin::OurPkgVersion
15             our $DATE = '2023-09-23'; # DATE from Dist::Zilla::Plugin::OurDate
16              
17 1     1   7 use Carp;
  1         2  
  1         84  
18 1     1   9 use Import::Into;
  1         3  
  1         296  
19             require Spreadsheet::Edit;
20             our @CARP_NOT = ('Spreadsheet::Edit');
21              
22             sub import {
23 2     2   8614 my $pkg = shift; # that's us
24              
25 2         34 my $callpkg = caller($Exporter::ExportLevel);
26              
27             # Import Spreadsheet::Edit and the usual variables for the user
28 2         20 Spreadsheet::Edit->import::into($callpkg, ':all');
29              
30             # Load the spreadsheet
31 2 100       5271 my $opthash = ref($_[0]) eq "HASH" ? shift(@_) : {};
32            
33             # Create new sheet, setting verbose, etc. from options
34             my $sh = Spreadsheet::Edit->new(
35 2 100       6 map{ exists($opthash->{$_}) ? ($_ => delete $opthash->{$_}) : () }
  6         25  
36             qw/verbose silent debug/
37             );
38              
39             # Read the content
40 2         11 $sh->read_spreadsheet($opthash, @_);
41              
42             # Tie variables in the caller's package
43 2         15 $sh->tie_column_vars({package => $callpkg}, ':all', ':safe');
44              
45             # Make it the 'current sheet' in the caller's package
46 2         13 Spreadsheet::Edit::sheet( {package => $callpkg}, $sh );
47             }
48              
49             1;
50             __END__
51             =pod
52              
53             =encoding utf8
54              
55             =head1 NAME
56              
57             Spreadsheet::Edit::Preload - load and auto-import column variables
58              
59             =head1 SYNOPSIS
60              
61             use Spreadsheet::Edit::Preload {OPTIONS}, PATH
62              
63             use Spreadsheet::Edit::Preload
64             {sheet => "Sheet1", title_rx => 2}, "/path/to/file.xls" ;
65              
66             apply {
67             say "row ",($rx+1)," has $FIRST_NAME $LAST_NAME";
68             };
69              
70             say "Row 4, column B contains ", $rows[3]{B};
71             say "Row 4: "First Name" is ", $rows[3]{"First Name"};
72             say "Row 4: "Last Name" is ", $rows[3]{Last_Name};
73             say "There are ", scalar(@rows), " rows of data.";
74             say "There are $num_cols columns";
75              
76             =head1 DESCRIPTION
77              
78             This is a wrapper for C<Spreadsheet::Edit> which loads a spreadsheet
79             at compile time. Tied variables are imported having names derived
80             from column titles or letter codes; these may be used during "apply"
81             operations to access the corresponding column in the "current row".
82              
83             The example above is equivalent to
84              
85             use Spreadsheet::Edit qw(:FUNCS :STDVARS);
86             BEGIN {
87             read_spreadsheet {sheet => "Sheet1"}, "/path/to/file.xls";
88             title_rx 2;
89             tie_column_vars ':all';
90             }
91              
92             You need not (and may not) explicitly declare the tied variables.
93              
94             =head1 OPTIONS
95              
96             The {OPTIONS} hashref is optional and may specify a workbook sheetname,
97             CSV parsing options, etc. (see I<read_spreadsheet> in L<Spreadsheet::Edit>).
98              
99             title_rx => ROWINDEX
100              
101             explicitly specifies the 0-based row index of the title row. If not
102             specified, the title row is auto-detected.
103              
104             =head1 SECURITY
105              
106             A fatal error occurs if a column letter ('A', 'B' etc.), a title,
107             or identifier derived from the title (any COLSPEC)
108             clashes with an object already existing in the caller's package,
109             or in package main.
110              
111             =head1 SEE ALSO
112              
113             Spreadsheet::Edit
114              
115             =cut