MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
May
Sun Mon Tue Wed Thu Fri Sat
   
   
2012
Months
May


Mon, May 28, 2012 9:02 pm

Perl Sleep Function

Perl has a function sleep that will cause a Perl script to wait, i.e., "sleep" a specified number of seconds before going on to the next step in a script.

Syntax

sleep EXPR

Definition and Usage

Causes the script to sleep for EXPR seconds, or forever if no EXPR. May be interrupted if the process receives a signal such as "SIGALRM". Returns the number of seconds actually slept. You probably cannot mix "alarm" and "sleep" calls, because "sleep" is often implemented using "alarm".

On some older systems, it may sleep up to a full second less than what you requested, depending on how it counts seconds. Most modern systems always sleep the full amount. They may appear to sleep longer than that, however, because your process might not be scheduled right away in a busy multitasking system.

For delays of finer granularity than one second, you may use Perl’s "syscall" interface to access setitimer(2) if your sys- tem supports it, or else see "select" above. The Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the stan- dard distribution) may also help.

Example

my $sleeptime = 3; # Number of seconds to "sleep"
sleep($sleeptime);

Note

If you use sleep alone without any value given to it for the sleep period, then the script will sleep indefinitely.

[/languages/perl] permanent link

Mon, May 28, 2012 3:16 pm

Wide Character in Print Warning from Perl Script

When I ran a Perl script I wrote to download webpages from a website, I kept getting two warning messages printed for the same line whenever I ran the script.
Wide character in print at ./get_webpage.pl line 39.
Wide character in print at ./get_webpage.pl line 39.

I found at Unicode-processing issues in Perl and how to cope with it, written by Ivan Kurmanov, that the problem can occur when a file has Unicode characters in it. Unicode is a "computing industry standard for the consistent encoding, representation and handling of text expressed in most of the world's writing systems."

Unicode can be implemented by different character encodings. The most commonly used encodings are UTF-8, UTF-16 and the now-obsolete UCS-2. UTF-8 uses one byte for any ASCII characters, which have the same code values in both UTF-8 and ASCII encoding, and up to four bytes for other characters. UCS-2 uses a 16-bit code unit (two 8-bit bytes) for each character but cannot encode every character in the current Unicode standard. UTF-16 extends UCS-2, using two 16-bit units (4 x 8 bit) to handle each of the additional characters.

The webpages I was downloading were encoded using UTF-8, which I confirmed by viewing the source code for one of the webpages I was downloading. In the "head" section of the webpage, I saw the following meta tag.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Line 39 in my Perl script, which printed the webpage, was as follows:

print WEBPAGE $page;

Prior to that line I had the following line:

open(WEBPAGE,">$first_fname") || die "$page$n could not be opened";

The variable first_fname is the name for the file holding the first page saved to disk.

To resolve the problem, I followed the suggestion offered by Ivan to use open FILE, ">:utf8", $filename;. I therefore changed the line in my script to the following:

open(WEBPAGE,">:utf8",$first_fname) || die "$page$n could not be opened";

For subsequent pages, I used an id number followed by ".html" for the filename for the webpages with the id number changing based on the contents of the webpage downloaded. I enclosed ">:utf8" and "$id.html" in double quotes to get the script to run without producing the "wide character in print" warning nor any error messages.


     if ($n == 1) {
       # First page will be named differently, e.g., index.html
       open(WEBPAGE,">:utf8",$first_fname) || die "$page$n could not be opened";
     }
     else {
       open(WEBPAGE,">:utf8", "$id.html") || die "$page$n could not be opened";
     }
     print WEBPAGE $page;
     close(WEBPAGE);
     $n++;

[/languages/perl] permanent link

Mon, May 28, 2012 2:52 pm

Interactive Spelling Checker aspell

For interactive spellchecking on Linux systems, you can use the aspell command at a shell prompt. Just type the command followed by the option list then hit enter and type one or more words for which you wish to check the spelling. Hit Ctrl-D when you've entered all the words for which you wish to check the spelling. You will then see the words that are misspelled, or at least the ones for which aspell doesn't recognize the spelling, displayed.

You can put the words one per line, if you wish, as in the example below. In that example, "cat" was the first word I entered and "apropos" was the last word I entered before hitting Ctrl-D. The aspell spellchecker showed "cateb" and "flummoxe" as the misspelled words immediately after the last word I entered.

$ aspell list
cat
cart
cateb
flummox
flummoxe
apropos
cateb
flummoxe

You can also enter all of the words you want checked on one line, if you prefer, as in the example below, where "cat horse dog punto giraffe" were typed. When I hit Enter and Ctrl-D to terminate word entry, aspell showed "punto" as the misspelled word, since though it is Italian for the English word "point", it is not a valid word in English..

$ aspell list
cat horse dog punto giraffe
punto

[/os/unix/commands] permanent link

Sun, May 27, 2012 10:51 pm

Downloading Webpages with Perl Using Get

I had written a Perl script to dowload specific webpages from a site and peform some processing on the downloaded copies of the webpages. When I moved the script to another system, I received the error message below when I ran it.
Can't locate LWP/Simple.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at ./get_webpage.pl line 5.
BEGIN failed--compilation aborted at ./get_webpage.pl line 5.

The script uses the get command from the LWP::Simple Perl module, which allows me to download a webpage similar to how I would use the GNU wget command from a shell prompt to download webpages. I wanted to use the Perl script, instead of wget, though, since I wanted to examine the webpages for specific links and then download other pages based on specific URLs present in the downloaded web pages.

When I checked for the presence of the LWP::Simple module, which provides support for the get, command on the system on which the script worked, I saw the following:

$ perldoc -l LWP::Simple
/usr/lib/perl5/vendor_perl/5.8.8/LWP/Simple.pm
$ cd /home/joe/www/support/languages/perl

When I checked on the system on which the script failed, I saw the following:

$ perldoc -l LWP::Simple
You need to install the perl-doc package to use this program.

I decided just to proceed with the installation of the LWP::Simple module by running the command perl -MCPAN -e shell. Since it was the first time I had run it, I was prompted to answer a number of questions. I hit Enter to accept the default answer for all of the questions. When the configuration process was completed, I entered install LWP::Simple at the cpan prompt. When the module was installed, I entered exit.

$ sudo bash
[sudo] password for joe: 
root@Saturn:~/Documents/bin# perl -MCPAN -e shell

CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] 

 <install_help>

Warning: You do not have write permission for Perl library directories.

To install modules, you need to configure a local Perl library directory or
escalate your privileges.  CPAN can help you by bootstrapping the local::lib
module or by configuring itself to use 'sudo' (if available).  You may also
resolve this problem manually if you need to customize your setup.

What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')
 [local::lib] 

Autoconfigured everything but 'urllist'.

Now you need to choose your CPAN mirror sites.  You can let me
pick mirrors for you, you can select them from a list or you
can enter them by hand.

Would you like me to automatically choose some CPAN mirror
sites for you? (This means connecting to the Internet) [yes] 
Trying to fetch a mirror list from the Internet
Fetching with HTTP::Tiny:
http://www.perl.org/CPAN/MIRRORED.BY

Looking for CPAN mirrors near you (please be patient)
.............................. done!

New urllist
  http://httpupdate3.cpanel.net/CPAN/
  http://httpupdate23.cpanel.net/CPAN/
  http://mirrors.rit.edu/CPAN/

Autoconfiguration complete.

Attempting to bootstrap local::lib...

Writing /home/joe/.cpan/CPAN/MyConfig.pm for bootstrap...
commit: wrote '/home/joe/.cpan/CPAN/MyConfig.pm'
Fetching with HTTP::Tiny:
http://httpupdate3.cpanel.net/CPAN/authors/01mailrc.txt.gz
Going to read '/home/joe/.cpan/sources/authors/01mailrc.txt.gz'
............................................................................DONE
Fetching with HTTP::Tiny:
http://httpupdate3.cpanel.net/CPAN/modules/02packages.details.txt.gz
Going to read '/home/joe/.cpan/sources/modules/02packages.details.txt.gz'
  Database was generated on Mon, 28 May 2012 01:35:04 GMT
  HTTP::Date not available
..............
  New CPAN.pm version (v1.9800) available.
  [Currently running version is v1.960001]
  You might want to try
    install CPAN
    reload cpan
  to both upgrade CPAN.pm and run the new version without leaving
  the current session.


..............................................................DONE
Fetching with HTTP::Tiny:
http://httpupdate3.cpanel.net/CPAN/modules/03modlist.data.gz
Going to read '/home/joe/.cpan/sources/modules/03modlist.data.gz'
............................................................................DONE
Going to write /home/joe/.cpan/Metadata
Running make for A/AP/APEIRON/local-lib-1.008004.tar.gz
Fetching with HTTP::Tiny:
http://httpupdate3.cpanel.net/CPAN/authors/id/A/AP/APEIRON/local-lib-1.008004.tar.gz
Fetching with HTTP::Tiny:
http://httpupdate3.cpanel.net/CPAN/authors/id/A/AP/APEIRON/CHECKSUMS
Checksum for /home/joe/.cpan/sources/authors/id/A/AP/APEIRON/local-lib-1.008004.tar.gz ok

  CPAN.pm: Going to build A/AP/APEIRON/local-lib-1.008004.tar.gz

Attempting to create directory /home/joe/perl5

*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
*** Since we're running under CPAN, I'll just let it take care
    of the dependency's installation later.
[Core Features]
- ExtUtils::MakeMaker ...loaded. (6.57_05 >= 6.31)
- ExtUtils::Install   ...loaded. (1.56 >= 1.43)
- Module::Build       ...loaded. (0.38 >= 0.36)
- CPAN                ...loaded. (1.960001 >= 1.82)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for local::lib
Writing MYMETA.yml
cp lib/POD2/PT_BR/local/lib.pod blib/lib/POD2/PT_BR/local/lib.pod
cp lib/lib/core/only.pm blib/lib/lib/core/only.pm
cp lib/local/lib.pm blib/lib/local/lib.pm
cp lib/POD2/DE/local/lib.pod blib/lib/POD2/DE/local/lib.pod
Manifying blib/man3/POD2::PT_BR::local::lib.3pm
Manifying blib/man3/lib::core::only.3pm
Manifying blib/man3/local::lib.3pm
Manifying blib/man3/POD2::DE::local::lib.3pm
  APEIRON/local-lib-1.008004.tar.gz
  /usr/bin/make -- OK
'YAML' not installed, will not store persistent state
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl -I/home/joe/perl5/lib/perl5/i686-linux-gnu-threa
d-multi-64int -I/home/joe/perl5/lib/perl5 "-MExtUtils::Command::MM" "-e" "test_h
arness(0, 'inc', 'blib/lib', 'blib/arch')" t/classmethod.t t/coderefs_in_inc.t t
/de-dup.t t/install.t t/lib-core-only.t t/pipeline.t t/stackable.t
t/classmethod.t ...... Name "File::Spec::rel2abs" used only once: possible typo 
at t/classmethod.t line 20.
t/classmethod.t ...... 1/? Attempting to create directory t/var/splat
t/classmethod.t ...... ok   
t/coderefs_in_inc.t .. ok   
t/de-dup.t ........... ok   
t/install.t .......... skipped: Install Capture::Tiny to test installation
t/lib-core-only.t .... ok   
t/pipeline.t ......... ok   
t/stackable.t ........ ok     
All tests successful.
Files=7, Tests=29, 41 wallclock secs ( 0.17 usr  0.04 sys +  1.32 cusr  0.14 csys =  1.67 CPU)
Result: PASS
  APEIRON/local-lib-1.008004.tar.gz
  /usr/bin/make test -- OK
Running make install
Installing /home/joe/perl5/lib/perl5/POD2/PT_BR/local/lib.pod
Installing /home/joe/perl5/lib/perl5/POD2/DE/local/lib.pod
Installing /home/joe/perl5/lib/perl5/lib/core/only.pm
Installing /home/joe/perl5/lib/perl5/local/lib.pm
Installing /home/joe/perl5/man/man3/local::lib.3pm
Installing /home/joe/perl5/man/man3/lib::core::only.3pm
Installing /home/joe/perl5/man/man3/POD2::PT_BR::local::lib.3pm
Installing /home/joe/perl5/man/man3/POD2::DE::local::lib.3pm
Appending installation info to /home/joe/perl5/lib/perl5/i686-linux-gnu-thread-multi-64int/perllocal.pod
  APEIRON/local-lib-1.008004.tar.gz
  /usr/bin/make install  -- OK
Tried to deactivate inactive local::lib '/home/joe/perl5'

local::lib is installed. You must now add the following environment variables
to your shell configuration files (or registry, if you are on Windows) and
then restart your command line shell and CPAN before installing modules:

Use of uninitialized value $deactivating in numeric eq (==) at /home/joe/perl5/l
ib/perl5/local/lib.pm line 354.
Use of uninitialized value $deactivating in numeric eq (==) at /home/joe/perl5/l
ib/perl5/local/lib.pm line 356.
Use of uninitialized value $interpolate in numeric eq (==) at /home/joe/perl5/li
b/perl5/local/lib.pm line 366.
export PERL_LOCAL_LIB_ROOT="/home/joe/perl5";
export PERL_MB_OPT="--install_base /home/joe/perl5";
export PERL_MM_OPT="INSTALL_BASE=/home/joe/perl5";
export PERL5LIB="/home/joe/perl5/lib/perl5/i686-linux-gnu-thread-multi-64int:/home/joe/perl5/lib/perl5";
export PATH="/home/joe/perl5/bin:$PATH";

Would you like me to append that to /home/joe/.bashrc now? [yes] 


commit: wrote '/home/joe/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.960001)
Enter 'h' for help.

cpan[1]> install LWP::Simple
<text snipped> cpan[2]> exit

I was then able to successfully run the Perl script.

[/languages/perl] permanent link

Wed, May 23, 2012 7:17 pm

Creating an Audio CD from MP3 Files in iTunes

Apple's iTunes software can be used to "burn", i.e., write MP3 files to an audio CD that can be played in a car CD player or similar device. You can store up to 80 minutes of music or other audio files on a CD that will play in almost all CD players.

[ More Info ]

[/software/audio_video/itunes] permanent link

Sun, May 20, 2012 5:07 pm

Excel Defined Name

Microsoft Excel provides the capability to assign a "defined name" to a cell or range of cells to make it easier to understand the purpose of a cell or group of cells. E.g, TaxRate is more meaningful in a formula than E21. Defined names also allow you to ensure that hyperlinks pointing to locations in a spreadsheet are updated appropriately when a spredsheet is modified.

[ More Info ]

[/os/windows/office/excel] permanent link

Fri, May 18, 2012 11:37 pm

Port Forwarding RDP With PuTTY

You can use PuTTY, which is a free implementation of Telnet and SSH clients for Windows and Unix platforms to log into a system via SSH and by using the SSH port forwarding functionality that PuTTY provides, establish a Remote Desktop Protocol (RDP) connection through a "tunnel" you establish via the SSH connection.

[ More Info ]

[/os/windows/network/ssh/putty] permanent link

Sat, May 12, 2012 4:16 pm

Keep Header Row Visible in Excel When Scrolling

If you have an Excel spreadsheet with many rows, you may want to keep the header row visible even when you scoll far down through the rows of the spreadsheet, so you know what the values in each column represent. This can easily be done, if you have headings for each column in the top row of the spreadsheet, or near the top row, by going to the top of the spreadsheet, so that the header row is the top row of the Excel window. Then click on the cell in column A just below the header row to make it the current cell. Then click on Window and select Freeze Panes. Now, when you scroll down through the spreadsheet, the header row will "float" at the top of the Excel window, so that it is always visible and you don't have to scroll back to the top again to remind yourself what data is found in each column. If you have other rows above the column heading row you selected, those will remain visible as well as everything above the row you were on when you selected Freeze Panes.

If you later want to turn the display of the header row or rows when you scroll through the spreadhseet, click on Window and select Unfreeze Panes.

Note: this works in Microsoft Excel 2000 and later on Microsoft Windows systems and Microsoft Excel 2008 for Mac and should work on other versions of Excel as well.

[/os/windows/office/excel] permanent link

Sat, May 12, 2012 2:25 pm

Determining the Package for a File on a Ubuntu Linux System

To determine which installed package provides a file on a Ubuntu Linux system, you can use the command dpkg -S filename. If you need a particular file, but the package providing it is not installed, you can install the apt-file package and then use the command apt-file search filename to determine which package or packages provide it.

[ More Info ]

[/os/unix/linux/ubuntu] permanent link

Thu, May 03, 2012 10:58 pm

Time and Date Issue and mDNSResponder and configd messages

When I started a MacBook Pro laptop this morning, I found the date was set set to December 31, 2000, though it had the correct date the day before. I also received messages asking whether I wanted to allow configd and mDNSResonder to accept incoming network connections. I believe those messages appeared because the signing certificates for the two applications no longer appeared valid to the system because their signing dates were after the December 31, 2000 date the system was showing.

[ More Info ]

[/os/os-x] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo