Let's try to implement something like Python's with statement in Perl.
First, let's look at Python. Python lets you do something like this:
class controlled_execution:
def __enter__(self):
set things up
return thing
def __exit__(self, type, value, traceback):
tear things down
with controlled_execution() as thing:
some code
(code example stolen from effbot)
The idea is that when the with block exits, the __exit__ cleanup is automatically called. This is nice for dealing with files, for example:
with f = open("somefile"):
f.read()
... where the implicit __exit__ code closes the file for us.
Can we do this in Perl? Sure. We just don't have the luxury of using nicely named __enter__ and __exit__ functions. Here is an example:
package Dir;
use strict;
use warnings;
use Cwd;
sub cd {
my $dir = shift;
my $code = shift;
my $origdir = getcwd();
chdir($dir);
$code->();
chdir($origdir);
}
1;
Now the script that utilises it:
#!/usr/bin/env perl
use strict;
use warnings;
use Dir;
Dir::cd "..", sub {
my @files = glob("*");
print join("\n", @files);
};
This context manager is for "cd", which chages directory, runs all the code in the block, and as it exits it simply changes back to the original directory it was in. There you go! Simple context managers in Perl.
In Debian or Ubuntu you can install all apt provided Perl modules like so:
apt-cache search "\-perl" | grep -e '^lib' | awk '{ print $1 }' | xargs sudo apt-get install
If, before running that command, you just want to check what it would install, run this:
apt-cache search "\-perl" | grep -e '^lib' | awk '{ print $1 }'
(Obviously expensive in this context refers to time or resources.)
From perldoc -f use, we know that use Module; is the same as:
BEGIN { require Module; Module->import( LIST ); }
Code in BEGIN blocks is executed at compile time, not run time. ('Compilation' in perl refers to the compilation to internal bytecode format.)
Consider an expensive module that your script uses. Imagine that the functionality that this module provides is only used in one of your subroutines, and that this subroutine is not necessary run in every invocation of your script.
If you used the expensive module, it would be included every time your script is run. If, instead, you simply required this module within the subroutine that actually needs it, you could avoid the cost of including the module when you don't need it!
Consider this expensive-to-use module, Expensive.pm:
package Expensive;
sleep 5;
1;
Now consider use.pl:
#!/usr/bin/env perl
use strict;
use warnings;
use Expensive;
sub rarely_called {
# uses Expensive.pm's functionality here
return;
}
rarely_called if defined $ARGV[0];
versus require.pl:
#!/usr/bin/env perl
use strict;
use warnings;
sub rarely_called {
require Expensive;
Expensive->import();
# uses Expensive.pm's functionality here
return;
}
rarely_called if defined $ARGV[0];
Let's time them:
$ time ./use.pl
real 0m5.011s
user 0m0.008s
sys 0m0.008s
$ time ./use.pl xxx # see how both invocations of use.pl take the same amount of time
real 0m5.011s
user 0m0.008s
sys 0m0.004s
$ time ./require.pl # but when require doesn't call the sub it is fast!
real 0m0.010s
user 0m0.004s
sys 0m0.004s
$ time ./require.pl xxx
real 0m5.011s
user 0m0.012s
sys 0m0.000s
Oh, and in case you are wondering, no you can't just put the use statement in the sub... all uses are processed at compile time, so it would still be expensive even if that sub was never called.
To install a Perl module somewhere custom, simply:
$ perl Makefile.PL PREFIX=/your/prefix/here
$ make
$ make test
$ make install
Benchmarking tr vs. s:
$ perl -M'Benchmark qw(:all)' -E '$a= "fhkjds fhdj fhj fdjskfl";
> cmpthese(5000000,
> {"tr" => sub { $b=$a; $b =~ tr/ /_/ },
> "s" => sub { $b=$a; $b =~ s/ /_/g }})'
Rate s tr
s 1173709/s -- -78%
tr 5263158/s 348% --
This means that tr is faster for this type of operation (a simple string-wide character substitution)
If, when you look at the documentation for a core-builtin, like so:
$ perldoc -f system
you get some funny characters in the output (long dashes, 'smart' quotes, etc) - and you wish to remove them (to make copy-pasting of code easier,) then simply:
$ perldoc -tf system
The -t flag avoids any fancy formatting and just outputs plain text.
Just by using the utf8 pragma in your Perl, you can do things like this:
use Modern::Perl;
use utf8;
my $天 = 'sky!';
say $天;
for my $否 (split(/ /, "no no NO no! NO!")) {
say $否;
}
Seeing escape codes when you run perldoc?
Set your $PAGER environment variable up correctly:
export PAGER="less -R"
... between two Perlers today:
chromatic:
Some days the jerks and bugs and workarounds and fame-chasing pundits make me want to leave technology as a profession.
Larry Wall:
If you do leave, you will find jerks and bugs and workarounds and fame-chasing pundits in most other professions too.
chromatic
At least real life hides its backwards compatibility misfeatures in DNA where I can't get at it.
Haha :-)
I have set up this mapping:
nnoremap <silent> <F9> :%! perltidy -q -pbp<CR>
... now I just hit F9 and I'm set.
Django Python 960.gs Git Vim NetBSD Nginx
This is the blog of Brad Willis, a software engineer living in Brisbane.
Help
Latest entries
*BSD Agile Apache Apple apt Athletics Best-Practice Censorship Chrome Comedy Cool Crosswords Deployment Django English Exim Firefox Git Hardcore Health irssi Javascript Jira Languages Linux Makefile Mathematics Mobile Broadband Mutt MySQL NetBSD nginx Nokia OpenVZ OSX Perl Postfix Privacy Python Rant Requirements rsync Ruby Shell Slackware SQL SQLite SSH Standards Subversion Television Testing ThisBlog Vim VMWare (Fusion) VPN X zsh
gvim - Always open new files as new tabs
crontab - escape % (percentage)
OSX Google Chrome - start in incognito mode
SQLite date arithmetic
Postfix - delete message in mailq
Checking for exceptions in doctests
Homer's Curling Speech
retry in Python
Vim Makefile tabs
Centos (or RH) IPTables
Converting ssh2 public keys to openssh
Vim comment hints
Context managers in Perl
Dish rotation
Git - fixing commit user
apt stuff
Using shell variables in AWK
Linux - Too many open files
Tell gvim to save and quit... remotely
Vim - automatically remove whitespace at EOL
Python - relative paths from within modules
TV Aspect Ratios
Git - Which commits are in your branch only?
Subversion setup cheat sheet
Force detach a screen session
Modify sudo's use of environment variables
Install all Perl modules
Mutt - delete old messages
OpenVZ VPS and swap space
fail2ban on NetBSD for ssh
NetBSD - Using sup
Python - testing for a sys.exit
Python Best Practice Link Dump
Python script names
Perl - Using an expensive module
Speed of git clone
Perl Modules with Custom Prefix
Perl: tr vs. s
Brilliant sysadmin Reference
Why is GRUB better than LILO?