perl

How do I call a function name that is stored in a hash in Perl?

I'm sure this is covered in the documentation somewhere but I have been unable to find it... I'm looking for the syntactic sugar that will make it possible to call a method on a class whose name is stored in a hash (as opposed to a simple scalar): use strict; use warnings; package Foo; sub foo { print "in foo()\n" } package main; my %...

Perl (or something else) - ^M problem

I'm trying to add " at beginning and ", at end of each non-empty line of text file in Perl. perl -pi -e 's/^(.+)$/\"$1\",/g' something.txt It adds " at beginning of each non-empty line, but i have problem with ",. Example input: bla bla bla blah That's output i'm getting: "bla ", "bla bla ", "blah ", And that's output i actuall...

How can I compile 64-bit Postgres bindings for Perl on Solaris?

I'm running 64-bit Solaris 10, and I have self-compiled Perl 5.10 and Postgresql 8.4.1 installed in /usr/local, both 64 bits. Solaris came with 32-bit Postgresql 8.1.4 installed in /usr, but it is not running. When I attempt to install DBD::Pg, it hits a problem because the libpq.so it finds is the 32-bit one in /usr/lib rather than the ...

How can I easily bulk rename files with Perl?

I have a lot of files I'm trying to rename, I tried to make a regular expression to match them, but even that I got stuck on the files are named like: File Name 01 File Name 100 File Name 02 File Name 03 etc, I would like to add a "0" (zero), behind any of file that are less than 100, like this: File Name 001 ...

Can I use the y operator to do a non-one-to-one transliteration in Perl?

The y operator in Perl does character-by-character transliteration. For example, if we do y/abc/dfg to the string "foobar", we get "foofdr". But what if I want to transliterate "ā" to "ei" and "ä" to "a:" and "ō" to "әu" and "o" to "ɒ". I tried the following line of code but no luck:( y/āäōo/(ei)(a:)(әu)ɒ/ Do we hopefully have a wor...

How should I parse large XML files in Perl?

Does reading XML data like in the following code create the DOM tree in memory? my $xml = new XML::Simple; my $data = $xml->XMLin($blast_output,ForceArray => 1); For large XML files should I use a SAX parser, with handlers, etc.? ...

How do I set the floating point precision in Perl?

Is there a way to set Perl script's floating point precision (to 3 digits), without having to change it specifically for every variable? Something similar to TCL's: global tcl_precision set tcl_precision 3 ...

How can I get the second-level keys in a Perl hash-of-hashes?

I need to get all of the values for a certain key in a hash. The hash looks like this: $bean = { Key1 => { Key4 => 4, Key5 => 9, Key6 => 10, }, Key2 => { Key7 => 5, Key8 => 9, }, }; I just need the values to Key4, Key5 and Ke...

What should I put in my starter template for my Perl programs?

I'm basing the majority on my Perl scripts on the following template/skeleton: #!/usr/bin/perl -w use strict; use utf8; $| = 1; binmode(STDOUT, ":utf8"); # my code goes here. The things achieved by this template: Enable warnings (-w) Enabling strict mode (use strict) Going pure UTF-8 (use utf8 + binmode(STDOUT, ":utf8")) Disable ...

How can I get file's modification date in DDMMYY format in Perl?

What's the best way to do it? EDIT: Originally the question was "How to do it without the DateTime module". I removed this restriction in order to make the question more general. ...

Save chart image with open flash chart2

I am using Open Flash Chart 2 to create some graphs. I want to be able to save an image of the graph, which OFC2 supplies some methods to accomplish this. I used the example on the OFC2 site to directly display the raw image data on the page, but that does not work in IE6, which most of our users are using (I know, I know). I switched...

Hashing Multiple Files

Problem Specification: Given a directory, I want to iterate through the directory and its non-hidden sub-directories,  and add a whirlpool hash into the non-hidden file's names. If the script is re-run it would would replace an old hash with a new one. <filename>.<extension>   ==>  <filename>.<a-whirlpool-hash>.<e...

Perl to PHP With s///;

In perl, I can do: 1 while $var =~ s/a/b/;, and it will replace all a with b. In many cases, I would use it more like 1 while $var =~ s/^"(.*)"$/$1/; to remove all pairs of double quotes around a string. Is there a way to do something similar to this in PHP, without having to do while (preg_match('/^"(.*)"$/', $var)) { $var = preg...

Is Tie::File lazily loading a file?

I'm planning on writing a simple text viewer, which I'd expect to be able to deal with very large sized files. I was thinking of using Tie::File for this, and kind of paginate the lines. Is this loading the lines lazily, or all of them at once? ...

How can I send raw IP packets with Perl under Windows?

Is there any Perl module which has the capability to send raw packets on Windows? I know there is Net::RawIP, but it seems that it does not work on Windows. ...

How could I redefine a subroutine and keep the old one too?

Here's what I'd like to achieve: sub first { print "this is original first"; } *original_first = \&first; sub first { print "this is first redefined"; } original_first(); # i expect this to print "this is original first" first() # i expect this to print "this is first redefined" I thought that by saving the symbol for first...

Why does Perl and /bin/sha1 give different results?

I'm confused as to why the following return separate sHA1s $ perl -MDigest::SHA1 -E'say Digest::SHA1::sha1_hex("http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg");' e1133fa3b7ea0bfb8ffa4d877932ed6c6fa10cef $ echo "http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg" | sha1sum 5c3731e83ae0184ed93b595b9f5604863dd331...

Should explicit character encoding and encoding/decoding be promoted as a "best practice" in Perl?

I previously only had vague awareness of character encoding issues, but answers to a question today got me thinking about it. The following provided more food for thought too: perlunitut - Perl Unicode Tutorial perlunifaq - Perl Unicode FAQ The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode a...

Is LWP::UserAgent not thread-safe?

I'm running 40-or-so threads with the following subroutine: my $app = shift; my $ua = LWP::UserAgent->new(); $ua->timeout(5); my $response = $ua->get($$app{'watch_url'}); my $new_md5; if ($response->is_success()) { $new_md5 = md5_hex($response->content()); } return ($$app{'short_name'}, $$app{'watch_md5'}, $new_md5); Core dumps en...

What's the difference between system() in C and Perl?

The system() function will launch a new process from C and a Perl script. What exactly are the differences between processes called by system() in C and from Perl scripts, in terms of representation of error codes? ...