 |
|
|
06-17-2004, 02:15 AM
|
|
#1
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
Perl: Frequently Asked Questions
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-25-2004 at 01:41 AM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-17-2004, 02:42 AM
|
|
#2
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How do I get my Perl script up and running?
[list=1]
[*] First is always read any instructions that came with your Perl script. The instructions will tell you what variables you will need to edit within the scripts to customize them for your specific web server. Such as paths to files or URLS.
[*] Make sure the first line of the script is the correct path to Perl for your particular web server. Most use the commn path of:
#!/usr/bin/perl
Always edit and save Perl scripts in plain text or ASCII format. Never use a word processor to edit and save Perl scripts unless you know how to save in plain text format using your word processor program. Use a text editor like Notepad if you have to.
[*] Upload your script in ASCII mode using your FTP program into the folder designated by your web host for Perl/CGI scripts. This is almost always called the cgi-bin. Read the help files of your particular FTP program about how to do that if you are unsure.
[*] CHMOD your perl scipt (set the file permissions) using your FTP program according to the instructions given with the script. Most scripts suggest 755 or 777. Read the help files of your particular FTP program about how to CHMOD files.
A setting of 755 means to set the CHMOD the file to:
owner: read write execute
group: read execute
other: read execute
A setting of 777 means to CHOMD the file to:
owner: read write execute
group: read write execute
other: read write execute
[*] That's it. You should now be able to enter the URL of the script into your browser and it should run. If not you will have to double-check that you did everything correctly.
[*] Most web hosts have a FAQ's section that lists the paths you need for running Perl scripts and other information like the path to sendmail the path to date, etc.[/LIST=1]
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-17-2004 at 03:10 AM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-17-2004, 03:19 AM
|
|
#3
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
Regular Expressions
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-17-2004 at 11:47 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-17-2004, 11:56 PM
|
|
#4
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How do I delete a file using Perl?
To delete files you use the unlink() function. Here are a few examples of how the unlink() function can be used:
Code:
$cnt = unlink 'a', 'b', 'c';
unlink @goners;
unlink <*.bak>;
unlink("/path/to/file.bak");
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 12:00 AM
|
|
#5
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How do I delete a directory using Perl?
To delete a directory you use the rmdir() function. Directories must be empty before they can be deleted.
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 12:04 AM
|
|
#6
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How do I rename a file or directory using Perl?
To rename a file or directory you use the rename() function.
Code:
rename(OLDNAME,NEWNAME);
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 12:15 AM
|
|
#7
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How do I CHMOD a file using Perl?
To CHMOD (set file/folder permissions) you use the chmod() function. Some examples:
Code:
chmod 0755, @executables;
chmod 0644, filename;
$cnt = chmod 0644, 'file1','file2','file3';
note that the numeric value of chmod (0755, 0644, etc) should not be quoted ('0755', '0644', etc).
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 03:36 AM
|
|
#8
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How do I sort arrays using Perl?
There are many ways of sorting arrays with Perl, but there are two basic sort types, numerical sort and string (alpha) sort. For sorting numbers by their numeric value (and not the order of the digits as in a string sort) you use the numerical comparison operator, <=>. To sort by string (alphabetical and digital ) order you use the cmp operator (which is the default sort order unless you use the numerical comparison operator, <=>).
This is the sample list we will use:
@list = qw(99 Fred barney !help 4011111 Wilma 12 Betty 401 pebbles bam-bam 56789 400 #num);
It's an array of mixed case words, digits, and symbols. It will help to display better how the various sort methods work and the results they get.
1. Simplest way to sort an array:
Quote:
@sorted = sort @list;
The precedence for this simple sort is
1. symbols
2. digits (by the order of the digits)
3. upper case alpha
4. lower case alpha
the new array (@sorted) will look like this:
!help #num 12 400 401 4011111 56789 99 Betty Fred Wilma bam-bam barney pebbles
it's not much good unless you are only sorting same case alpha words or characters.
|
2. A better way to sort:
Quote:
@sorted = sort { $a <=> $b || $a cmp $b} @list;
The precedence for this sort method is:
1. symbols
2. upper case alpha
3. lower case alpha
4. digits (by numeric value)
@sorted will now look like this:
!help #num Betty Fred Wilma bam-bam barney pebbles 12 99 400 401 56789 4011111
the alpha sort still leaves a little to be desired since all upper case is still sorted before any lower case. We solve this by using the lc (lower case) operator in the sort:
@sorted = sort { $a <=> $b || lc($a) cmp lc($b) } @list;
now the precedence is:
1. symbols
2. alpha (case insensitive)
3. digits (by numeric value)
@sorted will now look like this:
!help #num bam-bam barney Betty Fred pebbles Wilma 12 99 400 401 56789 4011111
|
3. Sorting In Reverse Order
Quote:
To sort in reverse order you just transpose $a and $b:
@sorted = sort { $b <=> $a || lc($b) cmp lc($a) } @list;
|
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-18-2004 at 03:28 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 03:51 PM
|
|
#9
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How Can I find Occurences in Lines Between Two Patterns Using Perl?
Assuming the two patterns are START and END you can do something like this:
Code:
open (FILE,"yourfile");
while ( <FILE> ) {
push(@temp,$1) if (/START(.*?)END/gs);
}
close(FILE);
any occurences of a match will be stored in the @temp array in the above example.
If you just wanted to print the lines that had the two patterns and not worry about what was between the patterns you can do something like this:
Code:
open (FILE,"yourfile");
while ( <FILE> ) {
print "$_\n" if (/START/ .. /END/);
}
close(FILE);
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 05:13 PM
|
|
#10
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How Do I Process MY CGI Forms Using Perl?
There really is only one answer to this question. You should be using the CGI.pm module that comes with all standard distributions of Perl to process form data sent to the server. Users of Perl (at least for the internet) should take the time to learn how to use the CGI module if they never learn to use any other module. You can read about how to use the CGI module here:
http://www.perldoc.com/perl5.8.4/lib/CGI.html
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-18-2004, 10:53 PM
|
|
#11
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How can I know what's causing a 500 Internal Error Message?
When you see the typical "500 Internal Server Error" message it's not much help at understandling what went wrong and is causing the script to crash and burn. To see a much better error message place this code at the beginning of your Perl script, but below the very first line of the script:
Code:
use CGI::Carp qw(fatalsToBrowser);
This will force Perl to display in your browser a more detailed description of what is causing your script to crash.
You should use it when debugging problems but remove it or comment it out once your script is running properly.
If you still get a 500 Internal Server Error after inserting that line just below the first line of your Perl script, which will look something like this:
#!/usr/bin/perl
that generally means the very first line is the wrong path to Perl or there is a syntax error in that line. Make sure the line starts with a number sign, follwed by an exclamation:
#!
and check with your host that you are using the correct path to Perl.
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-19-2004 at 05:16 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-19-2004, 04:05 AM
|
|
#12
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How Can I See a List of the Perl Modules Installed on My Server?
This short script should work on just about any server to list the Perl modules installed on the server or your hosts server. It might take a moment to run and display so be patient if the list takes a few moments to display in your browser.**
Code:
#!/usr/bin/perl -w
use CGI;
use strict;
use File::Find;
my %list;
my $q = new CGI;
print $q->header();
find (\&wanted, @INC);
sub wanted {
next if (/^\.{1,2}$/);
$list{$_} = $_ if -f && /\.pm$/;
}
my @sorted = sort { lc($a) cmp lc($b) } keys %list;
my $cnt = @sorted;
print "$cnt unique modules found.<br /><br />";
print "$_<br />\n" for @sorted;
** this script is provided as-is, no warranty of fitness is expressed or implied. Install and use if you know how, I will not answer questions or provide support for the script.
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-19-2004 at 03:02 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-19-2004, 04:17 PM
|
|
#13
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
Should I always quote my "$variables"?
In general it does no harm to quote your $variables, but its not good Perl programming practice to do so and most of the time is not necessary. When you double-quote your variables you force Perl to make them into strings (stingification), but they already are strings, why do it over again? Numbers do not have to be quoted unless you want them in a string context. Some examples to consider:
$num = "123"; #BAD
$num = 123; #GOOD
somefunction("$num"); #BAD
somefunction($num); #GOOD
$word = 'a string of words';
$copy = "$word"; #BAD
$copy = $word; #GOOD
print "$sentence"; #BAD
print $sentence; #GOOD
also, if you double-quote an array when printing it Perl adds extra spaces, or blanks, between the array elements. This sometimes is handy but sometimes it's not.
@array = `some_command`;
print "@array"; #might not be what you expect
print @array; #printed with no extra blanks
OK, so double-quoting for the most part is not a big deal, but it's better to not quote variables when they shouldn't be quoted. Could save you some typing too. 
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-23-2004 at 02:13 AM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-19-2004, 04:46 PM
|
|
#14
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
What does "Can't Find String Terminator "XXX" Anywhere Before EOF" error mean?
This most often seems to happen when using the print command like this to print some output to the screen:
Code:
print <<"EOF";
Hello, my name is Kevin
EOF
The last line, EOF, which is the end of string terminator, must be flush against the left margin and no spaces or other characters should be to the right of it on the same line.
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 06-20-2004 at 02:22 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
06-20-2004, 02:42 PM
|
|
#15
|
 |
|
Super Deity (Level 18)
Join Date: Sep 2000
Posts: 10,217
|
How can I output my numbers with commas added?
This subroutine will add commas to your numbers:
Code:
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
You call the subroutine where needed in your script, something like:
$num = 16574.33 + 19983745.21;
commify($num);
print $num;
This regex from Benjamin Goldberg will also add commas to numbers:
Code:
s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
** this FAQ is quoted directly from the Perl 5.8.4 Documentation (perlfaq5 - Files and Formats)
__________________
1000MB Disk Space - 100GB Data Transfer - PHP - Perl - MySQL - Frontpage
Bored? Play Hangman
Last edited by kevin : 07-03-2004 at 08:53 PM.
|
|
Add to del.icio.us
Can you digg it?
|
|
 |
|
|
KEEP TABS |
|
SPONSORS |
| |

|
| |
|
|
| |
|