perl

How to localize a variable in an upper scope in Perl?

I have run across the following pattern a few times while developing Perl modules that use AUTOLOAD or other subroutine dispatch techniques: sub AUTOLOAD { my $self = $_[0]; my $code = $self->figure_out_code_ref( $AUTOLOAD ); goto &$code; } This works fine, and caller sees the correct scope. Now what I would like to do ...

Efficient way to continually read in data from a text file

We have a script on an FTP endpoint that monitors the FTP logs spewed out by our FTP daemon. Currently what we do is have a perl script essentially runs a tail -F on the file and sends every single line into a remote MySQL database, with slightly different column content based off the record type. This database has tables for content of...

Perl Regex syntax

I would like to use Perl to take a previously generated SPSS syntax file and format it for use in an R environment. This is probably a very simple task for those familiar with Perl and regex, but I am stumbling. The steps as I've laid them out for this Perl script are as follows: Read in SPSS file Find appropriate chunks of SPSS fil...

returning a lazily-computed scalar, in perl

I'm trying to add some functionality to our code base by using tied scalars. We have a function which is specified to return scalars. I thought I could add some features to the system by tie-ing these scalars before returning them, but it looks like the FETCH method is called just before the return, which results in an untied scalar bei...

How do I process captured output from `tail` without reprocessing?

Hi, I want to excecute a tail command in Unix for indefinite time and capture its output in a Perl script, process it and store certain data into a database. But it should be live, meaning old data – once stored in the database – shouldn't be reprocessed. It should only capture, and process only the most recent output. Can someone pleas...

How do I learn about programming fundamentals?

I am a beginner in programming. I have a large piece of code. I want to break it up into functions, return values, pass those values to another function. I want to be an expert in doing this. Give me suggestions on where to study or how to be an expertise on these simple fundamentals. When will I use pointers in the functions and all thi...

How to selectively start and end a persistent Apache::DBI for perl from the client/browser?

This question is related to our web-based application, which uses perl, apache, Apache::DBI and a database (MySQL, SQLite, or anything). We know that Apache::DBI is used to create persistent db connections. These connections live in memory from the time the Apache web server starts to the time it shuts down. My Question is: Is it poss...

java doesnt seem to read my perl piped output intime and is filling up

I have the following snippet calling a perl script which writes to STDERR and STDOUT. I've been following the recomended procedures such as auto flushing the STDOUT and STDERR in the perl script and using streamgobbler threads. I've noticed this to help my issue in to a degree but at times where the perl script generates large volumes of...

Sending Mail Through Perl

I am using the below code to send an email #!/usr/bin/perl sub BEGIN { unshift (@INC,'/opt/dev/common/mds/perlLib'); } use Mail::Sender; $sender = new Mail::Sender {smtp => 'xxx.xxx.x.xx', from => '[email protected]'}; $sender->MailFile({to => '[email protected]', subject => 'Here is the file', msg => "I'm sending you the list you wanted...

passing a data structure from java to perl (and vice versa)

Hi, A few days ago I asked about passing a data structure from java to perl and vice versa, and one of the recos was JSON. I played with it (mainly using Gson for java) and it seems quite nice. The only problem is I have references inside my data structure (to other objects inside the same data structure). Currently, each such reference...

Find combinations of numbers that sum to some desired number

I need an algorithm that identifies all possible combinations of a set of numbers that sum to some other number. For example, given the set {2,3,4,7}, I need to know all possible subsets that sum to x. If x == 12, the answer is {2,3,7}; if x ==7 the answer is {{3,4},{7}} (ie, two possible answers); and if x==8 there is no answer. Note t...

How do I collect the lines between two key words?

Hi, I have a file like: START Length: 1432 RIdentifier: 4 VIdentifier: 4 Format: 5 TS number: 9 DHeader Version = 1 Length = 1432 Command Flags = RPT (0xd0) Command Code = Accounting-Request (271) Application Id = Rf-Application (3) Hop By Hop Id = 51 End To End Id = 884...

How to reslove array hash issue

my %geo_location_map = ( US => [ 'US', 'CA' ], EU => [ 'GB', 'ES' ], ); $location= "US" ; my $goahead = 0; if (exists $geo_location_map{US} ) { print "exists"; my @glocation = $geo_location_map{US}; foreach @glocation { ...

Perl map/grep memory leak

I have been working on a perl project at work, and came across a strange memory leak. I have boiled down the source of my problem into a contrived example: #!/usr/bin/perl use strict; use warnings; # takes: an array reference # returns: 1 sub g { my ($a) = @_; return 1; } # takes: nothing # returns: the result of applying g ...

Push call in Perl overwriting array

Ok, here's the deal. I have an array (inputted from a 400 MB file) where if I run the sort() command on it, comp runs out of memory. The input file isn't the problem, so I've decided to break the initial array into smaller arrays that I can perform the sort on. I can break the initial array into arrays of size 100k, which my code does...

Sending text messages with perl?

Is there a way to send text messages for free or cheap with Perl? I see a lot of things on CPAN, but they all cost quite a bit. Is there anyway to send an email as a text message? ...

Dependencies in Perl code.

I've been assigned to pick up a webapplication written in some old Perl Legacy code, get it working on our server to later extend it. The code was written 10 years ago by a solitary self-taught developer... The code has weird stuff going on - they are not afraid to do lib-param.pl on line one, and later in the file do /lib-pl/lib-param....

How do I find "wide characters" printed by perl?

A perl script that scrapes static html pages from a website and writes them to individual files appears to work, but also prints many instances of wide character in print at ./script.pl line n to console: one for each page scraped. However, a brief glance at the html files generated does not reveal any obvious mistakes in the scraping. ...

How to write from n-th row to a file using perl

I have a source text in a file and looking for a code that would take the second (or n-th - in general) row from this file and print to a seperate file. Any idea how to do this? ...

match and replace multiple newlines with a SED or PERL one-liner

I have an input C file (myfile.c) that looks like this : void func_foo(); void func_bar(); //supercrazytag I want to use a shell command to insert new function prototypes, such that the output becomes: void func_foo(); void func_bar(); void func_new(); //supercrazytag So far I've been unsuccessful using SED or PERL. What didn't w...