parsed.org

Tips by tag: perl

Add Line Numbers by xinu on Dec 31, 2005 04:07 PM

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
Backup a Flickr Photo Set by mandric on Mar 05, 2007 08:19 PM

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
Fatals to Browser by xinu on Sep 10, 2005 12:21 AM

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
Function Help by xinu on Aug 30, 2005 03:37 PM

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
Locale Fix by cygnus on Jan 12, 2005 11:04 AM

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
Perl Include Paths by xinu on Oct 25, 2007 02:38 PM

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
Processing An Array by xinu on Apr 02, 2008 12:34 AM

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
Remove Control Characters with Perl by xinu on Aug 28, 2008 08:56 AM

With the following code you can be rid of control characters forever:

$string_with_ctrl_characters =~ tr/\040-\176/ /c;
asciicontrolperl
Replace CR/LF with CR by xinu on Jun 07, 2005 11:21 AM

Given an array @slobber, replace all the CR-LF with CR:

foreach (@slobber) {
   s/\015\012$/\n/; print;
}
dosforeachlanguagesline-endingsperlprogrammingsyntaxunix
Unique Perl Sort by xinu on Dec 13, 2007 12:51 PM

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
RSS