Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
my $results = $dbh->selectall_arrayref('select * from players where name = ?', undef, $name);

As long as the query isn't too complex and does some simple filling in the blanks kind of stuff, that's the way you would use the database handle. Notice there are convenience methods off of the handle. You shouldn't really have to prepare/execute queries for super simple apps.

Adbot
ADBOT LOVES YOU

wigga please
Nov 1, 2006

by mons all madden
So basically the whole function could be replaced by just
code:
my $rawstats = $dbh->selectall_arrayref('SELECT SUM(HDs),SUM(street0VPI),SUM(street0Aggr),SUM(street1seen),
SUM(foldToOtherRaisedStreet1),SUM(street1Aggr),SUM(street2seen),
SUM(foldToOtherRaisedStreet2),SUM(street2Aggr),SUM(street3seen),
SUM(foldToOtherRaisedStreet3),SUM(street3Aggr),SUM(wonAtSd),SUM(sawShowdown)
FROM hudcache,players WHERE hudcache.playerId=players.id AND players.name=?',undef,$pName');
?

syphon
Jan 1, 2001
well you still need to connect (and disconnect) to the DB, but yeah.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Yeah, I guess. You might have a stray single quote at the end of that function. I suggest looking at the documentation for DBI to see what the various fetch convenience methods are, as well as controlling how you want the data returned back to you (hashes or arrays for each row).

Also, that's a pretty nasty looking query. Consider refactoring out the SUM for fun, or just storing it in a phrasebook somewhere. Or abstracting the select statement with a view.

Fenderbender
Oct 10, 2003

You have the right to remain silent.

Triple Tech posted:

Well, he would have to have a shallow execute, like perl -MTeX::Hyphen -e 1.

You don't need to execute anything, just using the -M flag will compile the module and will drop an error if it doesn't compile properly or isn't found. It will however sit around indefinitely if it successfully compiles, so yeah.

wigga please
Nov 1, 2006

by mons all madden
Thanks for the fast replies, you guys are great.

Just two more things about DBI, I only have
code:
use DBI;
at the start of my .pl file, but in no way is the port number or server specified anywhere. Do I need to do it in the function using DBI or is it implemented somewhere else? Also do I need to refer to the MS ODBC driver somewhere, because I do in C#?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Crazy how you'd ask considering the block of code you posted earlier shows that a database handle comes from the connect method off DBI. The driver is implicit to the connection string you provide.

wigga please
Nov 1, 2006

by mons all madden

Triple Tech posted:

Crazy how you'd ask considering the block of code you posted earlier shows that a database handle comes from the connect method off DBI. The driver is implicit to the connection string you provide.

I'm very new to both Perl and DBI, do you mean the DBI connect method can take more parameters to detail the location of the SQL server? When I'm using MS' ODBC driver in C# the connection string looks more like "driver="MySQL ODBC driver";port=3306;server=localhost; database=db;user=user;password=password".

In the code I posted those first three are missing, can I insert them elsewhere OR do I need to do something involving DBD::MYSQL somewhere?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
It's in the documentation for DBI, right under the heading connect. It's pretty much in the same.

dizzywhip
Dec 23, 2005

One more question - I've got the GD module installed so I can do some work with image files. It works fine except for jpeg support, which I've been trying to get working all day with no luck, it's driving me crazy. It seems gdlib won't install with jpeg support.

I've installed jpeg6b properly, and when I run configure for gdlib it seems to recognize this:

quote:

** Configuration summary for gd 2.0.34:

Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
Support for Fontconfig library: yes
Support for Xpm library: yes
Support for pthreads: yes

Before I installed jpeg6b it had said no for jpeg support, so it seems to have found the new libraries.

Then I do make and make install, which seem to work fine, but gdlib-config still tells me it has no jpeg support, and I get no jpeg support in perl after reinstalling GD. Any ideas?

Gary Mitchell
Apr 3, 2007

Morals are for men, not gods.
Wow, I am having a bitch of a time returning the absolute path of files in my web-root. Let's say I want to upload/read a file on my server at /home/clinton/webroot/proj/cont. This would correspond to the web-directory http://localhost/proj/cont.

Everything I try, Perl just keeps returning the scripts current directory as '.' which is completely useless when it's not relative to anything. Trying to turn this into an absolute path just returns /, my root directory, instead of /home/clinton/webroot/proj.

When I try to upload files to /home/clinton/webroot/proj/cont/$filename it works great, but uploading files to cont/$filename vanishes the file into oblivion.*

I have tried everything in File::Spec and CWD, and it all doesn't work. I've googled around for absolute file paths perl and those seem to be the only recommendations I can find.

How do I make this code portable, so that I don't need to hardcode the directory Apache is hosting the files from? I'm pulling my hair out.

*edit: Actually, I just created a folder called /cont (in the root directory of my whole OS), and files uploads there when I don't specify an absolute path.

And here is how I've set up apache, from my httpd.conf:
code:
Alias /proj /home/clinton/proj
PerlModule ModPerl::Registry
<Location /proj>
   SetHandler perl-script
   PerlHandler ModPerl::Registry
   PerlHandler ModPerl::PerlRun
   Options +ExecCGI
   PerlSendHeader On
</Location>

Gary Mitchell fucked around with this message at 00:47 on Mar 29, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Gary Mitchell posted:

I have tried everything in File::Spec and CWD, and it all doesn't work. I've googled around for absolute file paths perl and those seem to be the only recommendations I can find.

That's really strange. What does this give you?

code:
perl -MCwd -le 'print Cwd::abs_path(".")'
My only guess is that your server chdirs to / before invoking scripts for some reason.

Gary Mitchell
Apr 3, 2007

Morals are for men, not gods.
From the console? It just returns the directory I run it from:
code:
clinton@clinton-desktop:~$ perl -MCwd -le 'print Cwd::abs_path(".")'
/home/clinton
clinton@clinton-desktop:~$ cd Documents
clinton@clinton-desktop:~/Documents$ perl -MCwd -le 'print Cwd::abs_path(".")'
/home/clinton/Documents
clinton@clinton-desktop:~/Documents$ cd Resources/
clinton@clinton-desktop:~/Documents/Resources$ perl -MCwd -le 'print Cwd::abs_path(".")'
/home/clinton/Documents/Resources
clinton@clinton-desktop:~/Documents/Resources$
Thanks for the fast response, though!

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
What about print Cwd::abs_path('.') from your script?

Gary Mitchell
Apr 3, 2007

Morals are for men, not gods.

Sartak posted:

What about print Cwd::abs_path('.') from your script?
/

:(

This is honestly a fresh, stock install of Perl and Apache on Ubuntu 8.10. And I can't find anyone else with my problem anywhere, although it's pretty hard to specify "." or "/" as a useful search term on Google.

I might end up just assuming that relative paths isn't broken on other machines, get it working, change all the paths, and submit it untested. But that would suck balls.

Gary Mitchell fucked around with this message at 01:27 on Mar 29, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Gary Mitchell posted:

From the console? It just returns the directory I run it from:

What if you do this on your server? And like I said, it's probably using chdir. To see if that's the issue, use chdir yourself:

code:
chdir '/home/clinton/webroot/cont';
print Cwd::abs_path('.');

Gary Mitchell
Apr 3, 2007

Morals are for men, not gods.
Oh holy poo poo. I moved onto another problem I was having with my .htaccess files and, after fixing that, this problem solved itself! And for clarification the console is on my server, I'm serving the file to myself from localhost. But now it's cleared up!

Sartak, thank you so much for your time! :hfive:

edit: Uh, actually, no it didn't. Hold on, I'm figuring out if it was the CHDIR that temporarily fixed my prob

edit2: Screw it. I've uploaded the files to my website, and have opened them through FTP in my text editor (yey KDE KIOSlaves!). Identical workflow, but now everything works the way it should. I'll see if the problem persists after I upgrade to Jaunty in April. I do thank you for your help, though!

Gary Mitchell fucked around with this message at 03:05 on Mar 29, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
I submitted a talk to YAPC::NA in Pittsburgh. It's about extending Moose for applications. Metaprogramming is so good. I won't know until the end of April whether it's accepted, but considering Moose's popularity I'm sure I'll be giving this talk.

Is anyone else planning to attend or better yet give a talk?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
It looks like my vacation plans for this year fell through, so the week of YAPC is actually free. I wonder if I should go now...

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Are any of you guys Catalyst whores? I think my company is set on using it but god it feels so ghetto... And I have a question about implementing multiple views.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Don't ask to ask man. Just ask. If someone knows it they'll answer.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
That was bad form, I agree.

In Catalyst, how exactly do multiple Views work? My impression was that the controller should be agnostic to what view the application is currently running under, and that the templates are dependencies of the View. Such that if I change the view to something else with one line of code, I can change all the templating behavior implicitly. Is this not the case with Catalyst?

I find the documentation to be thin and full of errors; it's quit irksome. And sort of embarassing since I blindly extolled its virtues without looking under the hood.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
As of 5.7007, calling $c->view() will honour the default_view setting in your config before checking $c->stash->{current_view} and then walking through the list of loaded View classes:

http://search.cpan.org/~mramberg/Catalyst-Runtime-5.71001/lib/Catalyst.pm#$c-%3Eview($name)

So yes, that's the case. You may set up content-type/accept sniffing in your end method and jump out to the appropriate view, e.g. TT, JSON or Email, for example.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Also, if you're using Catalyst::Action::RenderView, which gets enabled by default with the end controller in Root, it will only try to call a view's process() method if no HTTP body has been defined, so you can jump out to an arbitrary view from a given controller method, if you have to.

Flamadiddle
May 9, 2004

I know this is a longshot and I don't even know if Perl is necessarily the right thing to be using for this, but I would like to query a remote server to check that a certain service is running. Any advice on whether I can knock up a .bat file or a perl script to do this automatically?

Edit: Nevermind, think I've got there with Win32::Services.

Flamadiddle fucked around with this message at 12:26 on Apr 6, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

A quick GD::Graph question for you all:

I'm plotting some graphs for a project that I have to present tomorrow, and I'm having trouble with line graphs where the intervals of data aren't evenly spaced; it plots the range [0,0.5] in the same amount of space as [0.5,0.6]. Is there a way to make the plotting spaced out as it should be? I'm sure it's obvious but I can't see it in the man page.


Got it, turns out that x_tick_number has to be defined.

Dijkstracula fucked around with this message at 21:58 on Apr 6, 2009

heeen
May 14, 2005

CAT NEVER STOPS
Riddle me this: why does php outperform perl in this nsieve test twelve-fold:
http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=perl&id=2
http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=php&box=1
I converted the perl version from lists to strings like the php version, and it made it two seconds slower, so that can't be it.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
That implementation is gay. Looking at the C implementation, I'm going to assume these aren't out to prove how a certain data structure performs (array access), but to just solve the problem. You'd use a bit vector with vec(). (See Computing primes)

Edit: Ugh, who wrote that Perl submission. It's so different from the PHP one.

Triple Tech fucked around with this message at 23:06 on Apr 7, 2009

heeen
May 14, 2005

CAT NEVER STOPS

Triple Tech posted:

That implementation is gay. Looking at the C implementation, I'm going to assume these aren't out to prove how a certain data structure performs (array access), but to just solve the problem. You'd use a bit vector with vec(). (See Computing primes)

Edit: Ugh, who wrote that Perl submission. It's so different from the PHP one.

There's a seperate version with bitsets for each language.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Catalyst 5.8000_07 hit CPAN today. Apparently a production-ready release will be made this week if no more blockers show up during testing. Time to start porting our existing apps over to Catamoose!

leedo
Nov 28, 2000

atomicstack posted:

Catalyst 5.8000_07 hit CPAN today. Apparently a production-ready release will be made this week if no more blockers show up during testing. Time to start porting our existing apps over to Catamoose!

I think this will be the impetus to get me to finally start using Moose. Cool!

dagard
Mar 31, 2005
I've been using Catalyst lately for an internal project (inventory tracking for my group of system administrators), and while I love it, I seem to be google-blind on one thing. Query caching.

Example: You're looking at an audit page, where you see the last 30 changes. Each row of the audit log is generating another hit against the 'users' table to pull out username, display name, email address, etc.

Ideally, I'd like something that, either in Catalyst or TT2, I can go 'ok, for this page, this render, cache everything'

Would I have to roll my own? (doable), or is there something I'm just too blind to see?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Caching aside, have you set up your indexing properly? I don't know that much about DBs and what have you but the least you could do is make naive calls to your database as smooth as possible and then investigate caching afterwards.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?

dagard posted:

Ideally, I'd like something that, either in Catalyst or TT2, I can go 'ok, for this page, this render, cache everything'
Catalyst::Plugin::PageCache . Supports Memcache etc.

leedo
Nov 28, 2000

dagard posted:

I've been using Catalyst lately for an internal project (inventory tracking for my group of system administrators), and while I love it, I seem to be google-blind on one thing. Query caching.

Example: You're looking at an audit page, where you see the last 30 changes. Each row of the audit log is generating another hit against the 'users' table to pull out username, display name, email address, etc.

Ideally, I'd like something that, either in Catalyst or TT2, I can go 'ok, for this page, this render, cache everything'

Would I have to roll my own? (doable), or is there something I'm just too blind to see?

I haven't done much with it, but you could use Catalyst::Plugin::Cache. An example could look something like this (taken straight from the docs):
code:
unless ($user_data = $c->cache->get($user_id) ) {
  $user_data = $c->model('MyApp::User')->find($user_id);
   $c->cache->set( $user_id, $user_data );
}
And then clear the cache at the end of the request.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
I'm writing an IRC bot that manages a card game. I was annoyed with how much work it would be to have the bot reset its state once the game was over, fresh for a new game. I put off this semi-necessary feature for a while, until tonight. Since I wrote the IRC bot with Moose, so resetting state turned out to be as easy as:

code:
for my $attribute ($self->meta->get_all_attributes) {
    my $clearer_method = $attribute->clearer
        or next;
    $self->$clearer_method;
}
Every attribute that declares a clearer (a method to clear that attribute's value) will have that clearer called when the bot re-initializes. Since everything tends to have a lazy default, that means the decks of cards will automatically be reshuffled, and the player hash will be reinitialized to an empty hashref, and so on.

Attributes that should persist across games (such as the connection to the IRC server) do not get reinitialized because they declare no clearer.

:science:

Filburt Shellbach fucked around with this message at 08:14 on Apr 18, 2009

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
KiokuDB is pretty rad. Set up a test cluster of memcachedb machines and am writing a backend using libmemcached to see how much better/worse it is than CouchDB, which seemed to support about 1000 transactions/sec with replication enabled. Using MooseX::Declare makes it really weird to write Perl-but-not-Perl.

leedo
Nov 28, 2000

I have been super bored this weekend and started working on an Gtk+ irc client in perl. My main goal is to be compatible with Colloquy styles, which consist of html, css, and an xslt file. I am using WebKit for the HTML view, which is also what Colloquy uses. It is pretty neat, because there is a great Colloquy style that can do inline images, as well as inline audio (via the <audio> element.)

Here is a large screenshot

I've got everything working, but now I am going back and re-writing it with Moose. It is a bit of a pain, but I can already see it becoming more readable and easier to update.

dizzywhip
Dec 23, 2005

What's the most efficient way to do multiple substitutions at once? There's lots of information online about doing multiple matches at once, but I can't find anything about substitutions.

For example, is it possible to do something like this:

code:
$text =~ s/string1/string2/g;
$text =~ s/string3/string4/g;
But do both substitutions at the same time, so that perl is not scanning the same text twice in a row?

Adbot
ADBOT LOVES YOU

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

mit_senf posted:

What's the most efficient way...

Does it really matter?

Try benchmarking a hash lookup with the /e switch. s/foo|bar/lookup($1)/ge (untested)

  • Locked thread