perl

Missing output when running system command in perl/cgi file

I need to write a CGI program and it will display the output of a system command: script.sh echo "++++++" VAR=$(expect -c " spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD match_max 100000 expect \"*?assword:*\" send -- \"$PASS\r\" send -- \"\r\" expect eof ") echo $VAR echo "++++++" In CGI file: my $command= "ksh ../cgi...

In Perl, how can a subroutine get a coderef that points to itself?

For learning purposes, I am toying around with the idea of building event-driven programs in Perl and noticed that it might be nice if a subroutine that was registered as an event handler could, on failure, just schedule another call to itself for a later time. So far, I have come up with something like this: my $cb; my $try = 3; $cb = ...

Learning C++ as a Perl programmer

I'm a Perl5 programmer for 7 years and I'm trying to learn C++ now. Some of the C++ syntax is hard for me to understand and to think in C++ way. For example: In Perl, you can mix the data in the arrays @array = (1,"string",5.355); You can assign any value to a scalar variable: $var = 1; $var = "string"; $var = \$reference_to_scalar;...

Is it a bad idea to fork under mod_perl2?

Are there any counterindications to fork under mod_perl2? Should one use another way to run background process under mod_perl2? ...

Forcing a mixed ISO-8859-1 and UTF-8 multi-line string into UTF-8 in Perl

Consider the following problem: A multi-line string $junk contains some lines which are encoded in UTF-8 and some in ISO-8859-1. I don't know a priori which lines are in which encoding, so heuristics will be needed. I want to turn $junk into pure UTF-8 with proper re-encoding of the ISO-8859-1 lines. Also, in the event of errors in th...

How can I substitute the nth occurrence of a match in a Perl regex?

Following up from an earlier question on extracting the n'th regex match, I now need to substitute the match, if found. I thought that I could define the extraction subroutine and call it in the substitution with the /e modifier. I was obviously wrong (admittedly, I had an XY problem). use strict; use warnings; sub extract_quoted { # ...

Greenspun's 10th rule in Perl?

Greenspun's Tenth Rule of Programming is a common aphorism in computer programming and especially programming language circles. It states: Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp. The questions are, Would you consider this...

perl: tk: a way/widget that allows pixel level control over the output

I want something like a canvas, but where i'd be able to manipulate pixels easily in addition to all the provided geometries, that can be drawn on canvas. Is it possible to embed something like GD::Image into a canvas? So then I maybe could make the image transparent and set some pixels in it (GD::Image->setPixel()) positioning it over ...

Perl: Recursively rename all files and directories

I need to recursively rename every file and directory. I convert spaces to underscores and make all file/directory names to lowercase. How can I make the following script rename all files in one run? Currently the script needs to be run several times before all the files/directories are converted. The code is below: #!/usr/bin/perl use...

Is there a Perl idiom which is the functional equivalent of calling a subroutine from within the substitution operator?

Perl allows ... $a = "fee"; $result = 1 + f($a) ; # invokes f with the argument $a but disallows, or rather doesn't do what I want ... s/((fee)|(fie)|(foe)|(foo))/f($1)/ ; # does not invoke f with the argument $1 The desired-end-result is a way to effect a substitution geared off what the regex matched. Do I have to write sub lal...

How can I print lines that match a pattern in Perl?

Assuming file.txt has just one sentence per line as follows: John Depp is a great guy. He is very inteligent. He can do anything. Come and meet John Depp. The Perl code is as follows:- open ( FILE, "file.txt" ) || die "can't open file!"; @lines = <FILE>; close (FILE); $string = "John Depp"; foreach $line (@lines) { if ($lin...

Convert Memory Size (Human readable) into Actual Number (bytes) in Perl

Is there an actual package in CPAN to convert such string: my $string = "54.4M" my $string2 = "3.2G" into the actual number in bytes: 54,400,000 3,200,000,000 And vice versa. In principle what I want to do at the end is to sum out all the memory size. ...

How can I use `pipe` to facilitate interprocess communication in Perl?

Can anyone explain how I can successfully get my processes communicating? I find the perldoc on IPC confusing. What I have so far is: $| = 1; $SIG{CHLD} = {wait}; my $parentPid = $$; if ($pid = fork();) ) { if ($pid == 0) { pipe($parentPid, $$); open PARENT, "<$parentPid"; while (<PARENT>) { print $...

Convert Chunk of Data into Tabular Format Using Perl

I have a data that looks like this 1:SRX000566 Submitter: WoldLab Study: RNASeq expression profiling for ENCODE project(SRP000228) Sample: Human cell line GM12878(SRS000567) Instrument: Solexa 1G Genome Analyzer Total: 4 runs, 62.7M spots, 2.1G bases Run #1: SRR002055, 11373440 spots, 375323520 bases Run #2: SRR002063, 22995209 spots, 7...

How can I create PDF output from rrdcgi?

I have created a rrdcgi script to display information about the system performance with graphs. Now I would like to add an option for the users to create PDF on the fly with the details on current page (images and information) and header and footer. I also want the generated PDF files to be saved in some location so that that can be easi...

What does the DumpXS in Perl's Data::Dumper do?

I have gone through the source code of Data::Dumper. In this package I didn't understand what's going on with DumpXS. What is the use of this DumpXS? I have searched about this and I read that, it is equal to the Dump function and it is faster than Dump. But I didn't understand it. ...

Perl: How to get all grouped patterns

I have the following code. Here I am matching the vowels characters words: if ( /(a)+/ and /(e)+/ and /(i)+/ and /(o)+/ and /(u)+/ ) { print "$1#$2#$3#$4#$5\n"; $number++; } I am trying to get the all matched patterns using grouping, but I am getting only the last expression pattern, which means the fifth expression of the if condi...

How can I get the name of the current subroutine in Perl?

In Perl we can get the name of the current package and current line number Using the predefined variables like __PACKAGE__ and __LINE__. Like this I want to get the name of the current subroutine: use strict; use warnings; print __PACKAGE__; sub test() { print __LINE__; } &test(); In the above code I want to get the name of the ...

How do I use an array as an object attribute in Perl?

Hello guys, I need some help regarding the arrays in Perl This is the constructor I have. BuildPacket.pm sub new { my $class = shift; my $Packet = { _PacketName => shift, _Platform => shift, _Version => shift, _IncludePath => [@_], ...

Why doesn't DBI (DBD::Pg) return the correct column type for text for my ad-hoc query?

What's with this? my $sth = $dbh->prepare(q~ select 'hello'::text as my_text_column ~); $sth->execute; print $$sth{TYPE}[0]; # prints -1, expected 12 I can select 5 and it returns the correct type (4, for integer) or cast it like select 5::numeric(4, 2) and get back 3. Why doesn't it like text columns? ...