perl

How to copy all the files of a particular type using Perl?

Right now, I am using something like copy catfile($PATH1, "instructions.txt"), catfile($ENV{'DIRWORK'}); individually for each of the .txt files that I want to copy. This code is portable as it does not use any OS specific commands. How can I copy all the text files from $PATH1 to DIRWORK, when I do not know the individual names of a...

How do I call pod2usage using my caller's documentation?

I'd like to be able to call pod2usage in a method that encapsulates a lot of standardized command line argument handling, but I can't see how I'd be able to put that handling into a separate module and have the pod2usage output be the documentation from the calling file. Is this possible? ...

Converting relative path into absolute path

Not sure if its a duplicate. Given the relative path, how do i determine absolute path using a shell script? Example: relative path: /x/y/../../a/b/z/../c/d absolute path: /a/b/c/d ...

Perl match outside "if" doesn't reset $1 on loop

I am working on patching the Zoidberg Perl shell to fix some errors that modern Perl versions throw when testing. Note, I am not the original author nor am I criticizing him. I came upon an interesting problem that I would like a little history on. The code below takes an array of inputs and for each strips off and ending sigil. Then i...

How can I pass a url or variable from Perl to PHP?

I have two existing scripts that work fine as individuals. The main script is Perl. I wish to execute the PHP script from a sub in the Perl script. Usually, the PHP script is just run via direct url e.g. http://me.com/phpscript.php?foo=bar I would like to just call the PHP script from the Perl and pass the foo var so, I don't need to ...

Using a pre-parsed protocol definition in a script and keeping it up-to-date

For my work, I sometimes have to deal with logfiles from a binary protocol (the logfiles contain hexdumps of the messages). I want to write a Perl script that can interpret the binary data for me and print the contents in a more friendly format. I have a (machine readable) description of the protocol messages in a proprietary format and...

Remove Perl modules from CPAN on Mac

As far as I know it is required to run CPAN with sudo on Mac sudo perl -MCPAN -e shell to install new modules. Theoretically, a module can be removed by deleting it from the Perl folders. My question is: Where are Perl modules put when installed from CPAN with 'sudo' and without 'sudo'? I installed BioPerl both ways and it seemed to ...

Perl: Running a "Daemon" and printing

I am running a script that runs 24/7. It just loops through over and over, very simple: while ($daemon) { sleep 60; chomp(my $currentDate = `date +%c`); print LOG "-- Running trigger: $currentDate --\n"; system("$triggerCmd >> $daemonLog"); print LOG "-- Completed trigger test. --\n\n"; } It works fine. ...

Merging inner hashes in Perl

I have 3 dimensional hash and a 2 dimensional hash, and I want to merge the 2 dimensional hash with one of the inner hashes of the 3 dimensional hash, something like this, which is similar to what I do to merge a pair of 2d hashes: my %3dhash; my %2dhash; my $key = "some string"; %3dhash{$key} = ($3dhash{$key}, %2dhash); But when I tr...

How to reclassify Perl object

I'm working with a few Perl packages, we'll call them Some::Parser and Some::Data. A Some::Parser object has methods to return objects of type Some::Data. I have written a class that extends the Some::Data class, let's call it My::Data. Objects of class My::Data are really just objects of class Some::Data, but with additional methods tha...

In Perl, how can I join elements of an array after enclosing each element in brackets?

I was trying to join elements of a Perl array. @array=('a','b','c','d','e'); $string=join(']',@array); will give me $string="a]b]c]d]e"; Is there anyway I can quickly get $string="[a][b][c][d][e]"; ? ...

In Perl, how can I return a tied hash from a subroutine?

I want to have a perl subroutine that creates and returns an ordered hash via the Tie::IxHash module. It looks something like this: sub make_ordered_hash { my @hash_contents = munge_input(@_); # I get a list of alternating keys and values tie(my %myhash, Tie::IxHash, @hash_contents); return %myhash; } Now, if I do my %orde...

"Variable $foo will not stay shared" Warning/Error in Perl While Calling Subroutine

Dear All, Update3: If you like this posting please don't upvote me but upvote the genius answer by DVK below. I have the following subroutines: use warnings; #Input my @pairs = ( "fred bill", "hello bye", "hello fred", "foo bar", "fred foo"); #calling the subroutine my @ccomp = connected_component(@pairs); use...

Calling C function from Perl within embedded C application

Ok, this is a very interesting question and there may not be any easy way to do this but figured I would throw this out there before deciding that modifying Perl is my underlying answer. So I've got a C application that calls Perl scripts in an embedded fashion. This all works fine and dandy and it's pretty awesome that I can pass info...

How can I reverse-engineer a Perl program that has been compiled with perlcc?

Hi, I inherited an environment that has a "compiled" perl script on Unix. Is it possible to de-compile, reverse engineer (whatever the term is) it, and obtain the source code from the compiled object code ? Might not be possible, but thought I'd ask rather than assume. Thanks, -Kevin. ...

How can I tell a Perl function that takes a file to read from the special ARGV handle?

In perldoc perlvar, I read this: Note that currently "ARGV" only has its magical effect within the "<>" operator; elsewhere it is just a plain filehandle corresponding to the last file opened by "<>". In particular, passing "*ARGV" as a parameter to a function that expects a filehandle may not cause your function to a...

Regex anchor question

Would an anchor like "^" or "\A" at the beginning of this regex make any sense - any difference? $string =~/(.*)([a-z])$/ ...

is it possible to read the text in a pdf file using perl ?

I want to parse the text from a pdf file in perl without converting the pdf into any other format . Is it possible ? ...

Can't find string terminator "str" anywhere before EOF

Why I get this error? use strict; use warnings; my $str = <<str; 88087 23/11/2010 35192 25/07/2010 B3J 5X9 17/08/2011 C8U 5L6 16/08/2011 F4Q 3B4 17/10/2010 D3X 9P4 11/05/2010 O7L 6Z8 28/02/2010 W8L 9P2 05/09/2010 str print $str; my @arr = split/\n/,$str; foreach (@arr) { my @tmp = split/\t/; print "$tmp[...

Implementing functional programming in Perl

I am trying to learn pure functional programming language like Haskell as I am from Perl background and read that Perl can also implement functional programming techniques. So few qusetions came in mind: Whether it is worth doing it in Perl 5? Does Perl 6 will make a difference? Can anybody suggest some code/examples implementing functi...