CGI::Form::Table::Reader - read a table of form inputs


CGI-Form-Table documentation Contained in the CGI-Form-Table distribution.

Index


Code Index:

NAME

Top

CGI::Form::Table::Reader - read a table of form inputs

VERSION

Top

version 0.161

 $Id: /my/cs/projects/formtable/trunk/lib/CGI/Form/Table/Reader.pm 27836 2006-11-11T04:19:45.102963Z rjbs  $

SYNOPSIS

Top

 use CGI;
 use CGI::Form::Table::Reader;

 my $query = CGI->new;
 my $form = CGI::Form::Table::Reader->new(query => $query, prefix => 'user');

 my $users = $form->rows;

DESCRIPTION

Top

METHODS

Top

CGI::Form::Table::Reader->new(query => $query, prefix => $prefix)

CGI::Form::Table::Reader->rows

Returns an arrayref of hashrefs from the CGI inputs with the given prefix.

AUTHOR

Top

Ricardo SIGNES, <rjbs@cpan.org>

BUGS

Top

Please report any bugs or feature requests through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT

Top


CGI-Form-Table documentation Contained in the CGI-Form-Table distribution.
package CGI::Form::Table::Reader;

use strict;
use warnings;

our $VERSION = '0.161';

sub new {
	my ($class, %args) = @_;
	return unless $args{prefix} and $args{query};
	bless \%args => $class;
}

sub rows {
	my ($self) = @_;

	my @positions = $self->_read_positions;
	return unless @positions;

	my @rows;
	push @rows, $self->_read_row($_) for @positions;

	\@rows;
}

sub _read_row {
	my ($self, $position) = @_;

	my $row_prefix = $self->{prefix} . '_' . $position . '_';

	my %row;
	for (grep { /^$row_prefix/ } $self->{query}->param) {
		(my $name = $_) =~ s/^$row_prefix//;
		$row{$name} = $self->{query}->param($_);
	}

	return \%row;
}

# _read_positions
#
# returns a list of the positions found in the params

sub _read_positions {
	my ($self) = @_;

	my %temp;
	my @positions =
		sort { $a <=> $b }
		grep { ! $temp{0+$_} ++ }
		map  { /^$self->{prefix}_(\d+)_/; }
		$self->{query}->param;
}

1;