Gossamer Forum
Home : General : Perl Programming :

How to use ?

Quote Reply
How to use ?
I'm learning perl and I'm making a homepage in cgi.
I want to have several pages in one script and I want to get the address index.cgi?aboutme or index.cgi?links
Can somebody help me with that??
Please!!
Quote Reply
Re: [perlman] How to use ? In reply to
Well, if you CGI.pm, you can easily use the following types of codes:

Quote:

print $in->header();
if ($in->param('about')) {
&aboutme;
}
if ($in->param('links')) {
&links;
}


Then create some sub-routines (like links and aboutme. In these subs you can insert HTML codes using print qq|HTML CODES|; or you could try using a TEMPLATE parser and make your system template-based.

OR you could simply purchase a license for any of the GT products, since most of them do allow you to dynamically build pages as you are attempting to, rather than re-creating the wheel.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [perlman] How to use ? In reply to
Depending on your "newbiness" you may wish to use $ENV{QUERY_STRING} instead of jumping in with CGI.pm

Heres a basic example script:

Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";

if ($ENV{QUERY_STRING} eq 'aboutme') {
&aboutme;
}
else {
&main;
}

sub main {
print "This is the main sub";
}

sub aboutme {
print "This is the about me sub";
}

Then you can use script.cgi?aboutme


Last edited by:

RedRum: Nov 3, 2001, 1:58 PM
Quote Reply
Re: [perlman] How to use ? In reply to
Yes, if you prefer to use simpler Perl codes, that is fine...

Although one suggestion is that you do not use print "";...you would then have to escape all reserved Perl codes.

That is why using qq||; operators is more efficient and less stress in coding.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Chewbaca] How to use ? In reply to
Reserved perl codes?

The only character you can't use with print "" without escaping is "

One other is @ but you have to escape that with everything other than ' ' anyway.

Last edited by:

RedRum: Nov 3, 2001, 3:27 PM
Quote Reply
Re: [RedRum] How to use ? In reply to
Well, Paul, if the user is going to create web pages out of Perl, then there will definitely be "" for link and email links, not to mention table codes and basically most HTML codes.

Thus, it is much more efficient to use qq||; where you do NOT have to escape any characters.

The operators you chose ""; are not efficient and also headache for coding HTML within Perl scripts.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Chewbaca] How to use ? In reply to
Well actually the prefered choice would be qq~ ~; if we are going to be fussy as that is one of the most least likely characters to occur in html pages

| is often used in menus, like in the Links2 default templates.

Last edited by:

RedRum: Nov 3, 2001, 7:36 PM
Quote Reply
Re: [RedRum] How to use ? In reply to
Okey.
I tried to use the code RedRum posted:
#!/usr/bin/perlprint
"Content-type: text/html\n\n";
if ($ENV{QUERY_STRING} eq 'aboutme') { &aboutme;
}
else {
&main;
}sub main {
print "This is the main sub";
}
sub aboutme {
print "This is the about me sub";
}

--------------------------------------------

It worked fine, but I want to have a hole page inside one sub.
Can I do something like this: (??)


Okey.
I tried to use the code RedRum posted:
#!/usr/bin/perlprint
"Content-type: text/html\n\n";
if ($ENV{QUERY_STRING} eq 'aboutme') { &aboutme;
}
else {
&main;
}sub main {
print "This is the main sub";
}
sub aboutme {
print " &aboutmehtml ";
}

sub aboutmehtml {
print ~
<html>
<body>
Welcome to the page about me!
</body>
</html>
~;
}

--------------------

And maybe have more pages who have the address ?something ...
Is that possible??

Quote Reply
Re: [perlman] How to use ? In reply to
Yes definitely.

Just keep adding elsif's to the code like:

Code:
if ($ENV{QUERY_STRING} eq 'aboutme') {
&aboutme;
}
elsif ($ENV{QUERY_STRING} eq 'aboutmehtml') {
&aboutmehtml;
}
elsif ($ENV{QUERY_STRING} eq 'aboutyou') {
&aboutyou;
}
else {
&main;
}

You can't do this though:

sub aboutme {
print " &aboutmehtml ";
}

....you either need to delete that sub and at the top in the elsif change &aboutme to &aboutmehtml or do:

sub aboutme {
&aboutmehtml;
}

Last edited by:

RedRum: Nov 4, 2001, 4:13 AM
Quote Reply
Re: [RedRum] How to use ? In reply to
I tried it, but I keep getting errors!
Here's the code I used:
#!/usr/bin/perl
if ($ENV{QUERY_STRING} eq 'aboutme') {
&aboutme;
}
elsif ($ENV{QUERY_STRING} eq 'pictures') {
&pictures;
}
elsif ($ENV{QUERY_STRING} eq 'guestbook') {
&guestbook;
}
else {
&main;
}
sub main {
print ~
Welcome to my page! <br>
Here is what you can find in my page:<br>
<A HREF=?aboutme>About me</A> - <A HREF=?pictures>Pictures</A><br>
<A HREF=?guestbook>Guestbook</A>
~;
}
sub aboutme {
&aboutmehtml;
}

sub aboutmehtml {
print ~
<html>
<body>
Welcome to the page about me!<br>
Here you'll find lots of information about me!<br>
Isen't that great?<br>
My name is Foo Bar<br>
I live in a small town in Norway.
</body>
</html>
~;
}
sub pictures {
&pictureshtml;
}

sub pictureshtml {
print ~
<html>
<body>
Welcome to the picture page!<br>
Here's a pictures of me:<br>
</body>
</html>
~;
}
sub guestbook {
&guestbookhtml;
}

sub guestbookhtml {
print ~
<html>
<body>
Say what you think about this page!
Fill out the form:
</body>
</html>
~;
}

What have I done wrong??

Last edited by:

Andy: Mar 29, 2011, 2:41 AM
Quote Reply
Re: [perlman] How to use ? In reply to
You need print qq~ not print ~

Also these types of links may not work...<A HREF=?pictures> .....you may need <A HREF=script.cgi?pictures>

You should really take a look at perldoc.com as well as hotscripts.com and also browse search engine results. They will give you lots and lots of helpful information if you are new to perl.
Quote Reply
Re: [RedRum] How to use ? In reply to
Still getting errors!!!
I'm confused....

#!/usr/bin/perl
if ($ENV{QUERY_STRING} eq 'aboutme') {
&aboutme;
}
elsif ($ENV{QUERY_STRING} eq 'pictures') {
&pictures;
}
elsif ($ENV{QUERY_STRING} eq 'guestbook') {
&guestbook;
}
else {
&main;
}
sub main {
print qq~
Welcome to my page! <br>
Here is what you can find in my page:<br>
<A HREF=page.cgi?aboutme>About me</A> - <A HREF=page.cgi?pictures>Pictures</A><br>
<A HREF=page.cgi?guestbook>Guestbook</A>
~;
}
sub aboutme {
&aboutmehtml;
}

sub aboutmehtml {
print qq~
Welcome to the page about me!<br>
Here you'll find lots of information about me!<br>
Isen't that great?<br>
My name is xxx<br>
I live in a small town in Norway.
~;
}
sub pictures {
&pictureshtml;
}

sub pictureshtml {
print qq~
Here are som pictures!
~;
}
sub guestbook {
&guestbookhtml;
}

sub guestbookhtml {
print qq~
Say what you think about this page!
Fill out the form:
~;
}

Last edited by:

Andy: Mar 29, 2011, 6:07 AM
Quote Reply
Re: [perlman] How to use ? In reply to
You need to output a header to the browser

Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
Quote Reply
Re: [perlman] How to use ? In reply to
You are using twice as many subs as you need, for example:

sub pictures {
&pictureshtml;
}


...could be changed so you just add the html from pictureshtml() into that sub.