perl

How do I handle a varying number of items from a database query?

Effectively a duplicate of: How can I display data in table with Perl The accepted answer there applies here. So do some of the alternatives. I am trying to run raw database queries from Perl program and display results to the user. Something like select * from table. I want to display the information in a HTML table. The columns i...

How do I make a DBIx::Class relationship with a fixed join condition?

We have a link table that can handle multiple types of object on one side, and I can't work out how to get from one of these objects to the link table using has_many. Example: link table contains: id link_id link_table resource_id 1 1 page 3 2 1 page 5 3 2 page 3 4 1 not_page 1 Buildi...

How must I declare the regex for Perl's split?

I came across this Perl construct today: @foo = split("\n", $bar); That works well for splitting a large string into an array of lines for UNIX-type line endings, but leaves a trailing \r for Windows. So I changed it to: @foo = split("\r?\n", $bar); Which splits the string by lines and doesn't leave a trailing \r (tested under Acti...

What's the best way to have two modules which use functions from one another in Perl?

Unfortunately, I'm a totally noob when it comes to creating packages, exporting, etc in Perl. I tried reading some of the modules and often found myself dozing off from the long chapters. It would be helpful if I can find what I need to understand in just one simple webpage without the need to scroll down. :P Basically I have two modul...

How do I preserve the setuid bit in tar archives with Perl's Archive::Tar?

I'm using Perl's Archive::Tar module. It preserves the file permissions but doesn't preserve the sticky bit. At the other end where I extract the archive, all the sticky bits are gone. I think UNIX/LINUX operating system stores these sticky bits somewhere else. How can I make my archive preserve sticky bits also? Using the -p switch to ...

How do I handle optional parameters in Moose?

I'm currently starting with Perl OOP using the "Moose" package. The compiler complains that it "Can't modify non-lvalue subroutine call at Parser.pm line 16." I don't quite understand why I can't just assign a new object. I guess there is a better or more valid way to do optional parameters with Moose? #!/usr/bin/perl -w package ...

How can I poll web requests without blocking?

I have two web requests which I need to poll to find out when they return. Ideally I don't want to keep testing them in a tight loop. I would like to free up the CPU so other processes can execute. I'm currently using Perl's Time::HiRes::sleep(0.100) function to release the CPU before testing whether or not the web requests have returne...

What's the fastest way to make concurrent web requests in Perl?

I need to make some concurrent XML feed requests in Perl. What is the fastest way to do this? ...

Why doesn't this jQuery.post work with Perl CGI?

I'm trying to figure out why I'm not seeing params with $.post("/url/", {wtf: 2}). I'm using this perl: use strict; use CGI; my $cgi = new CGI; print $cgi->header("text/javascript"); print "'no'"; use Data::Dumper; warn Dumper({ (map {$_=>$cgi->param($_ )} $cgi->param), postdata=>$cgi->param("POSTDATA") }); When I issue a $.get("/u...

What's the difference between iterating over a file with foreach or while in Perl?

I have a filehandle FILE in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following? while (<FILE>) { # do something } and foreach (<FILE>) { # do something } ...

How can I expand variables in a Perl string?

I am trying to expand the string $searchCriteria in the if condition. Any clues? use strict; my $argNum; my $searchCriteria = ""; foreach $argNum (0 .. $#ARGV) { $searchCriteria = $searchCriteria . "(\$_ =~ \/" . $ARGV[$argNum] . "\/i) && "; } $searchCriteria =~ s/&& $//; #print $searchCriteria; open IP, "<vm.txt" or die $!; my ...

How can I conditionally define a Perl subroutine?

I want to define a Perl function (call it "difference") which depends on a command-line argument. The following code doesn't work: if ("square" eq $ARGV[0]) {sub difference {return ($_[0] - $_[1]) ** 2}} elsif ("constant" eq $ARGV[0]) {sub difference {return 1}} It appears that the condition is ignored, and therefore the "difference" ...

Can I install a pre-configured Perl binary package in my home directory?

I want to set up Perl 5.10 in my home directory in addition to the ancient Perl currently on the machine in the /usr/local. I am not a super-user on this machine. I found the require 5.10 HPUX perl binary package and it seems to work, but for some reason it seems to assume it's running in /usr/local (as evidenced by @INC error messages...

How do I add /doc/ to the end of every URL in Catalyst?

We're trying to make our REST API a bit more friendly, We have a base class for our REST API which inherits from Catalyst::Controller::REST. Each REST class can identify the query parameters it accepts. We thought it would be nice to make this information public and put this into the base class: sub doc : Regex('/doc$') { my ( $...

Migrating from Moose to Mouse in Perl - Mouse not executing BUILD

I'm trying to migrate from Moose to Mouse in the interests of speed but have encountered a showstopper error. I'm building two objects in the same scope: sub scope { my $foo = Foo->new(); my $bar = Bar->new(); } The BUILD method of Foo is firing but the BUILD method of Bar is not. Any ideas? Both Foo and Bar inherit from Baz ...

How do I do a full-text search search of flat files with Perl?

We have a Perl-based web application whose data originates from a vast repository of flat text files. Those flat files are placed into a directory on our system, we extensively parse them inserting bits of information into a MySQL database, and subsequently move those files to their archived repository and permanent home (/www/website/a...

How do I stop auto-formatting in emacs cperl mode?

When I do indent-region in cperl-mode if ($x) { next; } Emacs reformats it to: if ($x) { next; } How can I make it stop doing that? Note: the question originally said that this reformatting happens when yanking. I have yank setup to indent-region as well. ...

When are Schwartzian Transforms useful?

While going through the "Intermediate Perl" book I noticed a section on Schwartzian Transforms and tried the example in the exercise (9.9.2) but noticed that multiple runs resulted in the transform taking more time then the normal sort. The code here performs a simple sort of the files in the windows\system32 directory based on file size...

How can I easily generate a Perl function depending on name of the importing class ?

I want to export a function which depends on name of class where is exported into. I thought that it should be easy with Sub::Exporter but unfortunately the into key is not passed to generators. I have ended up with those ugly example code: use strict; use warnings; package MyLog; use Log::Log4perl qw(:easy get_logger); use Sub::Expo...

Why can't Perl's PAR find the loadable object for Socket.pm?

I was using PAR::Packer to package my Perl application on Cygwin and then running it on HPUX. A simple hello world works well, e.g.: pp -p hello.pl That results in a.par and then on HPUX: parl a.par It works great. However when package a bigger application with many dependencies with -B bundle switch, no such luck, instead I get...