Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(code)) CGI.pm - CSS, tables... (deprecated by node 50167)

by ybiC (Prior)
on Aug 13, 2000 at 10:08 UTC ( [id://27683]=sourcecode: print w/replies, xml ) Need Help??
Category: CGI
Author/Contact Info ybiC
Description: ## deprecated by (code) Toy Template ##

A simple script that uses CGI::Pretty to create an HTML document with CSS (Cascading Style Sheet) and tables.

Also includes stupid pet tricks like presenting server info and client info in generated page.
    server info: name, webserver, Perl ver, OS, IP address, date/time, script modified date/time
    client info: name, IP address, browser

Constructive criticism absolutely welcome.
Pod at bottom of script.

Update: Thanks to zdog, davorg, and tye for pointing out redundancy of creating package variables and lexical variables with same names, and for insight into applications of each.

Update 2: Changed "background:" to "background-color:" in CSS section A:hover so all CSS1 browsers display properly.

#!/usr/bin/perl -wT


# specify Perl modules
use strict;
use CGI::Carp qw(fatalsToBrowser);      # only for debugging
use CGI::Carp;
use CGI::Pretty qw(:all);
use Time::localtime;
use File::stat;


# declare lexical variables
my $type    = 'text/html';              # HTML document type
my $expires = 'now';                    # browser page expiration
my $css   = './ybiC.css';               # Cascading Style Sheet
my $title = 'CGI, CSS and Tables';      # HTML page title
my $tm    = localtime;                  # current host time
my $file  = '/var/www/htdocs/ybiC.cgi'; # this file
my $mod   = ctime(stat($file)-> mtime); # most recent modified time


# generate HTML header
print header(
    -type       => "$type",
    -expires    => "$expires",
),
start_html(
    -style      => {-src => "$css"},
    -title      => $title,
);



# top row
print table ({
    bgcolor     => 'orange',
    width       => '550',
    align       => 'center',
    cellspacing => '0',
    cellpadding => '10',
    border      => '0'
},
    br,
    Tr ({align=> 'center', valign=> 'top'},
        td({align => 'center', valign=> 'top',},
            span({-class=>'big'},
                ($ENV{'SERVER_NAME'})), br,
            span({-class=>'small'},
                $ENV{'SERVER_SOFTWARE'}, br,
                "Perl", "$]", "   ",
                "$^O", "   ",
                $ENV{'SERVER_ADDR'}),
            ),    # close td
        ),        # close Tr
    );            # close table



# Second row
print table({
    bgcolor     => '#cc3333',
    width       => '550',
    align       => 'center',
    cellspacing => '0',
    cellpadding => '10',
    border      => '0',
},
    Tr({align=> 'center', valign=> 'top',},
        td({align => 'center', valign=> 'top',},
            # most recently modified date, time
            span({-class=>'small'},
                $tm -> mon+1, '-', $tm-> mday, '-', $tm-> year+1900,
                "      ",
                $tm-> hour, ':', $tm-> min, ':', $tm-> sec, br,
                'updated', ($mod)),

            ),    # close td
        ),        # close Tr
    );            # close table





# Third row
print table({
    bgcolor     => 'green',
    width       =>'550',
    align       => 'center',
    cellspacing =>'0',
    cellpadding =>'10',
    border      =>'0',
},
    Tr({align=> 'center', valign=> 'top',},
        # Left column
        td({align=> 'center', valign=> 'top',},
                a({href=> 'http://www.openbsd.org/'}, em('OpenBSD')),
            br, a({href=> 'http://www.openbsd.org/art/'}, em('OpenBSD 
+art')),
            br, a({href=> 'http://www.openbsd.org/cgi-bin/man.cgi?'}, 
+em('OpenBSD mans')),
            br, a({href=> 'http://www.openbsd.org/cgi-bin/man.cgi?quer
+y=afterboot&sektion=&apropos=0&m anpath=OpenBSD+Current&title=New Sys
+tem afterboot'}, em('OpenBSD afterboot')),
            br, a({href=> 'http://www.openbsd.org/security.html'}, em(
+'OpenBSD security')),
            ),    # close td
        # Right column
        td({ bgcolor => 'cc3333', align=> 'center', valign=> 'top',},
                a({href=> 'http://www.w3.org/Style/CSS/'}, 'w3c CSS'),
            br, a({href=> 'http://search.cpan.org/search?dist=CSS-Pars
+er'}, 'CSS::Parser'),
            br, a({href=> 'http://stein.cshl.org/WWW/software/CGI/cgi_
+docs.html'}, 'Stein on CGI'),
            br, a({href=> 'http://search.cpan.org/search?dist=CGI.pm'}
+, 'CGI.pm v2.70'),
            br, a({href=> 'http://search.cpan.org/'}, 'Search CPAN'),
            ),    # close td
        ),        # close Tr
    );            # close table



# Fourth row
print table ({
    bgcolor     => 'green',
    width       => '550',
    align       => 'center',
    cellspacing => '0',
    cellpadding => '10',
    border      => '0'
},
    Tr ({align=> 'center', valign=> 'top'},
        td({valign=> 'top'},
            a({href=> 'http://www.perlmonks.org/index.pl'}, 'Monastery
+'),
            "   ",
            a({href=> 'http://www.perlmonks.org/index.pl?node_id=979'}
+, 'Fearless Leader'),
            "   ",
            a({href=> 'http://slashdot.org/'}, 'Barbarians'),
            ),    # close td
        ),        # close Tr
    );            # close table



# Fifth row
print table ({
    bgcolor     => 'gray',
    width       => '550',
    align       => 'center',
    cellspacing => '0',
    cellpadding => '10',
    border      => '0'
},
    Tr ({align=> 'center', valign=> 'top'},
        td({align => 'center', valign=> 'top',},
        # remote browser & host info
            span({-class=>'small'},
                $ENV{'REMOTE_HOST'}, "     &n
+bsp;",
                $ENV{'REMOTE_ADDR'}, br,
                $ENV{'HTTP_USER_AGENT'}),
            ),    # close td
        ),        # close Tr
    );            # close table



print end_html;


=head1 NAME

ybiC.cgi

=head1 DESCRIPTION

Simple example of CGI using CSS (Cascading Style Sheets) and HTML tabl
+es

=head1 COMPATIBILITY

 Pass = Perl 5.006 on OpenBSD 2.7
 Fail = Perl 5.00404 on Debian 2.1
     (5.00404 CGI.pm has no CSS support)
     (CGI::Pretty not in 5.00404 std dist)

 Doesn't crash any browsers that I'm aware of

=head1 CSS SYNTAX

 The following example shows just a few parameters that CSS can contro
+l.
 Save to file and location spec'd by $css above
 Blank lines are *not* allowed between sections.

 body    {
    background-color:   midnightblue;
    font-family:        arial, geneva, helvetica, verdana, sans-serif;
    text-align:         center;
    margin:             0px 0px 0px 0px;
    }
 A:link    {
    text-decoration:    none;
    font-weight:        normal;
    line-height:        135%;
    color:              black;
    }
 A:visited    {
    text-decoration:    none;
    font-weight:        normal;
    line-height:        135%;
    color:              black;
    }
 A:active    {
    text-decoration:    none;
    font-weight:        normal;
    line-height:        135%;
    color:              black;
    }
 A:hover    {
    text-decoration:    none;
    font-weight:        normal;
    line-height:        135%;
    color:              yellow;
    background-color:   midnightblue;
    }
 .small    {
    font-size:          75%;
    line-height:        135%;
    }
 .big    {
    font-size:          150%;
    font-weight:        bold;
    line-height:        200%;
    }

=head1 TODO's

 pretty-ify file modified date - ctime(stat($file)->mtime)

=head2 Constructive criticism absolutely welcome

 Methods for TODO's above?
 Any benefits to changing style from functional to OO ?

=head1 SEE ALSO

 perldoc CGI
 perldoc CGI::Pretty
 http://search.cpan.org/search?dist=CGI.pm
 http://www.w3.org/Style/CSS/
 http://stein.cshl.org/WWW/software/CGI/cgi_docs.html
 http://search.cpan.org/search?dist=CSS-Parser

=head1 AUTHOR

[ybiC] www.perlmonks.org

=cut
Replies are listed 'Best First'.
(zdog) RE: CGI.pm for CSS, tables, pet tricks
by zdog (Priest) on Aug 13, 2000 at 10:41 UTC
    In the following code you have already declared the variables with use vars, therefore you do not need to declare them again with my:

    use vars qw($type $expires $css $title $tm $file $mod); # declare variables my $type = 'text/html'; # HTML document type my $expires = 'now'; # browser page expiration my $css = './ybiC.css'; # Cascading Style Sheet my $title = 'CGI, CSS and Tables'; # HTML page title my $tm = localtime; # current host time my $file = '/var/www/htdocs/ybiC.cgi'; # this file my $mod = ctime(stat($file)-> mtime); # most recent modified time

    So, if I am not mistaken, you can do it like so:

    use vars qw($type $expires $css $title $tm $file $mod); # declare variables $type = 'text/html'; # HTML document type $expires = 'now'; # browser page expiration $css = './ybiC.css'; # Cascading Style Sheet $title = 'CGI, CSS and Tables'; # HTML page title $tm = localtime; # current host time $file = '/var/www/htdocs/ybiC.cgi'; # this file $mod = ctime(stat($file)-> mtime); # most recent modified time

    I am guessing that that may speed things up slightly.

    Update to first suggestion: I Benchmarked both ways and found that your way is faster anyway. I guess that I was mistaken.

    Also, I don't know the reason for this but I keep getting a "Too late for "-T" option." error when I run it using ActivePerl.

    Good luck.

    Zenon Zabinski | zdog | zdog7@hotmail.com

      use vars and my create variables in two completely different ways. use vars will create package variables which live in the *main:: typeglob. my creates a completely separate set of lexical variables that just happen to have the same names.

      Having created the package variables, you then 'mask' them by declaring the lexical variables. The only way to accessthem would be to use the fully qualified variable name (like $main::type, etc.)

      In summary, you don't need both sets of declarations and, in fact, you're declaring one set of variables that you never use.

      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000, ICA, London
      <http://www.yapc.org/Europe/>
        What you say makes sense, davorg.   Do I eliminate the redundancy by removing "my" from each of the second declarations, like so?

        use vars qw($type $expires $css $title $tm $file $mod); $type = 'text/html'; $expires = 'now'; $css = './ybiC.css'; $title = 'CGI, CSS and Tables'; $tm = localtime; $file = '/var/www/htdocs/ybiC.cgi'; $mod = ctime(stat($file)-> mtime);

        I like "use vars qw() for keeping me mindful of all the script's variables.   But if there are reasons for pitching it and using "my", please enlighten me.

        Update: Oops. Had I re-read zdog's reply above, I would have asked this question differently.   <sheepish grin>
            cheers,
            ybiC

RE: CGI.pm for CSS, tables, pet tricks
by Maclir (Curate) on Aug 14, 2000 at 02:36 UTC
    While using taint is a good thing, if you are running with mod_perl, the -T switch does not work. Basically, by the time your perl script runs, it is too late to switch tainting on. There is a special Apache mod_perl directive to be used if you want taint checking:
    PerlTaintCheck On
    However, I believe ALL perl scripts that then run in Apache are running with taint switched on.

    Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-06-17 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.