Perl Commands, Hacks, Tips and Examples
|
|
A quick way to install Perl modules is by using the CPAN shell. I recently needed to force an installation and it took some looking to find how to best accomplish it.
To install a module with the CPAN shell (for example; the SSH module I needed):
% perl -MCPAN -e 'install Net::SSH::Perl'
When a required module installation hung during the "make test' phase, I needed to force installation without 'make test' succeeding:
% perl -MCPAN -e CPAN::Shell->force(qw(install Net::SSH::Perl));
This time when the test hung, I used CTRL + C to crash out, at which point the installation continued. |
|
In any OS Perl can pass commands to the shell for execution. There are of course security considerations when using this functionality, but for our sys admin scripts, it usually doesn't much matter since we are merely trying to save some time and keystrokes. There are different ways to approach executing system commands depending on the value you are wanting returned.
The backtick (under the tilde on the keyboard) is the default encapsulation, but depending on your Perl editor of choice they might be easily confused with single quotes. Here are some examples for executing shell commands.
|
|
Read more...
|
|
One of the coolest features in Perl is that it conforms the slashes in a file path automatically. This really comes in handy when scripting for Windows since the backslashes used in file paths escape the following character and \\\\server\\share looks dumb.
Since Perl will turn the slashes around, all you need to type is: //server/share
When there are spaces in a directory name, the path is even harder to read: \\\\server\\share\\user\ used\ spaces\\to \create\\this\\directory
Using quote replacement with q helps even further: q[ //server/share/user used spaces/to create/this/directory ]
|
|
Here-docs or Here Documents for dumping data that would normally be difficult to do otherwise - like when there's a bunch of escape chars required.
It can also be used for mass population of arrays (not shown), and should be noted that the Perl here-doc is based on the unix shell scripting method. That's right, you can use almost the exact same syntax in a shell script.
|
|
Read more...
|
|
|
|
|
|