perl

Regular Expression to parse a string from end until a delimiter is reached(Perl)

Hello, I need a regular expression to parse a text The directory is /home/foo/bar/hello.txt. I would like to get the hello.txt and get rid of the rest of the directory, so i would like to delete home/foo/bar/ and only get the hello.txt ...

What does the function declaration "sub function($$)" mean?

I have been using Perl for some time, but today I came across this code: sub function1($$) { //snip } What does this mean in Perl? ...

Why do printf and sprintf behave differently when only given an array?

sub do_printf { printf @_ } sub do_sprintf { print sprintf @_ } do_printf("%s\n", "ok"); # prints ok do_sprintf("%s\n", "ok"); # prints 2 ...

How can I pad part of a string with spaces, in Perl?

Hello! Which version would you prefer? #!/usr/bin/env perl use warnings; use strict; use 5.010; my $p = 7; # 33 my $prompt = ' : '; my $key = 'very important text'; my $value = 'Hello, World!'; my $length = length $key . $prompt; $p -= $length; Option 1: $key = $key . ' ' x $p . $prompt; Option 2: if ( $p > 0 ) { $key = $...

How can I "store" an operator inside a variable in Perl?

I'm looking for a way to do this in Perl: $a = "60"; $b = "< 80"; if ( $a $b ) { then .... } Here, $b "holds" an operator... can I do this? Maybe some other way? ...

How can I build a generic dataset-handling Perl library?

Hello, I want to build a generic Perl module for handling and analysing biomedical character separated datasets and which can, most certain, be used on any kind of datasets that contain a mixture of categorical (A,B,C,..) and continuous (1.2,3,881..) and identifier (XXX1,XXX2...). The plan is to have people initialize the module and the...

How can I write to the same file in different subroutines in Perl?

I'm having some problems writing data into a file using Perl. sub startNewOrder{ my $name = makeUniqueFileName(); open (ORDER, ">$name.txt") or die "can't open file: $!\n"; format ORDER_TOP = PRODUCT<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<CODE<<<<<<<<AANTAL<<<<EENHEIDSPRIJS<<<<<<TOTAAL<<<<<<< . format ORDER = @<<...

How can I resume downloads in Perl?

I have a project that depends upon some other binaries to be downloaded from web at install time.For this what i do is: if ( file-present-in-src/) # skip that file else # use wget to download the file The problem with this approach is that when I interrupt a download in middle, and do invoke the script next time, the partially...

Why does Term::Size seem to mess up Perl's output encoding?

Hello! The Term::Size-module jumbles up the encoding. How can I fix this? #!/usr/bin/env perl use warnings; use strict; use 5.010; use utf8; binmode STDOUT, ':encoding(UTF-8)'; use Term::Size; my $string = 'Hällö'; say $string; my $columns = ( Term::Size::chars *STDOUT{IO} )[0]; say $columns; say $string; Output: Hällö 140 ...

How can I sort timestamps in Perl?

I have several thousand objects with a string property in the format of "yyyy-MM-ddTHH:mm:ssZ". I want to sort these objects by time. Are there any useful packages or scripts for this? (Currently I'm just comparing individual numeric values and it seems it's not very efficient and neat.) ...

What's the right way to kill child processes in perl before exiting?

I'm running an IRC Bot (Bot::BasicBot) which has two child processes running File::Tail but when exiting, they don't terminate. So I'm killling them using Proc::ProcessTable like this before exiting: my $parent=$$; my $proc_table=Proc::ProcessTable->new(); for my $proc (@{$proc_table->table()}) { kill(15, $proc->pid) if ($proc->ppid =...

How do I handle button presses in Gtk2::Image objects?

I've been trying to get an Gtk2::Image object in this Perl Gtk2 application to get to react to button presses, but to no avail. The image shows as expected but the button events don't get handled. What am I missing? my $img = Gtk2::Image->new_from_file( $file ); $img->set_property( sensitive => 1 ); $img->can_focus( 1 ); $img->...

Why do I get an error when inserting rows with Net::Cassandra::Easy and Cassandra 0.5x?

When using the Perl module Net::Cassandra::Easy to interface with Cassandra I use the following code to read colums col[123] from rows row[123] in the column-family Standard1: my $cassandra = Net::Cassandra::Easy->new(keyspace => 'Keyspace1', server => 'localhost'); $cassandra->connect(); my $result = $cassandra->get(['row1', 'row2', 'r...

Ruby on Rails, XML, AJAX, Perl Good Interview Questions

I am trying to devise a set of 15 to 25 questions to ask some of the people applying at our company. Can you guys throw in some good questions about Ruby, XML, Ajax, or Perl? This is for Junior position. I want just some easy questions, but at the same time they can be a little challenging. You know small answers, but requiring good k...

What's the easiest way to get an equivalent to GNU grep that supports negative lookbehinds?

I'm trying to grep through a bunch of files in nested subdirectories to look for regular expression matches; my regex requires negative lookbehind. Perl has negative lookbehind, but as far as I can tell GNU grep doesn't support negative lookbehinds. What's the easiest way to get an equivalent to GNU grep that supports negative lookbehi...

How can I log the HTTP response codes in the file in Perl?

I have created HTTP::Request which looks like this: #!/usr/bin/perl require HTTP::Request; require LWP::UserAgent; require HTTP::Cookies; $request = HTTP::Request->new(GET => 'http://www.google.com/'); $ua = LWP::UserAgent->new; $cookie_jar = HTTP::Cookies->new(); $ua->cookie_jar($cookie_jar); $cookie_jar->set_cookie(0,'testCookie','c...

How can I sort dates in Perl?

How can I sort the dates in Perl? my @dates = ( "02/11/2009" , "20/12/2001" , "21/11/2010" ); I have above dates in my array . How can I sort those dates? My date format is dd/mm/YYYY. ...

How can I copy a directory but ignore some files in Perl?

In my Perl code, I need to copy a directory from one location to another on the same host excluding some files/patterns (e.g. *.log, ./myDir/abc.cl). What would be the optimum way of doing this in Perl across all the platforms? On Windows, xcopy is one such solution. On unix platforms, is there a way to do this in Perl? ...

How do I install Perl's Lingua::Lid?

I am trying to install Lingua::Lid onto a unix system (ubuntu, latest version). Of course I am root. When I go into the package to install using perl Makefile.PL I get this dumb error: [root@csisl27 Lingua-Lid-0.01]# perl Makefile.PL /opt/ls//lib does not exist at Makefile.PL line 48. I have tried playing with the path on line 48, not...

How do I retrieve all columns for all matching rows with Perl's Net::Cassandra::Easy?

When using Perl's Net::Cassandra::Easy the following code will retrieve columns col[1-3] from rows row[1-3]: $result = $cassandra->get(['row1', 'row2', 'row3'], family => 'Standard1', byname => ['col1', 'col2', 'col3'); The corresponding SQL would be: SELECT col1, col2, col3 FROM rows WHERE id IN ('row1', 'row2', 'row3'); Suppose i...