|
You don't have to be writing Perl scripts to run complicated web sites
do use Perl effectively. Learning just a few one-liners can help you
a lot when you're running a Linux system.
get current GMT with perl
This one liner will get you the current Greenwich Mean Time:
perl -le "print scalar gmtime time"
use perl like sed
sed is a command line program that's an old standby on Linux systems.
It's primarily used to change text files by way of the command line. With
Perl you can do the same and with less typing!
Imagine you had a text file with a line: 'this is the summer of our
discontent'. You could change 'summer' to 'winter' very easily, like so:
perl -pi -e 's/summer/winter/g' file
This will overwrite that file, so if you'd rather have a backup,
then do this:
perl -p -i.bak -e 's/summer/winter/g' file
and the original file will now become file.bak. This trick also
works on multiple files using wildcards.
add new line to a file ... if there's space
You can use perl to add a line to file. The following adds the text 'this
is important information' to line 5 of the file but only if that line is blank.
perl -ple 's//this is important information/ if $. == 5; close ARGV if eof' file > newfile
change files to lowercase
At times, people send us files from different systems. The file names
may be in all UPPERCASE letters and you might want to change them to
lowercase. This perl one-liner will do that for you:
Go to the directory with the files and issue this command:
perl -le 'rename $_, lc( $_ ) foreach <*>'
By substituting 'lc' for 'uc', you do the reverse.
create random password
It's always a good idea to have very secure passwords. This
Perl command will output 8 random characters that will be a good secure
password, though probably not easy to remember.
perl -le 'print map { (a..z,A..Z,0..9)[rand 62] } 0..pop' 7
install CPAN modules
You don't have to a be a Perl guru to need to install Perl modules.
A module is an add-on to Perl. These add-ons are there so developers
don't have to reinvent the wheel, so to speak.
Some very common programs make use of modules and without them, you won't
be able to use them. Here's a basic explanation of installing Perl
modules. We'll be getting them from CPAN, the Comprehensive Perl
Archive Network.
First, in an xterm or terminal, open a CPAN shell
perl -MCPAN -e shell
The first time, it will ask you to configure it. If you want automatic
configuration, just answer [no]. This will probably not have any ill
effects.
Then you can install Perl modules like so
install Module_name::Bla.pm
If you have problems reaching the default site, use a search engine to
look up a CPAN mirror. Then, in the CPAN shell, enter the mirror like so:
o conf urllist push ftp://ftp.someplace.org/mirror/CPAN/
[ return to main tips page ]
|