CGI-FormMagick

 view release on metacpan or  search on metacpan

lib/CGI/FormMagick/Utils.pm  view on Meta::CPAN

#!/usr/bin/perl -w 
#
#
# FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org>
# Copyright (c) 2000-2002 Mitel Networks Corporation
# This software is distributed under the same licenses as Perl itself;
# see the file COPYING for details.

#
# $Id: Utils.pm,v 1.32 2003/02/05 17:18:37 anoncvs_gtkglext Exp $
#

package    CGI::FormMagick;

use Text::Template;

use strict;
use Carp;

=pod 

=head1 NAME

CGI::FormMagick::Utils - utility routines for FormMagick

=head1 SYNOPSIS

  use CGI::FormMagick;

=head1 DESCRIPTION

=head2 debug_msg($msg)

The debug method prints out a nicely formatted debug message.  It can 
be called from your script as C<$f->debug_msg($msg)>

=begin testing

BEGIN: {
    use vars qw( $fm );
    use lib "./lib";
    use CGI::FormMagick;
}

ok($fm = CGI::FormMagick->new(type => 'file', source => "t/simple.xml"), "create fm object");
$fm->parse_xml(); # suck in structure without display()ing

=end testing


=cut

sub debug_msg {
    my $self = shift;
    my $msg = shift;
    my ($sub, $line) = (caller(1))[3,2];
    print qq(<p class="debug">$sub $line: $msg</p>) if $self->{debug};
}

=head2 $fm->get_page_by_name($name)

get a page given the name attribute.  Returns the numeric index of
the page, suitable for $wherenext.

=for testing
is($fm->get_page_by_name('Personal'), 0, "get page by name");

=cut

sub get_page_by_name {
    my ($self, $name) = @_;

    for (my $i = 0; $i < scalar(@{$self->{xml}->{pages}}); $i += 1) { 
        return $i if $self->{xml}->{pages}->[$i]->{name} eq $name;
    }
    return undef;   # if you can't find that page   
}

=pod

=head2 $fm->get_page_by_number($page_index)

Given a page index, return a hashref containing the page's data.
This is just a convenience function.

=for testing
is(ref($fm->get_page_by_number(0)), 'HASH', "get page by number");

=cut

sub get_page_by_number {
    my ($self, $pagenum) = @_;
    return $self->{xml}->{pages}->[$pagenum];
}

=pod

=head2 pop_page_stack($self)

pops the last page off the stack of pages a user's visited... used
when the user clicks "Previous"

removes the last element from the stack (modifying it in place in
$self->{page_stack}) and returns the element it removed.  eg: 

    # if the CGI "pagestack" parameter is "1,2,3,5"...
    my $page = $self->pop_page_stack();
    $self->{page_stack} will be 1,2,3
    $page will be 5

=begin testing

local $fm->{page_stack} = "0,1,2,3";
my $p = $fm->pop_page_stack();
is($p, 3, "Pop page stack return value");
is($fm->{page_stack}, "0,1,2", "Pop page stack changes stack");



( run in 2.099 seconds using v1.01-cache-2.11-cpan-98e64b0badf )