perl

evaluating a user-defined regex from STDIN in Perl

I'm trying to make an on-the-fly pattern tester in Perl. Basically it asks you to enter the pattern, and then gives you a >>>> prompt where you enter possible matches. If it matches it says "%%%% before matched part after match" and if not it says "%%%! string that didn't match". It's trivial to do like this: while(<>){ chomp; ...

How can I check if a regex pattern is valid in Perl?

Firstly, I was wondering if there was some kind of built in function that would check to see if a regex pattern was valid or not. I don't want to check to see if the expression works - I simply want to check it to make sure that the syntax of the pattern is valid - if that's possible. If there is no built in function to do so, how do I...

Perl equivalent of (Python-) list comprehension

I'm looking for ways to express this Python snippet in Perl: data = {"A": None, "B": "yes", "C": None} key_list = [k for k in data if data[k]] # in this case the same as filter(lambda k: data[k], data) but let's ignore that So looking at it one way, I just want the keys where the values are None or undef. Looking at it another way, ...

How can I create a 2-dimentional matrix and feed information from arrays in Perl?

I have four arrays @A1, @A2, @A3, @A4 each with same number of elements, say 6. I want to create a 2 dimensional array which stores the values of two array elements I want output corresponding to following format. For example Array1: A B C D E F Array2: E F G H I J Array3: Q W E R T Y Array4: P O L I U G Then the Matrix should be: ...

"Cannot decode string with wide characters" appears on a weird place

Hello, I am trying to use XML::RAI perl module on UTF8 coded text and I still have error I don't really understand... here is the code (it shouldn't do anything useful yet): use HTTP::Request; use LWP::UserAgent; use XML::RAI; use Encode; my $ua = LWP::UserAgent->new; sub readFromWeb{ my $address = shift; my $request = HTTP:...

In Perl, how can I tell if a string is a number?

I am using Perl to convert some XML to JSON. If the XML attribute is a number, I don't want to put quotes around it so that JSON will treat it as a number and not a string. How can I tell if a Perl string is a number (contains only numbers 0 through 9 and possibly one decimal point)? ...

How can I print a two-dimensional array in Perl?

I have a 2 dimensional array. When I print/Dump this I get the following My 2 dim array: push (@matrix, \@a1Comparea2); push (@matrix, \@a3Comparea4); a1Comparea2 should be first row of array a3Comparea4 should be second row of array $VAR1 = [ [ '1 6', '2 7', '3 8', '4 9', ...

Reading sections from a file in Perl

I am trying to read values from an input file in Perl. Input file looks like: 1-sampledata1 This is a sample test and data for this continues 2-sampledata2 This is sample test 2 Data for this also is on second line I want to read the above data so that data for 1-sampledata1 goes into @array1 and data for 2...

In Perl, how can I read parts of lines that match a criterion?

Sample Data: 603 Some garbage data not related to me, 55, 113 -> 1-ENST0000 This is sample data blh blah blah blahhhh 2-ENSBTAP0 This is also some other sample data 21-ENADT)$ DO NOT WANT TO READ THIS LINE. 3-ENSGALP0 This is third sample data node #4 This is 4th sample data node #5 ...

How can I convert Perl to C?

Is there a tool available which will convert source code in Perl to source code in C? Any platform is fine. ...

How can I programmatically determine my Perl program's memory usage under Windows?

I use ActivePerl under Windows for my Perl script, so I can look at how much memory it uses via the 'Processes' tab in Windows Task Manager. I find having to do this rather cumbersome. Is there another way to determine my Perl program's memory use? ...

Why does my Perl server fail to bind to port 80?

I copied the following script and run it to have it listen on port 80. But netstat doesn't show port 80. Why does netstat not sow it, or the Perl script is not correct? #!/usr/bin/perl -w use Socket; use IO::Handle; $port=80; $host='localhost'; $packhost=inet_aton($host); $address=sockaddr_in($port,$packhost); socket(SERVER,AF_I...

How can I use the "s///" operator in order to change number of values?

Hello friends, I have a file that contains: foo1 = 1 foo2 = 2 foo3 = 8 . . . I need to replace only the values (1,2,8...) in that file with part of a memory hash values, the ones with the same keys (foo1 -> 33,foo2 -> 44,foo3...) How can I change it using the "s///" operator? If there is other elegant ways to conduct it I'll b...

How can I convert a PHP upload-file script to Perl?

In PHP I have the following code that gets a file submitted through CGI: move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile) The file is being sent as Content-Disposition: form-data; name="userfile"; filename="filename" Content-Type: application/octet-stream The file being upload is of type PNG. How do I access the da...

Why does it seem like the * in Perl regex isn't being greedy?

I expected this to print "[b]" but it prints "[]": $x = "abc"; $x =~ /(b*)/; print "[$1]"; If the star is replaced with a plus, it acts as I expect. Aren't both plus and star supposed to be greedy? ADDED: Thanks everyone for pointing out (within seconds, it seemed!) that "b*" matches the empty string, the first occurrence of which i...

How can I recursively copy the contents of directory using Perl?

Hi guys. I am running the current version of ActivePerl on Windows Vista, and I was wondering if you could show me the best and simplest way to copy a folder and it's contents to another location. Contents would include various files, and most likely some more nested folders. I imagine there must be a module out there somewhere that I ...

How can I embed a Perl interpreter in my C# program using Mono on Linux?

Does anyone know if it's possible to call a Perl sub from Mono in C#? This is on a Linux machine. Maybe DllImport? We want to avoid loading perl every time if possible, as well. ...

How can I combine files into one CSV file?

If I have one file FOO_1.txt that contains: FOOA FOOB FOOC FOOD ... and a lots of other files FOO_files.txt. Each of them contains: 1110000000... one line that contain 0 or 1 as the number of FOO1 values (fooa,foob, ...) Now I want to combine them to one file FOO_RES.csv that will have the following format: FOOA,1,0,0,0,0,0,0....

How can I convert HTML to PDF using Perl?

I need to convert some HTML reports into PDF using Perl. What are the best CPAN modules for the job? ...

Why can't DBD::SQLite insert into a database through my Perl CGI script?

I am running a SQLite database within a Perl CGI script which is being accessed by DBD::SQLite. This is being run as a straight CGI on Apache. The DBI connection works fine and selects are able to be run. However, when I attempt to do an insert I get a die with the following error: DBD::SQLite::st execute failed: unable to open databa...