perl

How do I turn a table into a matrix?

If I got a table in a text file such like A B 1 A C 2 A D 1 B A 3 C D 2 A E 1 E D 2 C B 2 . . . . . . . . . And I got another symbol list in another text file. I want to transform this table into a Perl data structure like: _ A D E . . . A 0 1 1 . . . D 1 0 2 . . . E 1 2 0 . . . . . . . . . . But I only need some selected symbol...

How do I do a simple Perl hash equivalence comparison?

I'm wondering if there's an idiomatic one-liner or a standard-distribution package/function that I can use to compare two Perl hashes with only builtin, non-blessed types. The hashes are not identical (they don't have equivalent memory addresses). I'd like to know the answer for both for shallow hashes and hashes with nested collections...

Can I make a single Perl module act as multiple kinds of mod_perl handlers?

I'm writing a series of related mod_perl handlers for various login-related functions in Apache, so my Apache config file looks like this (for example) PerlAccessHandler MyApache::MyAccess PerlAuthenHandler MyApache::MyAuthen PerlAuthzHandler MyApache::MyAuthz Each of the modules (MyAccess, MyAuthen, MyAuthz) defines a sub handl...

How do you compare the content of two archive files programmatically?

Hi there, I'm doing some testing to ensure that the all in one zip file that i created using a script file will produce the same output as the content of a few zip files that i must manually click and create via web interface. Therefore the zip will have different folder structure. Of course i can manually extracted them out and using ...

How can I install a CPAN module into a local directory?

I'm using a hosted Linux machine so I don't have permissions to write into the /usr/lib directory. When I try to install a CPAN module by doing the usual: perl Makefile.PL make test make install That module is extracted to a blib/lib/ folder. I have kept use blib/lib/ModuleName but it still the compiler says module can not be found. ...

Is there *simple* way to extract deeply nested values with XML::Simple?

I'm using Perl's XML::Simple to parse deeply nested XML and would like to extract a small list of elements about 4 levels down: A B C D1 D2 D3 Ideally I want to do this on the input step, if possible. Like this: my @list = XMLin($xml, { SomeAttribute => 'ButWhat?' }); ending up with the same thing as if I d...

How can I tell if I'm running in a VMWARE virtual machine (from linux)?

I have a VMWARE ESX server. I have Redhat VMs running on that server. I need a way of programatically testing if I'm running in a VM. Ideally, I'd like to know how to do this from Perl. ...

Why does Apache complain that my mod_perl program "disconnect invalidates 1 active statement handle"?

disconnect invalidates 1 active statement handle (either destroy statement handles or call finish on them before disconnecting) The following code which grabs data from MySQL gets executed successfully, but will cause Apache to generate the above message in its error log: my $driver = "mysql"; my $server = "localhost:3306...

How do I include all/some of the "sub modules" in a Perl script?

I'll just start out by saying I am not at all experienced with creating Perl modules so I'm sorry if I'm way off here. Let's say I am creating a few modules: foo::bar foo::bar::a foo::bar::b Since I don't know what they are called, I am calling the a.pm and b.pm modules "sub modules" since they are related to the bar.pm module, but c...

How can I handle proxy servers with LWP::Simple?

How can I add proxy support to this script? use LWP::Simple; $url = "http://stackoverflow.com"; $word = "how to ask"; $content = get $url; if($content =~ m/$word/) { print "Found $word"; } ...

Add Quotes in url string from file

I need script to add quotes in url string from url.txt from http://www.site.com/info.xx to "http://www.site.com/info.xx" ...

How can I distinguish $_ in nested list operators in Perl?

It is often useful to implement algorithms using nested array operations. For example, to find the number of words in a list that start with each given character, you might do something like this in Python: >>> a = ["foo","bar","baz"] >>> map(lambda c: len(filter(lambda w: w.startswith(c), a)), ('a','b','c','d','e','f')) [0, 2, 0, 0, 0,...

How can I use a code ref as a callback in Perl?

I have the following code in my class : sub new { my $class = shift; my %args = @_; my $self = {}; bless( $self, $class ); if ( exists $args{callback} ) { $self->{callback} = $args{callback}; } if ( exists $args{dir} ) { $self->{dir} = $args{dir}; } return $self; } sub test { my ...

Why can't I say print $somehash{$var}{fh} "foo"?

I have a line of code along the lines of: print $somehash{$var}{fh} "foo"; The hash contains the filehandle a few levels down. The error is: String found where operator expected at test.pl line 10, near "} "foo"" I can fix it by doing this: my $fh = $somehash{$var}{fh}; print $fh "foo"; ...but is there a one-liner? ...

Which is more efficient regular expression?

I'm parsing some big log files and have some very simple string matches for example if(m/Some String Pattern/o){ #Do something } It seems simple enough but in fact most of the matches I have could be against the start of the line, but the match would be "longer" for example if(m/^Initial static string that matches Some String Pat...

How can I find the free space available on mounted points?

I have raised this question previously but none of the solutions work on the mounted points. Neither du nor df work on the mounted points. Is there a way to find it out? ...

Is returning a whole array from a Perl subroutine inefficient?

I often have a subroutine in Perl that fills an array with some information. Since I'm also used to hacking in C++, I find myself often do it like this in Perl, using references: my @array; getInfo(\@array); sub getInfo { my ($arrayRef) = @_; push @$arrayRef, "obama"; # ... } instead of the more straightforward version: my ...

How can I render undefined values from printf in Perl?

I'm looking for an elegant way to denote undefined values in situations where formatted numbers usually render. I'll work up a small example. For starters, you of course can't use this: #!/usr/bin/perl use strict; use warnings; for my $s (1, 1.2, undef, 1.3) { printf "%5.2f\n", $s; } ...because the 'use warnings' nails you with 'U...

How can I set up Eclipse to edit Perl without the runtime checking?

I'm working on a project that include both Java (on the client side) and Perl (on the server side), and I would really like to use Eclipse to edit my Perl scripts instead of going to another editor. I've tried to use the EPIC plugin. However, I have no way of setting up a run environment for these scripts on my local machine, so the plug...

How can I merge lines in a large, unsorted file without running out of memory in Perl?

I have a very large column-delimited file coming out of a database report in something like this: field1,field2,field3,metricA,value1 field1,field2,field3,metricB,value2 I want the new file to have combine lines like this so it would look something like this: field1,field2,field3,value1,value2 I'm able to do this using a hash. In t...