Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

virtual tabbing module for CGI

by George_Sherston (Vicar)
on Oct 19, 2001 at 15:31 UTC ( [id://119941]=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info George_Sherston
Description: update: I've altered this module in various ways to make it meet my needs better. The latest version is here

Reading this thread reminded me that often I don't want a particularly fancy table to display my lists of info, I just want the same functionality you get in a word-processor with the tab key. So I wrote this to provide just that functionality in an intuitive way. You kick it off with the start_tabs(100,250,400,450) function, for which you specify as many tab stops as you like, in pixels. Then you put in your text, with a t at each point you want a tab. The tabs wrap round, so that you go back to zero after you've used up all your tab stops, but if you want to wrap before, use r. Finally at the end end_tabs shuts everything down. It's important to use this function because the whole thing is translated into tables, and this is the function that provides the </table> tag. The only difference between this and the kind of tabbing you do in your word processor is that, because it's done with tables, your text wraps between tabs - which I think is an advantage.

Here's an example gobbet (you can see the output from this here:
#!/usr/bin/perl -w use strict; # path to my module library: use lib '/home/htdocs/hosted/thinweb/lib/'; use CGI qw(:standard); use CGI::tab; my %data = ( foo => ['bimble','bumble','wimble'], bar => ['nimble','numble','rumble'], baz => ['zimble','zumble','gomble'], ); print header, start_html, h1('CGI::tab demo'), hr, h2('print out text from a hash:'), start_tabs(150,300); for my $key (keys %data) { print $_,t for @{ $data{$key} }; } print end_tabs, hr, h2('arbitrary text to show wrapping capability:'), start_tabs(100,250,350,400,450), 'hello',t,'everyone',t,'are',r, t,t,t,t,t,'we',t, 'having',t,t,'fun',t,'yet?', end_tabs, end_html;
There's no rocket science here, but I find it a useful labour saver in a situation that often comes up. I'd value your comments on how to improve it, particularly from the point of view of making it efficient as a module.
package CGI::tab;

require Exporter;
our @ISA = ("Exporter");
my @tabs;
my $posn = 0;

sub start_tabs {
    my $sum = 0;
    for (@_) {
        push @tabs, ($_ - $sum);
        $sum += $tabs[$#tabs];
    }
    my $output = '<table cellpadding=0 cellspacing=0 border=0><tr heig
+ht=0>';
    $output .= "<td width=$_></td>" for @tabs;
    $output .= '<td></td></tr><tr><td>';
    return $output;
}

sub t {
    if ($posn == @tabs) {
        $posn = 0;
        return '</td></tr><tr><td>';
    }
    else {
        $posn ++;
        return '</td><td>';
    }
}

sub r {
    my $colspan = @tabs - $posn;
    $posn = 0;
    if ($colspan) {
        return "</td><td colspan=$colspan></td></tr><tr><td>";
    }
    else {
        return '</td></tr><tr><td>';
    }
}

sub end_tabs {
    my $colspan = @tabs - $posn;
    undef @tabs;
    $posn = 0;
    if ($colspan) {
        return "</td><td colspan=$colspan></td></tr></table>";
    }
    else {
        return '</td></tr></table>';
    }
}

our @EXPORT = qw/start_tabs t r end_tabs/;

1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://119941]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found