MoonPoint Support Logo

Geeks.com - Free Shipping



Advanced Search
February
Sun Mon Tue Wed Thu Fri Sat
     
8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      
2012
Months
FebMar
Apr May Jun
Jul Aug Sep
Oct Nov Dec


Sun, Nov 13, 2011 8:22 pm

file_upload Perl Script

The PHP code I had been using to upload files to a website would not allow me to upload files more than a few MB in size, likely due to the MAX_FILE_SIZE setting for HTTP_POST_VARS. I decided I would use a Perl script, instead, for uploading files to the site.

I used a Perl script Uploading Files Using CGI and Perl Article written by Matt Doyle as the basis for my script with just some minor modifications. I changed the maximum allowed size for uploaded files from 5 MB to 15 MB. I also eliminated the code pertaining to the uploader's email address, since I didn't need that information, and removed all references to photos in the code, since I wanted to use the script for uploading any type of file not just photos. I added some code to provide file size information on the uploaded file when the upload completes, which I got from How do I display filesizes in a nice way? at Stuart's Useful Perl Pages.

When a file is uploaded, the script will now show something like the following when the upload is completed.

File upload completed!

File uploaded: opera-10.11.gcc4-static-qt3.pkg.gz
File size: 14.43 MB (15136905 bytes)

The Perl script, file_upload.cgi is available here

References:

  1. Uploading Files Using CGI and Perl Article
    By: Matt Doyle
    Date: March 14, 2008
    SitePoint >> Web Design, Web Development, Freelancing, Tech News and more
  2. How do I display filesizes in a nice way?
    Stuart's Useful Perl Pages
  3. How to get the size of a file in Perl using file test operators.
    Perl Programming & Tutorials from About.com

[/languages/perl] permanent link

Sun, Sep 11, 2011 11:15 pm

Album Thumbnail Generator

I needed to generate thumbnail images for some photos my wife took that I placed on the website, so I could send a webpage link to someone else so she could view the pages rather than me sending all of the images to her by email. I also needed a program to not only create the thumbnail images, but to create a webpage indexing all of the thumbnails and providing links from the thumbnails to the full-size images.

I was in a hurry and didn't want to spend a lot of time setting up software to do that nor spend a lot of time doing it manually. I thought I had installed an easy-to-use program on the system several year ago to do just that for a friend who wanted to post pictures of storm damage to his house, but I couldn't remember the name of the program nor how to access it. After looking on the system for his photos, I saw that I had used a Perl program, Album by David Ljung Madison. He also offers a number of other free scripts at his MarginalHacks website. There's an example of the output from his photo album generator script at DavePics.

Once I realized where the album Perl script was placed on my system, I was able to generate the thumbnails and associated HTML pages by just running the script while my working directory was the one in which the photos were located. E.g. /path_to_script/album. An index.html page was generated by the script containing all of the thumbnails, which are placed in a tn directory beneath the one containing the images. Clicking on a thumbnail then brought up a webpage containing the full-size image and navigation links to the additional images.

I thought I should make a note to my self here, so that if I forget the name of the program again, I have this blog entry to remind me of its name and how to use it to generate image thumbnails and webpages for the images.

[/languages/perl] permanent link

Thu, May 22, 2008 9:07 pm

perl-Calendar-Simple Package

I needed to install a calendar generation package on a Linux system. I had previously used pcal on another Linux system and decided to use it again, since I was familiar with it and found it met my requirements.

I looked for a pcal RPM package. I found that the perl-Calendar-Simple package contained a pcal program and decided to install it. I installed the package, which is available from http://packages.sw.be/perl-Calendar-Simple/ or RPM PBone. If you are using the RPMforge repository, you can install it with yum or another installer. To configure yum to use the RPMForge repository, see RPMForge Packages and Yum Priorites.

# yum install perl-Calendar-Simple

After I installed the package, I found it installed /usr/bin/pcal. That pcal program was a Perl script. I could use it to generate the current month's calendar by typing pcal or a specific month from the current year with pcal m, where m is a number representing a month, e.g. pcal 6 would display the month of June. Or you can use pcal mm, e.g. pcal 06 for June. That pcal can also generate a calendar for a specific month and year with pcal mm yyyy, e.g. pcal 06 2009 for the calendar for June 2009.

# pcal 06 2009

     June 2009
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

But that pcal program didn't offer me as many options as I had with the cal program that was already installed on the system. I didn't need anything particularly fancy, but I did need the capability to generate a calendar in HTML format, which the Perl pcal script didn't provide.

References:

  1. RPMForge Packages and Yum Priorites
    MoonPoint Support
  2. Pcal
    MoonPoint Support
  3. PCAL and LCAL: PostScript Calendar Programs
    SourceForge.net
  4. pcal and lcal - pcal branch
    freshmeat.net

[/languages/perl] permanent link

Sat, Oct 27, 2007 3:37 pm

ibmonitor

ibmonitor is a Perl script that can be used to monitor bandwidth usage.


 Interface    Received        Sent       Total
                  Kbps        Kbps        Kbps

 eth0           189.04        4.00      193.12

 lo               0.00        0.00        0.00

 All            189.04        4.00      193.12

 Press 'q' to quit...           Elapsed time: 0 hrs, 0 mins, 4 s

If you hit the m key while the program is running, it will show the maximum bandwidth used while it has been running. You can also start the program with ibmonitor --max to display the maximum bandwidth utilization.


 Interface    Received        Sent       Total
                  Kbps        Kbps        Kbps

 eth0           294.40        5.92      300.32
 |---- Max      443.12       64.40      448.40

 lo               0.00        0.00        0.00
 |---- Max       20.24       20.24       40.56

 All            294.40        5.92      300.32
 |---- Max      453.28       74.56      468.64

 Press 'q' to quit...           Elapsed time: 0 hrs, 0 mins, 43 s

Usage

ibmonitor is a command line program which will run
on a linux console or xterm (rxvt, konsole, gnome-terminal, etc)

usage: ibmonitor [ --bits ] [ --bytes ] [ --max ] [ --avg ] [ --interval n ] [--data]
                         [ --colors | --nocolors ] [--dev regex] [--file procfile]
                         [ --help ] [--version]

The following command line options (and their explanation) are available:

--bits Show output values in KBits/sec. This is the default
--bytes Show output values in KBytes/sec
--max Show maximum values per interface
--avg Show average values per interface
--interval n
Set time interval as n seconds. The default is 2 seconds
--data Show data transferred in KB/MB/GB
--colors Show some fancy coloring! (This is the default)
--nocolors No fancy coloring please!
--dev regex
Show output from device matching regex
--file proc
Specify which file to use in the proc filesystem
for the interface byte counter
--help Show help and exit
--version Show version number and exit


While running, ibmonitor can read the input key from the user
and dynamically change its output display format depending on the key
pressed.

The following keys are supported. Note that ibmonitor responds immediately
to the single keystroke. ie. The 'Enter' key need not be pressed

q
[q]uit
1 - 9
Set sleep time interval (in seconds) to the digit entered
m Toggle display of [m]ax bandwidth
a
Toggle display of [a]verage bandwidth
i
Toggle display of values in KB[i]ts/sec (Kbps)
y
Toggle display of values in KB[y]tes/sec (KBps)
d
Toggle display of [d]ata transferred
s
Shift interface up/down.
This should be followed by the interface number,
and then the direction (u or d)
r
[R]eset all values
? / h
Help screen for interactive commands

References:

  1. ibmonitor Interactive Bandwidth Monitor
    SourceForge.net
  2. Perl ReadKey Module for ibmonitor
    Date: October 26, 2007
    MoonPoint Support

[/languages/perl] permanent link

Fri, Oct 26, 2007 5:32 pm

Perl ReadKey Module for ibmonitor

I tried installing ibmonitor using the ibmonitor-1.4-1.noarch.rpm RPM file I obtained from ftp://download.fedora.redhat.com/pub/fedora/linux/extras/6/i386. However, when I tried to install it, I got a message indicating the Term::Readkey module was missing.

# rpm --install ibmonitor-1.4-1.noarch.rpm
warning: ibmonitor-1.4-1.noarch.rpm: V3 DSA signature: NOKEY, key ID 1ac70ce6
error: Failed dependencies:
        perl(Term::ReadKey) is needed by ibmonitor-1.4-1
# perldoc -l Term::ReadKey
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm

But when I used the perldoc -l command to check on whether all of the required modules for ibmonitor were present, I saw it listed.

$ perldoc -l Term::ANSIColor
/usr/lib/perl5/5.8.0/Term/ANSIColor.pm
$ perldoc -l Term::ReadKey
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm
$ perldoc -l Time::HiRes
/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/Time/HiRes.pm

I also saw it listed when I used the find-modules.pl script I have on the system.

# ./find-modules.pl | grep -i ReadKey
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm

When I used the one-line Perl command perl -MTerm::ReadKey -el Perl did not complain. Nor did it complain when I tried perl -e 'use Term::ReadKey;'. If the module wasn't found by Perl it should complain it couldn't locate the module anywhere when I used either command.

And when I used the List Perl Modules CGI script, I have on the system, I also saw it listed there. So I didn't know why I got the message indicating the module is missing when I tried to install ibmonitor.

I used the locate command to look for any other occurrences of the file ReadKey.pm on the system, which showed the following.

# locate ReadKey.pm
/root/.cpan/build/TermReadKey-2.21/ReadKey.pm
/root/.cpan/build/TermReadKey-2.21/blib/lib/Term/ReadKey.pm
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm

I decided to try downloading and installing the module again through CPAN.

# perl -MCPAN -e shell
Undefined value assigned to typeglob at (eval 14) line 15,  line 11.
Warning [/etc/inputrc line 11]:
  Invalid variable `mark-symlinked-directories'

cpan shell -- CPAN exploration and modules installation (v1.76)
ReadLine support enabled

cpan> install Term::ReadKey
PAN: Storable loaded ok
Going to read /root/.cpan/Metadata
  Database was generated on Thu, 24 Nov 2005 14:54:20 GMT
CPAN: LWP::UserAgent loaded ok
Fetching with LWP:
  ftp://archive.progeny.com/CPAN/authors/01mailrc.txt.gz
LWP failed with code[500] message[LWP::Protocol::MyFTP: connect: timeout]
Fetching with Net::FTP:
  ftp://archive.progeny.com/CPAN/authors/01mailrc.txt.gz
Fetching with LWP:
  ftp://carroll.cac.psu.edu/pub/CPAN/authors/01mailrc.txt.gz
Going to read /root/.cpan/sources/authors/01mailrc.txt.gz
CPAN: Compress::Zlib loaded ok
Fetching with LWP:
  ftp://archive.progeny.com/CPAN/modules/02packages.details.txt.gz
LWP failed with code[500] message[LWP::Protocol::MyFTP: connect: timeout]
Fetching with Net::FTP:
  ftp://archive.progeny.com/CPAN/modules/02packages.details.txt.gz
<text snipped>
  /usr/bin/make test -- OK
Running make install
Installing /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/auto/Term/ReadKey/ReadKey.so
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Term/ReadKey.pm
Installing /usr/share/man/man3/Term::ReadKey.3pm
Writing /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/auto/Term/ReadKey/.packlist
Appending installation info to /usr/lib/perl5/5.8.0/i386-linux-thread-multi/perllocal.pod
  /usr/bin/make install  -- OK

cpan> exit
No history written (no histfile specified).
Lockfile removed.

Installing the later version of ReadKey.pm didn't help, though. I still got the same error message when I tried to install ibmonitor.

# rpm --install ibmonitor-1.4-1.noarch.rpm
warning: ibmonitor-1.4-1.noarch.rpm: V3 DSA signature: NOKEY, key ID 1ac70ce6
error: Failed dependencies:
        perl(Term::ReadKey) is needed by ibmonitor-1.4-1

I downloaded the src.rpm file, ibmonitor-1.4-1.src.rpm . Installed it with rpm --install ibmonitor-1.4-1.src.rpm, which created two files: usr/src/redhat/SOURCES/ibmonitor-1.4.tar.gz and /usr/src/redhat/SPECS/ibmonitor.spec. When I checked the ibmonitor.spec file, I found the following line in the spec file:

Requires:       perl(Term::ReadKey)

The Term::ReadKey module is only needed for ibmonitor fo changing the display on the fly by hitting certain keys, e.g. "m" to show the maximum bandwidth used while the program has been running. The program, which is a Perl script, will run without ReadKey support being present on the system. There is code within the script that checks for the presence of the module, but doesn't require it to be present. That code is shown below.

# Check for installation of Term-ReadKey module
eval q/use Term::ReadKey;/;
if (!$@) {
        $useReadKey = 1;
}

Since the module is actually present on the system and I can change the ibmonitor display by hitting the keys that trigger options in the program, such as the display of maximum bandwidth, I used rpm --nodeps --install ibmonitor-1.4-1.noarch.rpm to ignore the dependency check for Term::ReadKey and proceed with the installation of the ibmonitor RPM file. The program worked fine after the installation.

The RPM installation installs the following files:

$ ls -l /usr/share/doc/ibmonitor-1.4/
total 44
-rw-r--r--    1 root     root           33 Oct 13  2006 AUTHORS
-rw-r--r--    1 root     root         4260 Oct 13  2006 ChangeLog
-rw-r--r--    1 root     root        18009 May  3  2003 COPYING
-rw-r--r--    1 root     root         5477 Oct 13  2006 README
-rw-r--r--    1 root     root           67 Oct 13  2006 TODO
$ ls -l /usr/bin/ibmonitor
-rwxr-xr-x    1 root     root        31971 Oct 13  2006 /usr/bin/ibmonitor

References:

  1. How do I find which modules are installed on my system?
    perlfaq3 - perldoc.perl.org

  2. Chapter 10. Advanced RPM Packaging
    Fedora Documentation on docs.fedoraproject.org

  3. Chapter 22. Spec File Syntax
    Fedora Documentation on docs.fedoraproject.org

  4. [COMMIT LOGREPORT] package/rpm/SPECS lire.spec,1.26,1.27
    By: Wytze van der Raay wraay at users.sourceforge.net
    Date: August 4, 2006
    lists.logreport.org Mailing Lists

  5. Spec file tags
    Date: July 2, 2005
    Wraptastic

[/languages/perl] permanent link

Sat, Apr 14, 2007 9:12 pm

Changing an Uppercase Name in Perl

For a Perl variable, $name, which contains a name in the form first name followed by a space and then the last name with all letters in uppercase, the following code will change the name so that only the first letter of both parts of the name is capitalized with the rest of the name in lowercase. E.g. if $name contains JOHN SMITH, afterwards it will contain John Smith.


# The name is in all uppercase letters. Leave the first letter of
# each part of the name in upper case, but put all the others in lowercase
$name =~ tr/A-Z/a-z/;
$name =~ s/([a-z]+)\s([a-z]+)/\u$1 \u$2/;

The first line changes all uppercase letters to lowercase. The next line looks for the first part of the name, which is stored in $1. There is then a space followed by the last name, which is stored in $2. Using the substitute command, the first letter of $1 is changed to uppercase as is the first letter of $2.

The [a-z] instructs Perl to look for an occurrence of any letter from "a" to "z". The + afterwards indicates that Perl should look for 1 or more occurrences of any letter between "a" and "z". Enclosing the [a-z]+ between ( and ) instructs Perl to store what if finds, i.e. the first name in this case, in a variable $1. The \s tells it to look for a whitespace character, i.e. a space in this case, and then the next ([a-z]+) will find all of the letters for the last name and store it in a variable $2 The \u changes the following letter to uppercase for $1, which is the first name. The \u$2 then changes the first letter of $2 , which is the last name to uppercase.

[/languages/perl] permanent link

Fri, Mar 10, 2006 8:37 pm

Troubleshooting CGI Scripts

If you are having problems with a CGI script, some useful links are listed below:

  1. Perl & CGI Tutorial: Troubleshooting CGI Scripts
  2. Troubleshooting Perl CGI scripts
  3. Troubleshooting CGI Installations [CGI & Perl Tutorials]

[/languages/perl] permanent link

Mon, Nov 28, 2005 11:07 pm

Bad interpreter: No such file or directory

If you receive the message "bad interpreter: No such file or directory" when attempting to execute a Perl script, it may because the path to Perl is incorrect in the script. You can check the location of the Perl executable with which perl. Some common locations for Perl are shown below:

/uisr/bin/perl
/usr/sbin/perl
/usr/local/bin/perl
/usr/bin/perl5
/usr/local/bin/perl5

The first line of the Perl script should point to the location revealed for Perl by which perl. E.g., if Perl is in /usr/bin, you should see the following line as the first line of the Perl script.

#!/usr/bin/perl

But, if the path is listed correctly in the script, another check to make is that you aren't using a Perl file in DOS format. For instance, if the file was created on a Windows system and was transferred to a Unix or Linux system in binary mode rather than ASCII mode, then the lines in the file may not be terminated properly for the Linux or Unix system. For a Linux or Unix system, each line should be terminated with a newline character, i.e. hexadecimal 0A. A DOS formatted file, i.e. the text file type you would find on a Windows system will use both a carriage return (CR) and line feed (LF), i.e. a hexadecimal 0D followed by a hexadecimal 0A.

If you attempt to run a Perl script which uses the DOS format on a Unix or Linux system, you will likely get the error message ": bad interpreter: No such file or directory"

You can check the line endins with the hexdump command. Below are two example files test.pl and test2.pl, which are identical, except for the line endings.


# hexdump -C test.pl
00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 70 65 72 6c 0d  |#!/usr/bin/perl.|
00000010  0a 0d 0a 70 72 69 6e 74  20 22 68 65 6c 6c 6f 5c  |...print "hello\|
00000020  6e 22 3b 0d 0a                                    |n";..|
00000025
# hexdump -C test2.pl
00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 70 65 72 6c 0a  |#!/usr/bin/perl.|
00000010  0a 70 72 69 6e 74 20 22  68 65 6c 6c 6f 5c 6e 22  |.print "hello\n"|
00000020  3b 0a 0a                                          |;..|
00000023

If you examined the code in a regular editor, you would see the following lines in each file:

#!/usr/bin/perl

print "hello\n";

But, if you tried to execute them on a Unix or Linux system, you would see different results.

# ./test.pl
: bad interpreter: No such file or directory
# ./test2.pl
hello

You can convert a file, e.g. test.pl, to Unix text file format with dos2unix, which is a DOS/MAC to UNIX text file format converter.

dos2unix test.pl

References:

  1. Perl & CGI Tutorial: Your First CGI Script
  2. dos2unix

[/languages/perl] permanent link

Mon, Nov 28, 2005 8:05 pm

List Perl Modules

I encountered a problem with a Perl module not being found when I tried to run a Perl script that required it, even though I thought I had successfully installed that module. I wanted to see a list of all the Perl modules installed on the system. I found a free Perl script list-modules.pl at http://webnet77.com/scripts/list-modules/, which will list all of the Perl modules installed on the system on which it is run. The output of the program is an HTML page listing the modules. So you can put the script in a cgi-bin directory on your Web server to make the information accessible over the web. On a Linux or Unix system, you should use chmod 755 list-modules.pl to first make the file executable.

I did encounter one problem when I first attempted to run the script. I kept getting a ": bad interpreter: No such file or directory" message whenever I tried to run it. That usually indicates the path to the Perl interpreter in the first line of the file is incorrect. When I checked it, I found it was "#!/usr/bin/perl". When I checked Perl's location, I saw it was in /usr/bin/perl.

which perl
/usr/bin/perl

It took me a few minutes to realize that when I downloaded list-modules.zip from the Webnet77 page and then unzipped it on my Linux server, that the extracted Perl file, list-modules.pl, was in DOS format, i.e. the end of every line was terminated with a carriage return and line feed (hexadecimal 0D and 0A) rather than just a line feed (hexadecimal 0A), which is how newlines are indicated on Unix and Linux systems. I spotted the problem when I used hexdump -C list-modules.pl. I used dos2unix list-modules.pl to convert the file to the Unix text file format. I was then able to successfully run the list-modules.pl script.

Download

list-modules.pl
list-modules.zip

[/languages/perl] permanent link

Fri, Apr 08, 2005 5:34 pm

antixls

I needed the capability to convert Microsoft Excel spreadsheets sent to my email account on a Linux system to a form I could work with on that system. The spreadsheets contain just email addresses that I need to put into a text file for a mailing list on the Linux email server. I wanted something simple and straightforward to use. I didn't need a lot of bells and whistles, just the capability to convert the data in the .xls spreadsheet file to a text or CSV file. I found antixls, which can be downloaded from the author's site at http://www.af0.net/~dan/?antixls, which suited my needs perfectly.

Antixls is a small Perl script that can display the information in a spreadsheet in a number of modes, including in ASCII art format, "linear" (unformatted), CSV, and linearly with cell indices, which is the default mode. The antixls Perl program provides a wrapper for Kawai Takanori's Spreadsheet::ParseExcel module.

Help on using the script can be viewed by typing antixls --help.

Usage: antixls [options...] excelfile1 [excelfile2 ...]
General Options:
--help                  This help information
--version               Show version information
--formatted             Display sheets in ASCII-art table
--linear                Display sheets in "linear" (unformatted) mode
--csv                   Display sheets in CSV mode
--indexed               Display sheets linearly with cell indices (default)

I wanted to convert the membership spreadsheet to text or CSV format. I found that converting to CSV format with antixls worked, since afterwards I only needed to remove the comma at the end of each line with vi to put the email addresses in the text format I needed of one addres per line. Linear (unformatted) mode would also have worked well. As examples of the output from the program, below I've included the output in the formats the program can use for output. I placed the commands that produced the output above the output. The actual email addresses have, of course, been altered.

CSV

./antixls-0.1b.perl --csv Members.xls >Members.csv

OfficeE-mail,
1pm4467@gw.njsp.org,
1701A@dunbararm.com,
1769B@dunbararm.com,
aackorman2@sovlog.com,
aaron.groom@pharma.com,
b1smith@bulldog-tech.com,

ASCII Art

./antixls-0.1b.perl --formatted Members.xls >Members.txt

Sheet: 2005_All_Members
================================================================================
|OfficeE-mail                             |
|1pm4467@gw.njsp.org                      |
|1701A@dunbararm.com                      |
|1769B@dunbararm.com                      |
|aackorman2@sovlog.com                    |
|aaron.groom@pharma.com                   |
|b1smith@bulldog-tech.com                 |

Indexed

./antixls-0.1b.perl --indexed Members.xls >Members.indexed

Sheet: 2005_All_Members
(0, 0)          OfficeE-mail
(1, 0)          1pm4467@gw.njsp.org
(2, 0)          1701A@dunbararm.com
(3, 0)          1769B@dunbararm.com
(4, 0)          aackorman2@sovlog.com
(5, 0)          aaron.groom@pharma.com
(6, 0)          b1smith@bulldog-tech.com

Linear

./antixls-0.1b.perl --linear Members.xls >Members.linear

Sheet: 2005_All_Members
OfficeE-mail
1pm4467@gw.njsp.org
1701A@dunbararm.com
1769B@dunbararm.com
aackorman2@sovlog.com
aaron.groom@pharma.com
b1smith@bulldog-tech.com

Site antixls
Developer Download
MoonPoint Download

Download antixls

[/languages/perl] permanent link

CompuVest - Notebooks

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo