...

View Full Version : using "require"



Phil Jackson
01-29-2010, 10:57 PM
Hello, im trying to create a base template which then loads data depending on what actions are taken. I included ( required ) some pages which was fine but then i included another file which I got a 500 internal error. pasting the code straight in and it works fine.

heres what i've got;



#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
require LWP::UserAgent;
use DBI;


#deal with post requests
require "perl/post-sort.pl";
#loading stylesheets and javascripts
require "header.pl";
# bring in loggin js
if( $arg{REQUEST_KEY} eq "") {
require "javascript/js-main-login.pl";
}
print "</head> \n";
print " \n";
...
...
...


perl/post-sort.pl



my %arg = ();
for (split /\&/, <STDIN>) {
my ($key, $val) = split /=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
$arg{$key} = $val;
}


Any help much appreciated

FishMonger
01-29-2010, 11:24 PM
If you want to write good quality Perl code, you will definitely not want to write your scripts in that manor.

Use the CGI module!!!

If you want to use a template, then use one Perl's template frameworks. The easiest to begin with is probably HTML::Template.
http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm

bazz
01-29-2010, 11:26 PM
AT the top of your perl file, you ought to have the location of files to be required, declared.

eg


use lib '/var/www/bleh/name_of_dir';


then in your perl, just



require 'name_of_file.pl';


I prefer not simply to require the file but, to send a call to the required file. instead of using require'header.pl'; and it runs the header file, I usually do this



require'name_of_file';
header($var1,$var2,$var3..etc);


I can't recall all my reasons for doing it like this but, for one, it lets me determine how the header file runs, depending on what params have been sent to it.

Final tip... why do you use a javascript login when it can't be secure and you are learning perl? I would suggest doing it better than I did and start to learn about security from the beginning otherwise, your scripts may well have holes in them which will necessitate a review/rewrite of them later.

hth

bazz

Phil Jackson
01-29-2010, 11:29 PM
AJAX can be secure. But thank you both.

FishMonger
01-29-2010, 11:48 PM
If you want secure logins, it would be best to use sessions and https

Phil Jackson
01-30-2010, 06:29 AM
Which it does do.

Puffin the Erb
01-30-2010, 02:43 PM
Did you post all the contents of your package? If so, it is missing code at the start and end :



package postsort;

my %arg = ();
for (split /\&/, <STDIN>) {
my ($key, $val) = split /=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
$arg{$key} = $val;
}
1;



The package declaration should be the name of the file minus the .pl or .pm
I removed the - in the name as this is not permitted.

It's a good idea to add a preamble to each package, something like this :



use strict;
use warnings;
use version; our $VERSION = qw(0.01);

FishMonger
01-30-2010, 03:11 PM
Rather than perpetuating incomplete and/or buggy form processing code, it would be better to use the CGI module.

Replace the loading of the post-sort.pl script with this:

use CGI qw(:standard :cgi-lib);
my %arg = Vars;
or use the OO interface, which I prefer.

use CGI;
my $cgi = CGI->new;
my %arg = $cgi->Vars;
either one of those is a far superior method.



package postsort;
Lowercase package names are "reserved" for pragmas. It's better to use titlecase names.

And, if you're going to turn it into a proper module, then it would be best to wrap that code in a subroutine that is exported and have that sub return a hash.

Phil Jackson
01-30-2010, 06:53 PM
Thanks going to give them a bash!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum