perl

What's the safest way to iterate through the keys of a Perl hash?

If I have a Perl hash with a bunch of (key, value) pairs, what is the preferred method of iterating through all the keys? I have heard that using each may in some way have unintended side effects. So, is that true, and is one of the two following methods best, or is there a better way? # Method 1 while (my ($key, $value) = each(%hash)...

Can you force either a scalar or array ref to be an array in Perl?

I have a perl variable $results that gets returned from a service. The value is supposed to be an array, and $results should be an array reference. However, when the array has only one item in it, $results will be set to that value, and not a referenced array that contains that one item. I want to do a foreach loop on the expected arr...

Is The Perl Journal available online?

Does anyone know where online copies of the old The Perl Journal articles can be found? I know they are now owned by Dr. Dobb's, just the main page for it says they are part of whatever section the subject matter is relevant too, rather than being indexed together. That said, I have never been able to find any of them online on that sit...

How do I remove duplicate items from an array in Perl?

I have an array in Perl: @my_array = ("one","two","three","two","three") How do I remove the duplicates from the array? ...

How do you create objects in Perl?

Perl has OOP features, but they are somewhat rarely used. How do you create and use Perl objects with methods and properties? ...

Class::DBI-like library for php?

I have inherited an old crusty PHP application, and I'd like to refactor it into something a little nicer to deal with, but in a gradual manner. In perl's CPAN, there is a series of classes around Class::DBI that allow you to use database rows as the basis for objects in your code, with the library generating accessor methods etc as appr...

Why does the Perl conditional operator not do what I expect?

This snippet of Perl code in my program is giving the wrong result. $condition ? $a = 2 : $a = 3 ; print $a; No matter what the value of $condition is, the output is always 3, how come? Edit: I wonder if Perl is alone in this regard. Does your favorite language suffer from this bug/feature ? ...

How do you retrieve selected text using Regex in C#?

How do you retrieve selected text using Regex in C#? I am looking for c# code that is equivalent to this perl code: $indexVal = 0; if($string =~ /Index: (\d*)/){$indexVal = $1;} ...

Parsing attributes with regex in Perl

Here's a problem I ran into recently. I have attributes strings of the form "x=1 and y=abc and z=c4g and ..." Some attributes have numeric values, some have alpha values, some have mixed, some have dates, etc. Every string is supposed to have "x=someval and y=anotherval" at the beginning, but some don't. I have three things I need ...

How can I determine the type of a blessed reference in Perl?

In Perl, an object is just a reference to any of the basic Perl data types that has been blessed into a particular class. When you use the ref() function on an unblessed reference, you are told what data type the reference points to. However, when you call ref() on a blessed reference, you are returned the name of the package that refe...

Why doesn't my Perl map return anything?

When I am running the following statement: @filtered = map {s/ //g} @outdata; it is returning an empty list instead of the filtered list that I expected. What I am trying to do is remove every occurance of   from an array of string (which is an XML file). Obviously, I am not understanding something. Can anyone tell me the...

How do I tell if a variable has a numeric value in Perl?

Is there a simple way in Perl that will allow me to determine if a given variable is numeric? Something along the lines of: if (is_number($x)) { ... } would be ideal. A technique that won't throw warnings when the -w switch is being used is certainly preferred. ...

How can I test STDIN without blocking in Perl?

Okay, I feel like a total newb having to ask this question, but I guess I am one. I'm writing my first Perl app -- an AOL Instant Messenger bot that talks to an Arduino microcontroller, which in turn controls a servo that will push the power button on our sysadmin's server, which freezes randomly every 28 hours or so. I've gotten all ...

How can Perl's system() print the command that it's running?

In Perl, you can execute system commands using system() or `` (backticks). You can even capture the output of the command into a variable. However, this hides the program execution in the background so that the person executing your script can't see it. Normally this is useful but sometimes I want to see what is going on behind the sce...

How do I read in the contents of a directory in Perl?

How do I get perl to read the contents of a given directory into an array? Backticks can do it, but I know there is some method using 'scandir' or a similar term? ...

How do I perform a Perl substitution on a string while keeping the original?

In Perl, what is a good way to perform a replacement on a string using a regular expression and store the value in a different variable, without changing the original? I usually just copy the string to a new variable then bind it to the s/// regex that does the replacement on the new string, but I was wondering if there is a better way ...

Why can't I fetch wikipedia pages with LWP::Simple?

I'm trying to fetch Wikipedia pages using LWP::Simple, but they're not coming back. This code: #!/usr/bin/perl use strict; use LWP::Simple; print get("http://en.wikipedia.org/wiki/Stack_overflow"); doesn't print anything. But if I use some other webpage, say http://www.google.com, it works fine. Is there some other name that I shou...

Why can't I connect to my CAS server with Perl's AuthCAS?

I'm attempting to use an existing CAS server to authenticate login for a Perl CGI web script and am using the AuthCAS Perl module (v 1.3.1). I can connect to the CAS server to get the service ticket but when I try to connect to validate the ticket my script returns with the following error from the IO::Socket::SSL module: 500 Can't co...

What's the fastest way to determine a full URL from a relative URL (given a base URL).

I'm currently using URI::URL to generate this, however it is isn't as fast as I'd like it to be. Does anyone know another way to do this that may be faster? ...

Best way to extract data from a FileMaker Pro database in a script?

My job would be easier, or at least less tedious if I could come up with an automated way (preferably in a Python script) to extract useful information from a FileMaker Pro database. I am working on Linux machine and the FileMaker database is on the same LAN running on an OS X machine. I can log into the webby interface from my machine. ...