perl's Journal
[Most Recent Entries]
[Calendar View]
[Friends]
Below are the 20 most recent journal entries recorded in
perl's LiveJournal:
[ << Previous 20 ]
| Tuesday, April 28th, 2009 | 2:34 pm [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? | | Saturday, December 6th, 2008 | 6:13 pm [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? | | Friday, November 21st, 2008 | 10:45 pm [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 | | Wednesday, September 10th, 2008 | 5:17 pm [jimpossible84]
 |
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
| | Monday, August 25th, 2008 | 2:09 pm [occupant]
 |
Perl moderately broken on RHEL
Hi LJ Perlers, long time lurker, first time poster. This was a disturbing bit of news in my day. I had not noticed this due to usually compiling Perl manually, but I definitely have been suspecting something since I tried switching to the one RedHat ships. This guy just called it-- certain functions are really, really slow out of the box on RHEL-- and they just get slower the more you use them. http://blog.vipul.net/2008/08/24/redhat-perl-what-a-tragedy/He provides a test which should exit very quickly on a "good" perl, and will take many seconds on a broken one. # Perl compiled manually, runs in a snap: sandbox# time perl perftest ........................................ .......... real 0m0.108s user 0m0.100s sys 0m0.007s # Redhat's stock Perl, took almost 19 seconds: sandbox# time perl perftest ........................................ .......... real 0m18.868s user 0m18.726s sys 0m0.072s To tell if Perl on a system you use is affected, run the following code: #!/usr/bin/perl use overload q(<) => sub {}; my %h; for (my $i=0; $i<50000; $i++) { $h{$i} = bless [ ] => 'main'; print STDERR '.' if $i % 1000 == 0; } The only reliable "fix" at this point is to not use RedHat's Perl, and to compile it manually, including anything linked against it like mod_perl. Not fun. Hope this comes in handy and/or helps spread the rage I'm feeling right now. | | Monday, August 4th, 2008 | 9:55 pm [mart]
 |
@INC hooks and tied filehandles I'm currently working on an app where I've installed a CODE ref in @INC to hook the loading of certain modules. I can't seem to find any documentation on hooking @INC, so I'm just working from examples I've found. I've seen this work with normal filesystem handles:
use Symbol;
use lib sub {
my $sym = gensym();
open ($sym, '<', '/dev/null');
return $sym;
};
The above will fail with the error that the included module did not return a true value, since of course /dev/null doesn't end with 1;.
What I'd like to be able to do is return a string containing source code. To do this, I attempted to use IO::String to create a tied handle that operates on a string in memory:
use IO::String;
use lib sub {
warn $_[1];
return IO::String->new("");
};
In this case, Perl just ignores my result and moves on to the next entry in @INC. Is there any way to make this work, or will hooking @INC only work on real system filehandles? (If it makes any difference, I'm running these on Perl 5.8.8.)
Partial Solution: After some searching about I found the docs for this nestled at the end of the require documentation. It seems that the hook sub can return a second argument which is another coderef that's called repeatedly to fetch lines of the source file. Rather bizarrely, it must set $_ to be the result and return 1, or return 0 to signal that there are no more lines. Even more bizarrely, this only works if the hook sub returns some kind of globref as its first return value. I just made a sub that returns the string on the first run and indicates the end on the second run, like this:
my $sym = Symbol::gensym();
my $done = 0;
return $sym, sub {
if (! $done) {
$_ = "die 'Blah'";
$done++;
return 1;
}
else {
return 0;
}
};
Quite why you need that random glob I'm not sure; the require documentation implies that it's optional. Perhaps someone else can shed some light. | | Saturday, July 19th, 2008 | 2:55 pm [paintitmatt]
 |
Perl and Pidgin's API
I'm trying to write a plugin for the Pidgin IM client in Perl that would take your latest post to http://identi.ca from an RSS feed and set it as your Pidgin status message. However the Perl API for Pidgin is poorly documented and I'm having trouble. I based a lot of it on the pidgintwitter-plugin. I'm not sure what's going on exactly though I suspect I'm not passing the URL correctly to the the parsing subroutine. Any help would be appreciated. ( code behind the cut ) | | Tuesday, July 15th, 2008 | 4:22 pm [publius_ovidius]
 |
| | Friday, May 30th, 2008 | 7:18 pm [dicedpear]
 |
Text Game Questions
Hey, I'm creating a text game, basically a choose-your-own adventure, for my Intro to Unix class. I have some basic questions that I desperately need answered to even begin creating this game. I was told to try using Perl. What are your thoughts on this? ( game questions )Is this the right place to post these questions? | | Friday, April 25th, 2008 | 10:44 pm [petdance]
 |
| 10:36 pm [paintitmatt]
 |
question about arrays and references
I wrote a script that will post to twitter. It takes a command line argument or reads a random line from a text file, and posts the result. I've got it working, but I think code could be a little cleaner. Basically, I want to know if there is a way to put each element in an array ( like @ARGV) into a string. For instance, if I run perl post-to-twitter.pl here is a tweet, I'd like to turn "here is a tweet" into a string so I can put it in a scalar like $post. I think I could use a reference, but I have a hard time wrapping my head around the concept. I really want to turn the last 4 or 5 lines into one. Thanks... ( here's my code )Oh yeah, if the command line argument has an apostrophe, the script breaks... Why is that? Current Music: King Tubby & Lee Perry - Splash Out [Dub] | | Sunday, April 6th, 2008 | 3:09 pm [le_trombone]
 |
| | Tuesday, March 18th, 2008 | 2:57 pm [cartman94501]
 |
Am I going insane? This code snippet (line numbers are obviously not part of the code):
137:print "right before the weirdness, url=\|$url\|\n"; 138:if ($url =~ /(jpg|gif|png)/i) { [do something utterly irrelevant here] }
sometimes, but not always, causes the following output:
right before the weirdness, url=|http://bp.specificclick.net?pixid=99002077| Use of uninitialized value in pattern match (m//) at [scriptname].pl line 138.
We see that $url is non-null, and obviously (jpg|gif|png) is a constant, so how could either side of the pattern match be uninitialized? The script appears to work, but it keeps throwing this error. Oddly, this only started this week ago, and until it happened, I had made no recent changes, either to the code or to Perl.
I lack the necessary hubris to think that I've discovered a bug in Perl, rather than in my humble code, but the mind does boggle.
I am running Perl 5.8.8.822 under Windows XP, SP2. There are 16 registered patches, none of which I installed manually, which I can provide if you think it's relevant.
Thanks in advance for your help! | | Monday, March 17th, 2008 | 9:01 am [w74endy]
 |
really basic question about installing ActiveState
so, I had PERL installed on my old computer at school. It seemed simple at the time. Then I would just so to my perl directory, type in "myprogram.pl" or whatever and my programs would run. I got a new computer so I installed perl. I copied my program of interest. I went to my new perl directory, but when I type in myprogram.pl it just opens the file in text editor. What am I doing wrong? I don't think I'm too dumb normally, but this just won't work at all. Please help me so I can use all my old programs. | | Wednesday, March 12th, 2008 | 9:09 pm [rekoil]
 |
| 10:34 pm [al1us]
 |
CGI::Session
Hey, I can't understand how sessions work in perl. I have read the manual on cpan but still can't get sessions to work. When a user comes for the first time, he sees a login page. Enters his credentials in the form and the is redirected to the same login page where the script controls the username and password and if everything is ok, then the user is redirected to another page or in case of any problems the user sees the login form again. On the login page I first create a session, then check whether the user is logged in or not and decide what to do next:
my $cgi = CGI->new();
my $session = new CGI::Session(undef, $cgi, {Directory=>"/tmp"});
if ($logged) { &redirect(); }
else {
... routines that check username and password ...
... if the username and pass are correct, then write some params, send cookie and redirect ...
$session->expire("+15m");
$session->param("logged", 1);
my $cookie = $cgi->cookie(CGISESSID => $session->id);
print $cgi->header( -cookie => $cookie);
&redirect();
This code seems to work, because I am being redirected to the proper page after submitting the login and password. On every other page I check whether the user is logged in or not and if not, then redirect him to the login page
my $cgi = CGI->new();
my $session = new CGI::Session(undef, $cgi, {Directory=>"/tmp"});
if ($logged != 1) { &redirect(); }
else { ... }
But every time I am being redirected to the login page. I looked in the /tmp and found 4 session files, 1 has "logged" parameter set and 3 sessions without "logged" being set. The first session is created when the user comes for the first time. The second one, as I understood, is created when the user submits his credentials. During this time the "logged" parameter is set. The third is created when I am redirected to another page (but why doesn't it pick already created session?) and the fourth when I am redirected back to the login page. I also tried to create sessions like it is described in cpan:
my $sid = $cgi->cookie("CGISESSID") || undef;
my $session = new CGI::Session(undef, $sid, {Directory=>"/tmp"});
But has the same result. Tried to pass the session id with the URL. Didn't help. Any ideas? Thanks in advance UPD: fixed. Passed the session id via URL and it worked. Guess, there is something wrong with my browser not receiving cookies | | Tuesday, January 29th, 2008 | 1:19 pm [shamess_the_elf] |
i must be new here
I'm new to perl; the language doesn't look that different from what I've been doing with PHP and stuff (not drastically different, at least), so that's not my problem. But now I have to install packages and stuff, I'm a little confused. ( I shouldn't have this many packages, should I? )I'm not sure why I have all those packages (I think I installed random ones when I was playing with MovableType). But should have have all of those? Do I need both Apache-DBI and DBI, or those two DBD-SQLite (plus the random grey one) - not that I even use SQLite. My second thing is, when I do "use DBI;" this just gets returned: "Can't locate DBI/mysql.pm in @INC (@INC contains: C:/usr/site/lib C:/usr/lib .)" What should I be doing differently? Thanks :) | | Monday, January 14th, 2008 | 7:58 pm [rekoil]
 |
XML::Simple conundrum
I'm having issues with XML::Simple treating a tag with a single nested element inside differently than a tag with multiple elements... ( Read more... ) | | Wednesday, December 19th, 2007 | 6:06 pm [foxmagic]
 |
I never figured out CPAN. Every time I run it to install something, it merrily goes downloading/compiling/installing stuff I don't even know what is. Whenever I use CPAN, I feel like I've invited a guy who doesn't even know me to come in and redecorate my house. My problem right now is that I'm trying to install a program which depends on Net::DNS, and Net::DNS fails its tests and won't install on my Mac OS X 10.4 system. Here's what 'install NET::DNS' spits out: ( Read more... ) | | Monday, December 10th, 2007 | 1:49 pm [sexeger]
 |
Consistent hash ordering when no new keys are introduced?
I know hash order is random during key insertion, but is ordering guaranteed when no keys are inserted in a particular statement? Basically, I want to know if this will remain safe on future versions of Perl: @merged{keys(%hash)} = values(%hash); |
[ << Previous 20 ]
|