Add line numbers to your source code with expand and some quick perl:
expand /etc/motd | perl -pe 's/^/\t=$.=\t/'
commandsexpandlanguagesperlprogramming
Here is a Perl version of Numbering A File:
perl -lne 'printf "%3d : %s\n", $., $_' filename > filename.out
enumerationperl
Here's a Perl script to back up a flickr photo set:
#!/usr/bin/perl -w
use Data::Dumper;
use Flickr::Photoset;
use Flickr::Photo;
use LWP::Simple;
use strict;
my $params = { api_key => 'your api key'};
my $info = {};
my $photoset = Flickr::Photoset->new($params);
# specify a photoset
if ($photoset->id({id => '72057594072478931'})) {
my $title = $photoset->title;
my $owner = $photoset->owner->real_name;
my $photos = $photoset->photos;
foreach my $p ( @$photos ) {
my $id = $p->id;
my $sizes = $p->sizes;
foreach my $s (@$sizes) {
if ( $s->{'label'} eq 'Original') {
$info->{$id} = {
source => $s->{'source'},
title => $p->title,
server => $p->server
};
my $ret = getstore(
$s->{'source'},
$p->title.'_'.$id.'.jpg'
);
print 'response was '.$ret.' for '.$p->title."/n";
}
}
}
}
backupdownloadeditflickrperlphotos
An essential part of any CGI written in perl that will push all the fatal errors to the browser instead of punting with the all-too-familiar 500 Internal Server error:
use CGI::Carp qw(fatalsToBrowser);
carpcgilanguagesmodulesperlprogramming
Type perldoc -f <function_name> for syntax help. Also, perldoc -q <regexp> will search question headings in the perlfaq[1-9] man pages.
commandsdocslanguagesmanpagesperlperldocprogramming
Output is day.month.year (e.g. 31.12.2006):
perl -le '($D,$M,$Y)=(localtime(time-86400))[3,4,5];printf("%.2d.%.2d.%.4d",$D,++$M,$Y+=1900)'
Example for month/day/year (e.g.: 12/31/2006):
perl -le '($D,$M,$Y)=(localtime(time-86400))[3,4,5];printf("%.2d/%.2d/%.4d",++$M,$D,$Y+=1900)'
Example for year/month/day/ (e.g.: 2007/12/31):
perl -le '($D,$M,$Y)=(localtime(time-86400))[3,4,5];printf("%.4d/%.2d/%.2d",$Y+=1900,++$M,$D)'
dateperlyesterday
If you get strange locale errors when running perl and GTK apps, run these commands and be sure to generate all en_US locales:
$ apt-get install localeconf $ dpkg-reconfigure locales
apt-getcommandsdebiandpkggtklocalelocaleconfperl
Here is a Perl version of Numbering A File:
perl -lne 'printf "%3d : %s\n", $., $_' filename > filename.out
enumerationlinesnumberingperl
If you need to figure out where to put a new perl lib, check out the @INC array:
$ perl -e "print qq(@INC)" /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/...
cpanlibsperlperl5.8
In Perl you may have an array you'd like to iterate over. This would work:
for $line (@array) {
print "line: $line\n";
}
Or you could use this abbreviated method with map():
print map ("line: $_\n") @array;
arraymapperlprint
With the following code you can be rid of control characters forever:
$string_with_ctrl_characters =~ tr/\040-\176/ /c;
asciicontrolperl
Given an array @slobber, replace all the CR-LF with CR:
foreach (@slobber) {
s/\015\012$/\n/; print;
}
dosforeachlanguagesline-endingsperlprogrammingsyntaxunix
You have an array of IP addresses but are only interested in the unique values. Since perl doesn't have a unique() function, we'll exploit the concept of unique keys in hashes:
#!/usr/bin/perl
my @ipAddresses = ('192.168.0.1', '192.168.0.27',
'192.168.0.1', '192.168.0.3');
print "Before: ", qq(@ipAddresses), "\n";
undef %saw;
@saw{@ipAddresses} = ();
@sorted = sort keys %saw;
print "After : ", qq(@sorted), "\n";
Resulting in:
Before: 192.168.0.1 192.168.0.27 192.168.0.1 192.168.0.3 After : 192.168.0.1 192.168.0.3 192.168.0.27
perlsortunique