perl

How can I extract multiple lines from a file with Perl?

Field 1 : data 1 Field 2 : data 2 Field 3 : data 3 Field 4 : <start> ... .. <end> field 5 : data 5 In this format file content is there. I was trying to extract information and store it into hash . It works for single line but its not working for multi-line Please help on this ...

How do I prevent the system command in Perl from executing any shell commands?

How do I prevent the system command in Perl from executing any shell commands? ...

Using Perl, how do I compare dates in the form of YYYY-MM-DD?

I have an array with n strings in format of YYYY-MM-DD (Example, "2010-10-31"). How do I compare a date to the strings in this array? For example, delete the strings more than 30 day ago? ...

In Moose, how do you declare predicate and clearer methods when defining multiple attributes?

In Moose you can declare a group of attributes at once, assuming the initialization parameters are the same: has [qw( foo bar baz )] => ( is => 'ro', isa => 'Str', required => 1, ); This is a lovely feature that saves a ton of typing. However, I find myself puzzled about how define a predicate, clearer or even a builder m...

How can I invoke /sbin/iptables from a Perl CGI under taint mode?

When I invoke "sudo /sbin/iptables ..." in my Perl CGI scripts, I get the error: Insecure dependency in system while running with -T switch at usr/lib/perl5/vendor_perl/5.8.8/IPC/Run3.pm line 403 I tried to add "/sbin:/etc/sysconf:/etc/init.d" in $ENV{'PATH'} but still no success. Anybody has any idea? ...

How can I correct the path of a file to be OS specific in Perl?

Due to some messed up legacy code, I have $path = [OS specific base DIR name][hardcoded Linux file path] So on Linux, it is something like $path = /common/path/to/dir/pathtofile/name.extension but on Windows it becomes this $path = C:\path\to\dir\pathtofile/name.extension Some of the code fails on Windows because it is expectin...

Perl: Template for an anonymous array?

I have a Perl script to analyze many megabytes of data line-by-line. As an example, I will use the close of the Dow Jones Average by day for several lines. The data is read and the layout of the data in the file is straightforward. In this example, it is: Date Open High Low Close Volume Adj-Close As I read the data, many calculation...

Can I call an Expect script from Perl by passing an array as a parameter?

Is it possible to call an Expect script from Perl by passing an array as a parameter? If so, how to retrieve that array in Expect like we retrieve non-array data as: set value [lindex $argv 0] ...

How can I generate the SQL query using SQL::Abstract?

How do I generate the WHERE clause for this query using SQL::Abstract: SELECT COUNT(*) FROM table WHERE id = 111 AND NOT FIND_IN_SET(type, '1,2,3,4') AND status = 'pending'; What's the proper way to include conditions like WHERE FIND_IN_SET(type, '1,2,3,4')? ...

How do I unescape some backslashes but not all in Perl?

Hello. I am using tinyperl with the following script: @lines=<STDIN>; foreach (@lines) { s/\\(.)/($1 eq '"' or $1 eq '\\') ? $1 : '\\' . $1/eg; print; } I would like each backslash to be considered only with the following character, and remove the backslash only if the following character is a double quote or another backslash...

How can I generate random unique temp file names?

I am trying to create a temp file using the following code: use File::Temp ; $tmp = File::Temp->new( TEMPLATE => 'tempXXXXX', DIR => 'mydir', SUFFIX => '.dat'); This is create the temp file. Because of my permission issue, the other program is not able to write into file. So I just...

Why does Perl use the empty string to represent the boolean false value?

When evaluating an expression in a scalar (boolean) context, Perl uses the explicit value 1 as a result if the expression evaluates to true and the empty string if the expression evaluates to false. I'm curious why Perl uses the empty string to represent boolean false value and not 0 which seems more intuitive. Note that I'm not concern...

Advice on which language to use

I'm trying to create a web application which will get input from system. What this application should do is to listen what happens when some shell scripts are executing and reporting the status trough web. An example : I'm copying thousands of records with shell script, and while this is still executing I'd like pass the current stat...

Why is Moose code so slow?

I'm trying to parse a large XML file. I read it using XML::SAX (using Expat, not the perl implementation) and put all the second level and below nodes into my "Node" class: package Node; use Moose; has "name" => ( isa => "Str", reader => 'getName' ); has "text" => ( is => "rw", isa => "Str" ); has "attrs" => ( ...

How can I select certain elements from a Perl array?

I want to search for all elements in an array which have the same starting set of characters as an element in another array. To make it clear: @array = ("1a","9","3c"); @temp =("1","2","3"); I want to print only 1a and 3c. When I try to use the following program it prints out all the elements in the array instead of the two I want: f...

How do I fix this Perl code so that 1.1 + 2.2 == 3.3?

How do I fix this code so that 1.1 + 2.2 == 3.3? What is actually happening here that's causing this behavior? I'm vaguely familiar with rounding problems and floating point math, but I thought that applied to division and multiplication only and would be visible in the output. [me@unixbox1:~/perltests]> cat testmathsimple.pl #!/usr/...

How can I count the amount of spaces at the start of a string in Perl?

How can I count the amount of spaces at the start of a string in Perl? I now have: $temp = rtrim($line[0]); $count = ($temp =~ tr/^ //); But that gives me the count of all spaces. ...

What are some good natural language parsing tools for Perl?

I've heard that Perl is used a lot for NLP, but I can't find almost any good NLP tools for Perl. What are some good Perl NLP tools/resources? Python has NLTK. Java has OpenNLP. Does Perl have anything similar? This is really a general question, but if someone could also specifically address chunking and POS-tagging, that would be awesom...

Converting MySQL TEXT field with breaklines to XML by Perl script returns a malformed notation

I have a table in MySQL that has one field defined as TEXT. The information is fed to the database by a webform using a textarea. I'm using the following script to generate an XML with the information of the table: #!/usr/bin/perl use strict; use DBI; use XML::Generator::DBI; use XML::Handler::YAWriter; my $dbh = DBI->connect ("DBI:...

How can I convert of the unix date output across multiple time zones to UTC, in Perl?

In Perl, how would one efficiently parse the output of unix's date command, taking into account time zone, and also convert to UTC? I've read many similar questions on stackoverflow, but few seem to take into account parsing multiple time zones. Instead they seem to set the timezone manually and assume it to stay fixed. # Example Inpu...