Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Use Strict - Advantages?

Quote Reply
Use Strict - Advantages?
Hi,

What are the advantages of using strict in the Links code? I was try to think of the advantages... but can't think of any - does it make the script compile faster or something like that?

------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
In short, it picks up a lot of errors that appear to work, but really don't, and it allows your program to run under mod_perl.

There are some other advantages, but those are the biggest, and probably 80-90% of the benefits.

Quote Reply
Re: Use Strict - Advantages? In reply to
Thanks for clearing it up,

Are there any limitations of it? Some people say it is hard to program with and to mod links, what makes it hard if it just picks up errors? I hate this, I am OK with Perl, I can write flat file scripts etc easily, but I don't know SQL or any of the new things out there Frown - I shall just keep bugging you in here till I do though Smile

------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
use strict is sometime annoying if you forget about it.. but after thousands of errors.. you usually get used to it Smile

i think c/c++ people like it cause in c/c++ you have to do it anyway.. but it's better coding.. there are limitations.. but there are ways to get over it.. for instance.. if you use "use CGI;" you can carry many things over through subroutines..

i like use strict.. cause if you complete a script.. it only means you have like no bugs Smile

also.. when you program.. always use "use CGI" and "use CGI::Carp qw/fatalsToBrowsers/;"

cause use CGI is VERY useful for forms and cookies and a bunch of other things (printing headers, redirections.. a bunch more).. then use CGI::Carp just gives you a better 500 page.. with a detailed error..

jerry
Quote Reply
Re: Use Strict - Advantages? In reply to
Check here: http://perl.about.com/.../weekly/aa082299.htm

Dan Smile
Quote Reply
Re: Use Strict - Advantages? In reply to
Everything has pretty much been said, but just to reiterate, use strict forces you to code better. Developing something the size of Links SQL would be very difficult to do without having use strict on, as otherwise typos and other errors can go by undetected.

If you don't like, you can either remove the use strict from the code, or do:

no strict;

# code that wouldn't pass use stirct goes here.

use stirct;

# back to normal.

However, I would get in the habit of making things work under use strict, you'll be a better programmer for it.

Cheers,

Alex
Quote Reply
Re: Use Strict - Advantages? In reply to
OK then - One more question on use strict. When it gives the error message, does it give the message on the screen, or in your error logs?

BTW - Where can I read about how to use "use CGI" - I have seen it used a lot, but in my 3 Perl books, it never mentions them Frown - Thanks a lot guys.

------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
It will be in the error log. You can test it by running perl -c yourscript.pl and it will generate the errors you can see.

For info on how to use CGI.pm I'd just look at the included docs. If you have perl from Activestate it comes with a nice HTML version. If you want a book, have a look at:

Official Guide to Programming with CGI.pm: http://www.amazon.com/...idos/ASIN/0471247448

Cheers,

Alex
Quote Reply
Re: Use Strict - Advantages? In reply to
OK then - I guess the best way for me to learn it all will be to buy Links SQL and study the code... I have ordered that mSQL and MySQL book, so as soon as I order Links SQL I can make my mods to get my site going...

------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
I don't like use strict at all...

I just tested it on a script I am writing, and it gives me error messages for my Parse Form Sub-Routine...

Code:
sub Parse_Form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

if ($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/, $ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}

foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
}

And the error messages...

Code:
[Fri Nov 26 19:38:46 1999] admin.cgi: Global symbol "buffer" requires explicit p
ackage name at /www/webhostarea/cgi-bin/mailing_list/admin/admin.cgi line 71.
[Fri Nov 26 19:38:46 1999] admin.cgi: Global symbol "getpairs" requires explicit
package name at /www/webhostarea/cgi-bin/mailing_list/admin/admin.cgi line 75.
[Fri Nov 26 19:38:46 1999] admin.cgi: Global symbol "pair" requires explicit pac
kage name at /www/webhostarea/cgi-bin/mailing_list/admin/admin.cgi line 83.
[Fri Nov 26 19:38:46 1999] admin.cgi: Global symbol "key" requires explicit pack
age name at /www/webhostarea/cgi-bin/mailing_list/admin/admin.cgi line 84.
[Fri Nov 26 19:38:46 1999] admin.cgi: Global symbol "value" requires explicit pa
ckage name at /www/webhostarea/cgi-bin/mailing_list/admin/admin.cgi line 84.
[Fri Nov 26 19:38:46 1999] admin.cgi: Global symbol "formdata" requires explicit
package name at /www/webhostarea/cgi-bin/mailing_list/admin/admin.cgi line 92.

If I used that code in Links SQL, how would I fix it?
Quote Reply
Re: Use Strict - Advantages? In reply to
The variables (noted in the error message) need to be declared in advance in the subroutine. For example, my $buffer;

Dan Smile
Quote Reply
Re: Use Strict - Advantages? In reply to
also in Links SQL you don't use parse form subroutines..

you just use

Code:
use CGI ();
my $in = new CGI;

and then $in->param('whatever');

is what "whatever" equals...

it's so easy.. Smile

if you go into links modification forum and look at the MY LINKS thread and look at my script i wrote.. it uses "use CGI" ALOT Smile

for cookies, forms.. etc.. just get an idea out of that.. also.. it used to have use strict.. but use strict simply doesn't work with Links 2.0.. Smile so i had to take it off.. but if you change the requires to the Links SQL modules and add use strict.. it would work.. cause all the variables in there are declared Smile

jerry

[This message has been edited by widgetz (edited November 26, 1999).]
Quote Reply
Re: Use Strict - Advantages? In reply to
I just started using "use strict" and "use CGI" today. I found that debugging was a lot quicker - it pointed out errors clearly. And parsing forms... well what can I say, EZ Smile Setting cookies doesn't seem much easier... but there is less code.

Once mSQL and MySQL arrives I am right to go. My banner advertisement mod was a bit of a dud for Links 2.0 - Infact I dropped in half way as I plan to switch to Links SQL. Links SQL will hopefully be easier to intergrate it into.


------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
 
Quote:
Setting cookies doesn't seem much easier... but there is less code.

Really? How easy is it:

# To set a cookie:
print $in->header ( -cookie => $in->cookie ( -name => 'somename', -value => 'somevalue', -expires '+3h') );

will set a cookie with somename=somevalue that expires in 3 hours.

# to retrieve a cookie.
my $value = $in->cookie ('somename');

will return 'somevalue' if the user has the cookie set. =)

Cheers,

Alex
Quote Reply
Re: Use Strict - Advantages? In reply to
We know that you put it that way Smile - It is easier. I can't get it to work setting cookies with CGI.pm - I used this.

Code:
my $cookie = $in ->cookie(-emailaddress=>'emailaddress',
-value=>'on',
-expires=>'+365d',
-path=>'/',
-domain=>'.webhostarea.com');
print "Set-cookie: $cookie", "\n";

My book must be wrong... I'll give your code a go and see how I go - cause I want to consistently use CGI.pm rather then a bit of everything.

------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
that works too..

but in cgi.pm cookies are printed with the header...

so

Code:
my $cookie = $in->cookie (
-name => 'CookieMonster',
-value => 'Likes to eat cookies!'
);
print $in->header( -cookie => $cookie );

pretty much prints out

Set-cookie: name=CookieMonster; value=Likes to eat cookies!;
Content-type: text/html

jerry
Quote Reply
Re: Use Strict - Advantages? In reply to
I notice that you and Alex never print out the domain or path, are they nessecary? Cause I don't really want to put any unnessecary stuff into the cookies, just the name and email address for convienience.

------------------
Michael Bray
....
Review your webhost, or find a new one at http://www.webhostarea.com


Quote Reply
Re: Use Strict - Advantages? In reply to
Adding -domain => ".yourdomain.com" is useful, otherwise the cookie will only work on the exact domain in which it was set. So if the person had the cookie set at http://yourdomain.com, it wouldn't work at http://www.yourdomain.com which may be a problem.

Adding the path is only useful if you want other programs to be able to access the cookie that are in a different directory.

Cheers,

Alex
Quote Reply
Re: Use Strict - Advantages? In reply to
Whoops... wrong thread.

------------------
POSTCARDS.COM -- Everything Postcards on the Internet www.postcards.com
LinkSQL FAQ: www.postcards.com/FAQ/LinkSQL/








[This message has been edited by pugdog (edited November 29, 1999).]