kmullin5016 has asked for the wisdom of the Perl Monks concerning the following question:

I am struggling with methods in CGI.pm. I can find no tutorials that discuss the methods in detail. Specifically, I'm having problems with the following code:

print CGI->start_table(-border=>1, -cellspacing=>0, -cellpadding=>1);

From this it generates:

<table>

It seems to have ignored my request for border, cellspacing, cellpadding. What is wrong with the way I'm doing it, and can someone point me to a good tutorial that discusses these issues? http://stein.cshl.org/WWW/CGI/ doesn't, http://www.cgi101.com/book/ doesn't either.

Replies are listed 'Best First'.
Re: help with start_table of CGI.pm
by AltBlue (Chaplain) on Feb 01, 2007 at 19:37 UTC
    you missed attributes declaration:
    print CGI->start_table({-border=>1, -cellspacing=>0, -cellpadding=>1})

    This feature is documented is in CGI's manual (man CGI):

    A large number of routines in CGI.pm actually aren’t specifically defined in the module, but are generated automatically as needed. These are the "HTML shortcuts," routines that generate HTML tags for use in dynamically-generated pages. HTML tags have both attributes (the attribute="value" pairs within the tag itself) and contents (the part between the opening and closing pairs.) To distinguish between attributes and contents, CGI.pm uses the convention of passing HTML attributes as a hash reference as the first argument, and the contents, if any, as any subsequent arguments.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: help with start_table of CGI.pm
by GrandFather (Saint) on Feb 01, 2007 at 19:45 UTC

    Try wrapping your options up in a hash:

    print CGI::start_table({border=>'1', cellspacing=>'0', cellpadding=>'1 +'});

    Prints:

    <table cellspacing="0" cellpadding="1" border="1">

    See the CGI documentation - especially the 'PROVIDING ARGUMENTS TO HTML SHORTCUTS' section.

    If you are starting out with CGI then Ovid's CGI Course is a good starting point.

    You may find some of the Tutorials' section CGI Programming material helpful too.


    DWIM is Perl's answer to Gödel
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: help with start_table of CGI.pm
by ww (Archbishop) on Feb 01, 2007 at 19:52 UTC
    quoted from: http://search.cpan.org/src/LDS/CGI.pm-3.25/cgi_docs.html
    (stale; no updating since Feb 2005, but perhaps helpful anyway:
    To add attributes to an HTML tag, simply pass a reference to an associ +ative array as the first argument. The keys and values of the associa +tive array become the names and values of the attributes. For example +, here's how to generate an <A> anchor link: use CGI qw/:standard/; print a({-href=>"bad_art.html"},"Jump to the silly exhibit"); <A HREF="bad_art.html">Jump to the silly exhibit</A> You may dispense with the dashes in front of the attribute names if yo +u prefer: print img {src=>'fred.gif',align=>'LEFT'}; <IMG ALIGN="LEFT" SRC="fred.gif"> Sometimes an HTML tag attribute has no argument. For example, ordered +lists can be marked as COMPACT, or you wish to specify that a table h +as a border with <TABLE BORDER>. The syntax for this is an argument t +hat that points to an undef string: print ol({compact=>undef},li('one'),li('two'),li('three')); Prior to CGI.pm version 2.41, providing an empty ('') string as an att +ribute argument was the same as providing undef. However, this has ch +anged in order to accomodate those who want to create tags of the for +m <IMG ALT="">. The difference is shown in this table:
    CODERESULT
    img({alt=>undef})
    img({alt=>''})<IMT ALT="">