perl

Reverse regular expression search

I have a string which contains "foo"s followed by numbers. I'm looking to print the number following the last foo in the string. I've been told that the only way to accomplish this is to reverse the string itself. This isn't very elegant, and I'm surprised Perl doesn't have a better way to get the job done. Is there a better way to do it...

Why do I get a number instead a list of filenames from readdir?

I have a piece of Perl code for searchnig a directory and display the contents of that directory, if match is found. The code is given below: $test_case_directory = "/home/sait11/Desktop/SaLT/Data_Base/Test_Case"; $xml_file_name = "sample.xml" $file_search_return = file_search($xml_file_name); print "file search return::$file_search_re...

Is it possible to pass parameters to a Perl module loading ?

Hello, I am currently developping a multi-environment perl script. As you all know, environment configuration juggling could be quite a pain if badly done. As my perl script must allow some command line parameters in a configuration value overload purpose, i came with the following solution : package Cfg; use strict; use warnings; my $...

How can I implement binary search in Perl?

I would like to implement a binary search algorithm in Perl. My 'array' is sorted in decreasing order (not an actual array, but a function that gets an index and returns values). the problem is that there might be stretches of identical values. If my searched value is in such a stretch, I want to return the first index that contains it. ...

Why is the return value of Perl's system not what I expect?

Let me begin by explaining what I'm trying to accomplish. Essentially there are two Perl scripts. One is what I call the Main script with an UI. The user who runs this script will see a list of other scripts he can call from the menu. This list is loaded through a custom config file. The purpose of the main script is to be able to add ot...

What modern Perl books should my company buy?

My manager told me that i can make a recommendation list for books they should buy for our department. Currently I am aiming at: Perl Best Practices Modern Perl Effective Perl What else am i missing? Edit: Selected additions from the answers: Higher Order Perl Perl Medic Perl Hacks Perl Testing: A Devoloper's Notebook ...

Why do I get "version (5.12.2) doesn't match executable version" with Perl's PAR?

When I run the following to create an executable out of my Perl script: pp -o process_target_mode_data Process_Target_Mode_Data.pl I get the following error output: Perl lib version (5.12.2) doesn't match executable version (v5.12.0) at /home/Neil/ActivePerl-5.12/lib/Config.pm line 50. Compilation failed in require at /home/Neil/Ac...

How does Perl variable scope work in while loop under strict mode?

I'm very new to Perl and I'm confused with exactly how its variable scope works. I'm trying to create an array of Hashes from the result of a MySQL query. The following code works, as intended, without use strict my %hash = (); while (my %hash = %{$qhand->fetchrow_hashref()} ) { push(@results, {%hash}); } but when strict ...

How do I concatenate arrays of aliases in Perl?

How do you concatenate arrays of aliases in Perl such that the resulting array also contains aliases? The solution that I came up with is: my ($x, $y, $z) = 1 .. 3; my $a1 = sub {\@_}->($x); my $a2 = sub {\@_}->($y, $z); my $a3 = sub {\@_}->(@$a1, @$a2); say "@$a3"; # 1 2 3 $_++ for $x, $y, $z; say "@$a3"; # 2 3 4 What I am n...

How can I extract a specific table and copy from MS Word to Excel in Perl?

I have an MS Word 2003 file which contains several tables in it and I want to extract a specific table contents. For example, tables will be coming under some sections and I want to extract the contents of the table that are coming under section 6 alone and no other table contents, I want copy those contents to an new Excel sheet with fo...

Unicode string mess in perl

I have an external module, that is returning me some strings. I am not sure how are the strings returned, exactly. I don't really know, how Unicode strings work and why. The module should return, for example, the Czech word "být", meaning "to be". (If you cannot see the second letter - it should look like this.) If I display the string,...

Can I use multiple keys in Berkeley DB ?

I want to use in Berkeley DB the following Perl logic (for many millions records): $hash{key1}{key2}{key3}{count1}++; $hash{key1}{key2}{key3}{count2}++; ... for (key1) { for (key2) { for (key3) { print $hash{key1}{key2}{key3}{count1}."\t".$hash{key1}{key2}{key2}{count2}; } } } Any example from multiple...

How can I send cookies with Perl's LWP::Simple?

use LWP::Simple; use HTML::LinkExtor; use Data::Dumper; #my $url = shift @ARGV; my $content = get('example.com?GET=whateverIwant'); my $parser = HTML::LinkExtor->new(); #create LinkExtor object with no callbacks $parser->parse($content); #parse content now if I want to send POST and COOKIE info as well with the HTTP header how can I co...

How should I handle errors inside or outside a Perl module?

I started learning how to make a module in perl with perltoot: package Person; use strict; my($NAME, $AGE, $PEERS) = ( 0 .. 2 ); sub new { my $self = []; $self->[$NAME] = undef; $self->[$AGE] = undef; $self->[$PEERS] = []; bless($self); return $self; } sub name { my $self = shift; if (@_) { $self...

How can I replace just the last occurrence in a string with a Perl regex?

Ok, here is my test (this is not production code, but just a test to illustrate my problem) my $string = <<EOS; # auto generated query SELECT users.* , roles.label AS role_label , hr_orders.position_label , deps.label AS dep_l...

How do I empty a Perl hash?

Possible Duplicate: How can I reset a hash completely without using a for loop? How do I empty a hash in Perl? I've got a code like this: my %hash; while (some_condition) { # do something if (deleting_condition) { # empty %hash } } My first idea was %hash={};, but this gives the hash a single value with ...

Unexpected speed behaviour when benchmarking Perl regexs

Whilst discussing the relative merits of using index() in Perl to search for substrings I decided to write a micro benchmark to prove what I had seen before than index is faster than regular expressions when looking for a substring. Here is the benchmarking code: use strict; use warnings; use Benchmark qw(:all); my @random_data; for (1...

How can I extract a pattern from all files in a directory, using Perl?

I am running a command which returns 96 .txt files for each hour of a particular date. so finally it gives me 24*96 files for one day in a directory. My aim is to extract data for four months which will result in 30*24*96*4 files in a directory. After I get the data I need to extract certain "pattern" from each of the files and display...

How can I parse incomplete XML fragments with Perl's XML::Twig?

I'm trying to extract data from log files in XML format. As these are huge, I am using XML::Twig to extract the relevant data from a buffer instead of the whole file(s) As these are concatenaded data from STDIN, the XML is far from well formed. So frequently the parser stops with an error. How can I get the XML parser to ignore the erro...

What does `?` mean in this Perl regex?

I have a Perl regex. But I'm not sure what "?" means in this context. m#(?:\w+)# What does ? mean here? ...