perl

Should I turn on Perl warnings with the command-line switch or pragma?

Is there a difference between the two examples below for beginning a Perl script? If so, when would I use one over the other? example 1: #!/usr/bin/perl use warnings; example 2: #!/usr/bin/perl -w ...

What's the simplest way of adding one to a binary string in Perl?

I have a variable that contains a 4 byte, network-order IPv4 address (this was created using pack and the integer representation). I have another variable, also a 4 byte network-order, subnet. I'm trying to add them together and add one to get the first IP in the subnet. To get the ASCII representation, I can do inet_ntoa($ip&$netmask...

How do I get the length of a string in Perl?

What is the Perl equivalent of strlen()? ...

Perl ADO thinks printed output in stored procedure is an error!

First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built for MSWin32-x86-multi-thread). I've just emerged from a three hour long debugging session, trying to find the source of an error. I found there was simply no error, but for some reason ADO's connection object was getting the Errors.Count increased with e...

Elegant structured text file parsing

I need to parse a transcript of a live chat conversation. My first thought on seeing the file was to throw regular expressions at the problem but I was wondering what other approaches people have used. I put elegant in the title as i've previously found that this type of task has a danger of getting hard to maintain just relying on reg...

Should the timezone be a constant or a variable?

I have a few places in the code where I need to use the TimeZone. I can get the timezone name using DateTime::TimeZone. Is it reasonable to put the timezone name in a constant? Or should it be in a variable? ...

How can I parse a Certificate Signing Request with Perl?

I want to use Perl to extract information from a Certificate Signing Request, preferably without launching an external openssl process. Since a CSR is stored in a base64-encoded ASN.1 format, I tried the Convert::PEM module. But it requires an ASN.1 description of the content, which I haven't been able to put together (ASN.1 being the be...

How can I remove an entire HTML tag (and its contents) by its class using a regex?

Hi, I am not very good with Regex but I am learning. I would like to remove some html tag by the class name. This is what I have so far : <div class="footer".*?>(.*?)</div> The first .*? is because it might contain other attribute and the second is it might contain other html stuff. What am I doing wrong? I have try a lot of set wit...

How can I copy a directory recursively and filter filenames in Perl?

How do I copy a directory including sub directories excluding files or directories that match a certain regex on a Windows system? ...

How can I reduce duplication in constants?

I have this Perl script with many defined constants of configuration files. For example: use constant { LOG_DIR => "/var/log/", LOG_FILENAME => "/var/log/file1.log", LOG4PERL_CONF_FILE => "/etc/app1/log4perl.conf", CONF_FILE1 => "/etc/app1/conf...

How can I get a call stack listing in Perl?

Is there a way I can access (for printout) a list of sub + module to arbitrary depth of sub-calls preceding a current position in a Perl script? I need to make changes to some Perl modules (.pm's). The workflow is initiated from a web-page thru a cgi-script, passing input through several modules/objects ending in the module where I nee...

What is Perl's equivalent to PHP's print_r()?

I find print_r in PHP extremely useful, but wonder if there is anything remotely equivalent in Perl? ...

How do I cleanly extract MySQL enum values in Perl?

I have some code which needs to ensure some data is in a mysql enum prior to insertion in the database. The cleanest way I've found of doing this is the following code: sub enum_values { my ( $self, $schema, $table, $column ) = @_; # don't eval to let the error bubble up my $columns = $schema->storage->dbh->selectrow_hashr...

What is the best way in Perl to copy files into a yet-to-be-created directory tree?

What is the best way in Perl to copy files to a yet to be created destination directory tree? Something like copy("test.txt","tardir/dest1/dest2/text.txt"); wont work since the directory tardir/dest1/dest2 does not yet exist. What is the best way to copy with directory creation in Perl? ...

What are some code coverage tools for Perl?

Are there any good (and preferably free) code coverage tools out there for Perl? ...

Is it possible use or require a Perl script without executing its statements?

I need to add unit testing to some old scripts, the scripts are all basically in the following form: #!/usr/bin/perl # Main code foo(); bar(); # subs sub foo { } sub bar { } If I try to 'require' this code in a unit test, the main section of the code will run, where as I want to be able to just test "foo" in isolation. Is there a...

RegSvr32 registering yet nothing actually registered

A rather odd experience. Using the latest PDK (v7.3) from ActiveState, I used perlctrl to build a COM DLL. Perlctrl ran without a hitch. OLEView read the typelib okay. RegSvr32 registered it okay. However ... there's no sign of it in registry, and anything that tries to use it fails. I hunted for the various UIDs using RegEdit and they'r...

How can I make my regex match the first pattern instead of the last?

I'm probably doing this all wrong. I have a text file full of data and I want to match and replace patterns of "item" and "catalog number" that are in the file. But the order of each element in the file is very important, so I want to match/replace starting from the top of the file and then work my way down. The code snippet below actua...

How do I load a file into a Perl hash?

Given the following file: department=value1 location=valueA location=valueB department=value2 I use the following to load the file into a Perl hash: use File::Slurp; use Data::Dumper; my %hash = map { s/#.*//; s/^\s+//; s/\s+$//; m/(.*?)\s*=\s*(.*)/; } read_file($file); print Dumper(\%hash); The result, however, is as f...

How can I fit a curve to a histogram distribution?

Someone asked me a question via e-mail about integer partitions the other day (as I had released a Perl module, Integer::Partition, to generate them), that I was unable to answer. Background: here are all the integer partitions of 7 (the sum of each row equals 7). 7 6 1 5 2 5 1 1 4 3 4 2 1 4 1 1 1 3 3 1 3 2 2 3 2 1 1 3 1 1 1 1 2 2 2 1 ...