The Archives

  • 27.Mar.13
    Limit running time to a program shell | pfortuny | (0)
    There is no obvious way to specify that a program should run for no longer than x seconds. The following stackoverflow question solves it in a very perlish-way. Define a helper function doalarm: $ doalarm () { perl -e 'alarm shift; exec @ARGV' "$@"; } # define a helper function And you are done. $ doalarm 300 ./my_prog will run for at most 300 seconds.
  • 12.Nov.11
    Unicode sorting in perl shell | pfortuny | (0)
    Just Normalize. Assume file.txt is a list of unicode words, then cat file.txt | perl -e 'use Unicode::Normalize; my @w ;\ while () {chomp; push @w, $_;} ; @w = sort {NFD($a) cmp NFD($b) } @w ;\ print(join("\n", @w))' will output the sorted list (well, sorted according to the NFD normalization, which for Spanish is enough).
  • 18.Feb.11
    Moving between encodings with emacs automated, scripts, shell | pfortuny | (0)
    I live in Spain. I work at Academia. This is a problem. My School's computers are plagued with Windows (XP!), my pupils most certainly use Windows (I bet it is 7) and I use (obviously) an MBPro with Snow Leopard. Even more, my School's servers are Linux. This gives rise to latin-1, UTF-8 collisions. While writing some text-only documents for use with MATLAB (another problem, by the way) I realized I was going to need to convert them from my Mac's UTF-8 to Windows latin-1 every time I edited them. Time for a makefile. I am not commenting this though, only the ...
  • 28.Feb.09
    Using $|=1 in real life, for squidGuard shell | pfortuny | (0)
    In a previous minipost I mentioned Perl's $|=1 'autoflush output' option, which autoflushes all output. This is relevant in IPC more than in interactive scripts (these are USUALLY autoflushed, although any threaded program will show you differently). In the Squid configuration at a local firewall where authpf is used, the system does not only filter connections per-user (which is authpf's job), but it also authenticates them to squid via authpf. The relevant configuration lines in /etc/squid/squid.conf are: # next line needed to read the /var/authpf directory cache_effective_group authpf external_acl_type check_authpf children=15 %SRC /usr/local/bin/authpf.pl acl authpf external check_authpf They tell Squid to get the IP's (%SRC) ...
  • 22.Feb.09
    Perl directory traversal scripts | pfortuny | (0)
    The following chunk of code sub traverse { my ($dir, $callback) = @_; my $present; opendir $present, $dir or return(); for (grep { ! /^(\.|\.\.)$/ } readdir $present) { my $path = "$dir/$_"; &$callback($path); traverse($path, $callback) if -d $path; } closedir $present; return(); } is an easy implementation (without error-checking) of a generic directory-traversing function in Perl. It should be called traverse(dir, ...
  • 16.Feb.09
    Perl for accents and spaces in html hrefs automated, scripts, shell | pfortuny | (0)
    Despite the title looking like spam, it uses all the relevant keywords in a problem which I guess is more common than it looks like, especially in non-English speaking countries. I had created (automatically, not by hand) a set of html, pdf and rtf files of the 477 items of a regional catalogue of industrial estate (sorry, no references because it belongs to other people and they have not published it yet). Then I made the index.html page listing and linking to all those files. It looked like a list of lines like the following one (notice that what follows looks ...
  • 21.Jan.09
    Perl autoflush cmd, shell | pfortuny | (0)
    $ perl -e '$|=1; while(<>){print "you entered: $_";}' Even though in the above code it is irrelevant, the $|=1 assignment makes all the output be unbuffered (auto-flushed). This is quite useful in sockets, network protocols and IPC.
  • 16.Dec.08
    TTYtter: twitter client for real commandliners scripts, shell | rafacas | (2)
    If you are a twitter adict (I know you are already a commandliner ;) TTYtter is your client. TTYtter requires only Perl 5.005 or better, and either cURL or Lynx. It supports encryption as long as your Lynx or cURL does. If your client supports it you can configure TTYtter to use SSL changing the relevant URLs. These can be changed using TTYter options (-url, -rurl, -uurl, -wurl, -update, -dmurl and -frurl) or you can cut and paste the following code into your .ttytterrc file in your $HOME directory: url=https://twitter.com/statuses/friends_timeline.json rurl=https://twitter.com/statuses/replies.json uurl=https://twitter.com/statuses/user_timeline wurl=https://twitter.com/users/show update=https://twitter.com/statuses/update.json dmurl=https://twitter.com/direct_messages.json frurl=https://twitter.com/friendships/exists.json The default cURL's certificate bundle is old and may not support Twitter's current ...