perl

Is object oriented exception handling in Perl worth it?

I recently read "Object Oriented Exception Handling in Perl" Perl.com article. Is there any point to use exceptions in Perl? ...

How can I get the file and line number where a Perl subroutine reference was created?

Given a subroutine reference, is there a way to find out the file and line number where the subroutine was declared? warn and friends seems to get this right, but I need this externally. Here's my test program: #!/usr/bin/perl -l use strict; use warnings; use B; # line 99 'bin/some_code.pl' { no strict 'refs'; print B::svref_2...

Extracting a block of text where the closing expression depends on the opening one.

I have a text string structured like this: = Some Heading (1) Some text == Some Sub-Heading (2) Some more text === Some Sub-sub-heading (3) Some details here = Some other Heading (4) I want to extract the content of second heading, including any subsection. I do not know beforehand what is the depth of the second heading, so I n...

How do I run a Perl script in background on Windows?

I have a Perl script that pops up a message box when its work is done. How can I run this in the background? I looked at Proc::Background but this requires launching a specific command. I'd like my code to run in the background with out spawning a new process if possible. ...

What are the uses of lvalue subroutines in Perl?

I don't understand what could be the uses of lvalue subroutines? What is it that I can't accomplish with normal subroutines? Could you please post some examples? Thanks ...

How do I remove all hyphens with a Perl regex?

I thought this would have done it... $rowfetch = $DBS->{Row}->GetCharValue("meetdays"); $rowfetch = /[-]/gi; printline($rowfetch); But it seems that I'm missing a small yet critical piece of the regex syntax. $rowfetch is always something along the lines of: ------S -M-W--- --T-TF- etc... to represent the days of the week a meetin...

How do I place a colon into a string two characters from the end using Perl?

I'm trying to find a way to place a colon ( : ) into a string, two characters from the end of the string. Examples of $meetdays: 1200 => 12:00900 => 9:001340 =>13:40 Not sure if this should be a regular expression or just another function that I'm not aware of. ...

mod_perl 2 variable and process corruption

We've just ported a fairly large codebase from an ancient Perl 5.005.03 CGI environment to mod_perl 2, and now that it's undergoing a public beta there are a few, possibly related, issues we're encountering from time to time. These build up until we have to restart the server. All of our code compiles under use strict, but has been prev...

Should I manually set Perl's @ARGV so I can use <> to open, scan, and close files?

I have recently started learning Perl and one of my latest assignments involves searching a bunch of files for a particular string. The user provides the directory name as an argument and the program searches all the files in that directory for the pattern. Using readdir() I have managed to build an array with all the searchable file nam...

How can I separate Perl variables from text in interpolated strings?

Hello, print " $foo", "AAAAAAAA", $foo, "BBBBBBBB"; Let's say I want to use this code with a print <<EOF;: print <<EOF; $fooAAAAAAAA$fooBBBBBBBB"; EOF That won't work because Perl thinks I have a variable called $fooAAAAAAAA. How can I easily use print << with such lines when I have a long test to print? ...

How can I open pipes with OO style?

I rewrite my old code in new style, like below: #old style open(FD,"file"); #new style $fh = IO::File->new("file","r"); Files are ok, but I don't know how to open pipes. # read from pipes. open(PIPE,"some_program |"); # write to pipes. open(PIPE,"| some_program"); How to treat pipes in OO Style IO? adding: thanks Jonathan, it's ...

Do I need Exporter if I'm going for pure OO in Perl?

The docs (Exporter and perlmodlib) say: As a general rule, if the module is trying to be object oriented then export nothing. But then perlmodlib also says: Standard, bundled modules are all expected to behave in a well-defined manner with respect to namespace pollution because they use the Exporter module. So I w...

How can I parse command-line switches in Perl?

In order to extend my "grep" emulator in Perl I have added support for a -r switch which enables recursive searching in sub-directories. Now the command line invocation looks something like this: perl pgrep.pl -r <directory> <expression> Both -r and the directory arguments are optional (directory defaults to '.'). As of now I simply c...

How can I remove all characters from string starting at first non-alpha character?

This would have been a lot easier if not for certain situations. Sample data: KENP989SD KENP913E KENPX189R KENP913 What regular expression can I use to remove all characters from the string starting at the first non-alpha character? Basically, I want to find the first non-alpha character and chop everything off after that regardless ...

Is PHP or Perl the right choice for my web project?

I'm looking forward to hear some suggestions for choosing the "right" language (and modules?) to realize a 1-man web project (LAMP only, complexity somewhere between a guestbook and a fully fledged blog, developed for high traffic sites with 50,000+ impressions per day) based on these requirements: Output cache (think: Wordpress Super-...

Is "local our" the thing to use in object modules under mod_perl2, or only in scripts?

To tailor your scripts toward mp2, avoiding the need for any compatibility wrappers and such, it's said that you're supposed to declare variables using "local our" rather than "my". What about in modules? sub new { local our $type = shift; local our $self = {}; bless $self, $type; } Is that right? Or should it be 'my' so...

Why does Perl complain when I use a hash reference with constant.pm?

I have perl, v5.6.1 built for MSWin32-x86-multi-thread Binary build 638 provided by ActiveState. I am working on a Perl script where I have declared constants that are used later for comparison purposes. For some reason, I am getting an error that states something along the line of Constant name has invalid characters at script's line ...

How can I find a specific line in a file without using regular expressions in Perl?

I have a file that I want to read in using the File::Slurp module then search that file for a specific line. I know Perl has regular expressions but the strings I’m searching for are user-generated so don’t want to have to worry about escaping everything. I could write a foreach loop to do this but was wondering if there’s a function in ...

How do I separate a variable from the rest of my Perl regex?

I have a regex where the variable $m_strFirstName is next to other identifier characters that aren't part of the variable name: if($strWholeName =~ m/$m_strFirstName_(+)/) .... I'm trying to extract something like: strWholeName is 'bob_jones' or 'bob_smith' m_strFirstName is 'bob' and I want the 'smith' or 'jones' part of the strin...

How do I get a file's last modified time in Perl?

Suppose I have a filehandle $fh. I can check its existence with -e $fh or its file size with -s $fh or a slew of additional information about the file. How can I get its last modified time stamp? ...