Article Author Perl FAQ
Bookmark and Share
Back to Programming
Printer Friendly
Email this Article
Link this Article
- 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");
- 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.
Code:
rmdir(DIRNAME)
-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);
-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';
-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);
-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.

-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.

-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.

-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.

-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 one is quoted directly from the Perl 5.8.4 Documentation (perlfaq5 - Files and Formats)

Comments:  
1
Luke says:
August 16 2008, 12:02 pm
This is a great article written by GeoServ from DNLodge!
Username:      Password:     
Log in to post comments and rate this article!