linux

Script help

I need help with updating multiple files in a directory. I have a block of text (multiple lines) in multiple files that needs to be replaced with another block of text. I have the two blocks of text (one for the match, and the other for the replace) in two separate files. I just am clueless as to how to do do this. I can do simple regex search and replace on a single line of a single file but don't know how to apply the technique to multiple lines over multiple files. I am just not that strong in Perl.

Any suggestions?
me

Pulling unique combos from a multiset

What's the fastest way to iterate over an ordered list and pull out only unique combos? The particular application here is unique seven-letter racks drawn from a fresh bag of Scrabble tiles. In other words, calling a blank @ for ease of ASCII ordering, my first draw is @@AAAAA, and there are 2C2 * 9C5 ways to draw it. The next one is @@AAAAB, and there are 2C2 * 9C4 * 2C1 ways to draw it. Then @@AAAAC, etc. all the way to @@AAAAZ, then down to three As like @@AAABB, @@AAABC, @@AAACC, etc., ending with VWWXYYZ (two ways). Given a hash such as:

(@ => 2,
A => 9,
B => 2,
C => 2,
D => 4,
E => 12,
...
X => 1,
Y => 2,
Z => 1)

what's the quickest way extract the unique combos? Once I have a unique combo, I can very quickly calculate the number of ways to pull it, but iterating over the list of unique combos is the hard part, at least for me.

Ideas? Thanks in advance.

Andrew
me

Time::HiRes no longer works

I recently upgraded to ActiveState Perl 5.12.2.203 on my Window XP SP3 box. It is my understanding that this version has Time::HiRes built in and thus there's no need to install it. If this is correct, why does this code snippet:

use Time::HiRes qw(time);
my $ts = time();
die "\|$ts\|\n";

only return the epoch time to the second, not the millisecond?

Per PPM, I am running Time::HiRes 1.9721 dated 2010-03-17

Any thoughts? Do I have to rebuild Perl from scratch? Thanks!
Martin
  • pne

Perl Survey 2010 is live

Here's the announcement I saw:

The Perl Survey 2010 is now live. Its purpose is to better understand the demographics and opinions of the Perl community. You can complete the survey at http://survey.perlfoundation.org - it should take about 10 to 15 minutes. Once you've done that, please let your relevant friends and colleagues know about the survey so they can coplete it as well. My aim is to get a response of over 1000 individuals, and to run the survey (lightly adapted) every two or three years so we can see how the community changes over time. The official announcement of the survey is here: http://news.perlfoundation.org/2010/05/grant-update-the-perl-survey-1.html

gchat-zalice
  • cos

simple html parsing

Is there a simple and straightforward way to parse HTML text (from a string) into a perl data tree, using only the modules provided with a standard perl distribution? HTML::Parser is the closest I've found, but it seems designed for entirely different purposes, and looks like it'd require some tricky gymnastics to make it simple take a string of HTML and give me back a data structure of all the contents. I've found HTML::Parser::Simple which seems to be exactly what I want, but it's a nonstandard module that I don't even see in the normal Linux repos, so having a script that depends on the presence of that module would be a major pain. (Sure, I can find and install the module on one host, but my context is to write something that could work on many hosts, that I don't necessarily admin myself)

Maybe a stupid HASH question but....

I've been self-learning perl from books and websites, and I am confused about the benefit of hashes....
An example often given is...

%HoH = (
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george"
wife => "jane",
"his boy" => "elroy", # Key quotes needed.
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);
So, why is this better than just having a bunch of variables like: 

$flintstones_husband = "fred";
$flintstones_pal = "barney";
$simpsons_husband = "homer";
$simpsons_wife = "marge";
and so on...?
  • darac

Being aware of subclasses

Hello,

I just joined the community and I wonder if I could jump straight in with a query.

I am writing a program that, given a directory with some files in it, would do the same functions to that directory, but in different ways based on the contents of the directory. That is, my directory may contain one of a number of different tests and I need to run those tests using different programs.

What I would like to be able to do is say "my $test = SomeClass->new($dir);" and have $test be set to a SomeClass::Foo, SomeClass::Bar etc. More importantly, I'd like the code to work if I, at a later time, create SomeClass::Baz.

I envision there being a detect() method with each class that says whether the given directory is suitable for this type, though clearly that could be done in new() (return undef if it's not suitable).

So, to cut a long story short, the point of my question is this: can I make SomeClass aware of subclasses before they even exist?
Chaos
  • xerhino

CPAN? Might as well call it CPAIN.

Ok, no. I love CPAN. But, for all the things it does effortlessly, I have never had SOAP::Lite install from CPAN. It’s somewhere in the dependancies (it’s not expat, I have that). I always end up having to download source for several of the dependent modules, which install fine, and doing the rest one level at a time till I’m at the SOAP::Lite install. Anyone know why that is? Why would a module install with no special instructions or configuration options from a make command, but not CPAN? The closest I can figure is maybe some dependency is not detected correctly even though it is compiling correctly. Or maybe there is some problem with the requirements trees? Nah. Maybe just the tests, though I’d think they would be the same for make installs as CPAN installs. Anyone have some greater insight about this? Does this happen to you too?
jolly roger
  • xerhino

Good to be bad.

Ok, this isn't something I would do in code that anyone would ever have to support or modify. This was purely a temporary monitoring script for an ill concieved application that, for dubious reasons, had 30 duplicate directories where logs could be written, depending on the day/time/traffic/phase of the moon/etc.

my @logs = grep {-e $_} map { "/opt/foo/bar/$_/logs/date.log" } 1..30;

I got a map, a grep, and a range operator all on one line and it returns an array of all the log files in the appropriate spots that actually exist. After so long making everything painfully clear this feels decadent. Really, I don't think it's that bad, but I guarentee no one else on my team would understand it. I just had to share with a group that would.

Note: I follow most of the tenets of Damian Conway's "Perl Best Practices" in my professional scripting.
  • Current Mood
    devious devious
bleaks_cannons

Including global variables

Greetings,

I have inherited hundreds of scripts which have some stuff hardcoded (which now need changing - lol). Anyway, I'd like to make a simple require file to put some globals in. Many of the scripts have package statements in them and most have "use strict" set. So I need a very general and reasonably fool-proof way to handle this. Here's what I came up with and wanted to bounce this off someone before actually implementing it:

include file:
==================================

use strict;

BEGIN {
	$main::__SAVEPACKAGE = __PACKAGE__;
}

package GLOBAL;

our $GLOBALVAR1 = "value1";
our $GLOBALVAR2 = "value2";
  ...

eval "package $main::__SAVEPACKAGE; ";

1;

==================================

Then using:


require 'my-globals.pl';
  ...
print $GLOBAL::GLOBALVAR1