perl

What is an appropriate data structure for a two-way name ↔ id relation?

I have a list of student names and their id. Sometimes I need to search name using id, sometimes I need to search id using name. If using array[id] = name, then it is quick to find a name using an id but slow to find the id using a name. If using hash{name} = id, then it is quick to find the id using a name but slow to find the name f...

Get hash keys/values on same line as the function call

Here is the code to reproduce the problem: sub hello { return (h => 1, n => 1); } print join ", ", values hello(); I get the error: Type of arg 1 to values must be hash (not subroutine entry) at - line 4, near ");" Execution of - aborted due to compilation errors. I know I can break the call and the print on two lines: su...

Perl using a sub call as argument to another sub - unexpected context

I am using a sub call as an argument to another sub. Example code: test(isInString(), 'second parameter', 'third parameter'); sub test { my ($boolean, $second, $third) = @_; print "boolean: $boolean\n second: $second\n third: $third\n"; } sub isInString { my $searchFor = 'a'; my $searchIn = 'bcd'; return ($s...

How do you handle multiple files in a form submission using Apache2::Upload?

I'm writing a small web application using Perl, HTML::Mason and Apache. I've been using Mason's usual <%args> method for receiving 'normal' form parameters, and Apache2::Upload for receiving files. However, I want to write a page that allows a user to upload multiple files, and I'd like to take advantage of HTML5's multiple attribute t...

Date::ICal mystery (hour does not return the correct value

Here's the code: $ical = Date::ICal->new( year => 1964, month => 10, day => 16, hour => 20, min => 12, sec => 47, #tz => '0530' ); ok( defined $ical, 'new() returned something' ); ok( $ical->isa('Date::ICal'), " and it's the right class" );...

What is the Java equivalent of Perl's qq operator?

I have a very long string which includes many new lines ( it's a really long SQL statement ). The SQL is easier to read when I break it up with newlines. But from time to time, I need to copy the sql statement from code to paste into sql developer. In Perl, I always loved the qq operator, which you can use in place of double quotes: ...

How do I convert passed flags/arguments to my Perl program without parsing them myself?

Hi, I would like to pass a perl program a set of arguments and flags, e.g. my_script.pl --flag1 --arg1=value --flag2 … Is there a way to quickly convert all of these into some standard structure (hash) instead of parsing? Thanks, Dave ...

How can I model an is-a relationship with DBIx::Class?

With the following (simplified) MySQL table definitions: create table items ( item_id int unsigned auto_increment primary key, purchase_date date ) engine = innodb; create table computers ( item_id int unsigned primary key, processor_type varchar(50), foreign key item_fk (item_id) references i...

How do I best use a library whose directory may change occasionally?

Hi, I am writing Perl scripts and when I have too many functions, I usually move them all into a library (also good for code reuse). So I usually create a package (e.g. my_lib.pm) and add use lib 'path/to/lib'; use my_lib; to my script. I wonder if it's possible to skip the use lib 'path/to/lib';, which sometimes gives me trouble sinc...

How do I receive command output immediately?

Hi, I'm using perl back-ticks syntax to run some commands. I would like the output of the command to written to a file and also printed out to stdout. I can accomplish the first by adding a > at the end of my back-ticked string, but I do not know hot to make the output be printed as soon as it is generated. If I do something like prin...

Perl and external programs

Hi, I have a Perl program and a C program. I want to run the Perl program and capture the return value of C program. To make it clear: C program (a.out) int main() { printf("100"); return 100; } Perl program: print `ls`; #OK print `a.out`; #No error but it does not print any output. Any ideas? Thanks. ...

Character number of match in Perl

Hello all, How can I make Perl to tell me the character number of a match, for example in a text file I have: CHI (3) - NSH (1) DAL (4) - CHI (3) VAN (3) - CHI (2) Want I want to get is for CHI the character number in which appears, for example: Line 1: 0 Line 2: 9 Line 3: 9 Any ideas or hints?. ...

Use perl to find the run time of an AIFF Audio File

What can I use to determine the run time of an AIFF audio file. Preferably using Perl. ...

What recommended multi-thread managers exist for Perl?

I'm new to multithreading in Perl and looking for something similar to Java's thread pools. Any recommendations? ...

How to get key from the reference of a hash element

suppose $my_ref = \$hash{'mary'}; #my_ref is a reference point to a hash element. .... later on, how can I use $my_ref to retrieve the key of the hash element it point to? i.e how to get string 'mary' from $my_ref? I ask this question because I have several groups of user name list, some user names appear in multiple groups which consum...

Why I can't run perl from Textmate?

#!/usr/bin/perl -w use WWW::Mechanize; print $WWW::Mechanize::VERSION."\n"; 1) run from Textmate : Can't locate WWW/Mechanize.pm in @INC (@INC contains: /Applications/TextMate.app/Contents/SharedSupport/Bundles/Perl.tmbundle/Support /Library/Perl/Updates/5.10.0 /System/Library/Perl/5.10.0/darwin-thread-multi-2level /System/Library/Per...

How to clear perl hash

Let's say we define an anonymous hash like this: my $hash = {}; And then use the hash afterwards. Then it's time to empty or clear the hash for reuse. After some Google searching, I found: %{$hash} = () and: undef %{$hash} Both will serve my needs. My question is, what's the difference between the two? Are they both identical w...

make perl shout when trying to access undefined hash key

Hi, I think the title is self-explanatory. Many times I have small typos and I get unexpected results when trying to access undefined hash keys. I know I can add some defined check before each time I access a hash key, but I wonder if there's any cleaner way to warn against such cases.... Best, Dave ...

Perl: Global substitution within tag-delimited string

My goal is to replace all instances of a trailing - to a trailing + within tag brackets. Lets assume the line to be replaced looks like this: <h> aa- aa- </h> <h> ba- ba- </h> and should afterwards look like <h> aa+ aa+ </h> <h> ba+ ba+ </h> First I tried this expression: s/<h>(.*?)-(.*?)<\/h>/<h>$1+$2<\/h>/g; which yielded th...

Perl: Global substition within tag delimited string

My previous post got the tags partially stripped, so here it is again: My goal is to replace all instances of a trailing - to a trailing + within tag brackets. Lets assume the line to be replaced looks like this: <h> aa- aa- </h> <h> ba- ba- </h> and should afterwards look like <h> aa+ aa+ </h> <h> ba+ ba+ </h>. First I tried th...