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
Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
So it doesn't seem to be widely documented and I suspect it's a bit of a dirty hack, but I've discovered a fairly easy to enable cross-schema joins with DBIx::Class and Catalyst - change your schema class's __PACKAGE__->table definition to use the fully qualified name:

code:
# DB::local::Schema::local_table
__PACKAGE__->table('local.local_table');

# DB::foreign::Schema::foreign_table
__PACKAGE__->table('foreign.foreign_table');
Append some lines to your DB/Schema.pm files to make another call to load_classes, specifying the foreign schemas you want to load:
code:
# DB::local::Schema
__PACKAGE__->load_classes('DB::foreign::Schema' => [qw/ foreign_table /]);
And add a relationship:
code:
# DB::local::Schema::local_table

__PACKAGE__->belongs_to(qw/
  foreign DB::foreign::Schema::foreign_table foreign_id
/);
This is enough to get it working under MySQL/Postgres (which we use in production), but SQLite, which we use on our local dev machines, is a little tricker. Since it operates on a single stand-alone file per DB, with no namespacing to speak of, you must resort to this dirty poo poo, using the ATTACH DATABASE command, which allows you to use the fully qualified schema.table syntax:
code:
# myapp_local.yml
Model::DB::local:
  connect_info:
    dsn:           'DBI:SQLite:__HOME__/db/local.db'
    on_connect_do: "ATTACH DATABASE '__HOME__/db/foreign.db' AS foreign"
Essentially you're attaching each SQLite database to every other database, which is all kinds of hosed up, but for us it's only in dev so it's no big deal.

Adbot
ADBOT LOVES YOU

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
How's this?
code:
$ (qw/zero one two three/)[-2 .. -1]     
                                                           
$ARRAY1 = [
  'two',
  'three'
];

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Thought that might be the case. In that case, try assigning to undef, it looks weird but should work in recent versions of Perl (a quick Googling didn't tell me which version made it syntactically valid):
code:
my (undef, undef, @foo) = get_foo();
Edit: Looks like the second and third editions of the Camel book both have this listed so it should work all the way back to 5.6 or so.

Mario Incandenza fucked around with this message at 19:15 on Mar 8, 2010

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Not really; update() doesn't take any arguments other than column values, so it's not really possible to specify a join condition as part of your update. Plus not all databases support joined updates (SQLite doesn't, anyway). However, there's nothing from stopping you from getting direct access to the underlying $dbh inside your resultset class and writing some SQL that does what you need.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
5.12.0 is out :toot:

Lots of changes and new stuff, perl5120delta here

Mario Incandenza fucked around with this message at 08:07 on Apr 13, 2010

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Anybody had much experience with the various distributed worker libraries like Gearman, TheSchwartz, and Cantella? Any advice to offer, or other libraries that would be worth trying out while I'm at it?

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
CGI::App's great for simple web apps with only a few different pages. Once you pass ~5 screens, or you need to glue together a bunch of different models, start looking at Catalyst. Stick to the same template system and ORM across the board if you can help it, Template::Toolkit and DBIC are usually good enough for most people's needs. Both CGI::App and Catalyst support FastCGI easily - Catalyst does it out-of-the-box, CGI::App has a helper module on CPAN.

Mario Incandenza
Aug 24, 2000

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

Marzzle posted:

Any other criticism is appreciated. I literally learned to program this stuff a week ago so I am sure there's other points I am off on.
From a quick once-over:
  • Too many global variables (KISS)
  • Don't use shift() on @ARGV or @_ unless you know you need to
  • You can move those two inner foreachs into a single sub (DRY)
  • This may be a matter of personal preference but I shudder when seeing more than one/two levels of nested blocks (e.g. a foreach inside an if inside another foreach)
  • Don't need to call ls using backticks, just use something like File::Glob, which is a core module

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
which just iterates through bash's $PATH variable so why not cut out the middle-man and do it yourself?
code:
my ($path) = grep { -f "$_/gzcat" } split /:/, $ENV{PATH}
Also, IO::Uncompress::Gunzip is a core module, so you don't really need to use gzcat either:
code:
use IO::Uncompress::Gunzip qw/gunzip/;

# input and output can be filenames, file handles, scalar refs, array refs, or a glob
gunzip $input, $output;

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Well, calling functions indirectly and tightly coupled like that is kinda asking for trouble, but here's how to do it (even when using strict):

code:
sub foo { 'foo' }

my $method_name = 'foo';
my $ref = \&{ $method_name };

warn $ref->();
But you should really be using a dispatch table, or consider swapping to an OO-based system with proper reflection, instead of resorting to nasty hacks or manually defined mappings.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Just read about 5.13.7's keys(), push(), pop() etc now accepting refs as valid containers, noice. That was one of the few things that still seems to bug me about Perl's syntax.

e.g. say() foreach keys $obj->returns_hashref()

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
just use Date::Manip, it parses everything :q:

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Native traits are nearly as cool as Moose itself, inlining them was a great idea.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
code:
	for ($i = ++$i ){
I had to actually run this to find out what Perl does with it, it turns out it merely executes that loop once (consuming a line from input) and then exits. But doesn't the while() loop already consume lines?

:wtc:

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Any OpenSSL gurus around? I'm trying to get a client-side x509 cert from Thawte working but I'm getting 'sslv3 alert certificate unknown' bubbling up through Net::SSLeay and IO::Socket::SSL. Apparently they added an intermediary certificate to their chain last year and now it seems Perl isn't properly walking up the chain of trust.

Mario Incandenza
Aug 24, 2000

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

Mario Incandenza posted:

Any OpenSSL gurus around? I'm trying to get a client-side x509 cert from Thawte working but I'm getting 'sslv3 alert certificate unknown' bubbling up through Net::SSLeay and IO::Socket::SSL. Apparently they added an intermediary certificate to their chain last year and now it seems Perl isn't properly walking up the chain of trust.
RESOLVED: socat TCP4-LISTEN:7000,reuseaddr,fork OPENSSL:example.com:700,verify=0,cafile=$ca_cert,key=$key_file,certificate=$cert_file

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
They're both right. Technically if the arguments to system() ended up containing metacharacters for whatever reason (malice, ignorance, bad luck) then it could do something dangerous, but if the scripts are not publicly accessible and the box is sufficiently locked down, then it's less of a concern.

Mario Incandenza
Aug 24, 2000

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

qntm posted:

code:
processArray( \(sort keys %hash) ); # wrong, calls processArray(\"key1", \"key2")
This is syntactic sugar to make building a list of refs easier. From perldoc perlref:
code:
As a special case, "\(@foo)" returns a list of references to the contents of
@foo, not a reference to @foo itself.  Likewise for %foo, except that the key
references are to copies (since the keys are just strings rather than full-
fledged scalars).

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Not really related but tchrist's recent UTF8 post is pretty fantastic:

http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Devel::REPL has a bunch of tab completion plugins, I can't imagine it would be difficult to hack something up for that.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
code:
for ( my $i = 0; $i < @array; $i += 50 ) {
  my @chunk = splice @array, $i, 50;
  do_stuff(\@chunk);
}

Mario Incandenza fucked around with this message at 19:25 on Jul 6, 2011

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
The most recent sub imported with a use statement will be the one that's called. No errors or warnings will be emitted by Exporter:

code:
  *{"$callpkg\::$_"} = \&{"$pkg\::$_"} foreach @_; 
After a bunch of other stuff, it just straight up clones of a list of named subs from their typeglob in one package to the caller.

Perl does have a 'subroutine undefined' warning but it only shows up when the parser (compile-time or run-time) encounters a named sub block that matches an existing one in the current package:

code:
perl> sub foo { 'foo' }
perl> sub foo { 'bar' }
Subroutine foo redefined at (eval 68) line 1.

Mario Incandenza
Aug 24, 2000

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

wntd posted:

BEGIN { $^I = ".bak" }
Neat!

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
We deal with the shortage by churning through as many voice and face-to-face interviews as possible. Even then we can't hire quickly enough, it's hard to find good Perl dudes :(

BTW we're literally always hiring, so if you're a good Perl dude and are willing to relocate, send me a PM.

Mario Incandenza
Aug 24, 2000

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

prefect posted:

Relocate to where?
Amsterdam.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
So, who's going to be at YAPC::EU next week?

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Shoulda just used IO::Pty to run an instance of lynx and feed it some keystrokes, problem solved!

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
5.18.0 is out, some cool new things there, might want to hold back on upgrading in production though as some CPAN modules are broken under the new version ATM due to the hash fixes made by Yves (which has caused some butthurt on p5p, sigh).

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Most Perl hackers use hashes for pretty much everything, they're way more flexible than arrays. I only use arrays when I specifically need them (i.e. a numerically indexed, ordered list).

If you're writing Perl, learn to love hashes.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
The main thing to keep in mind when reading Perl Best Practices (and to be fair it's mentioned in the preface) is that the book's contents are not to be taken as the gospel truth. As welcome to hell said, about half of what it recommends you should avoid outright (inside-out objects, anyone?).

Also, Higher-Order Perl is probably one of the better Perl books ever written, and is available for free from the author, so check that out too.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Current Work Status: Discovered one of our most important bits of code is a Frankensteinian mish-mash of Moose and Moo, because a contributor tried to s/Moose/Moo/ but got it wrong and left half of the relevant roles untouched :downs:

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Yeah, Moo will load Moose roles just fine (though using both libraries simultaneously is pretty weirdo).

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
BTW, use cpanminus if you're not already, it makes things much simpler (and easier). It supports lots of super-cool features like installing from an arbitary local path/URL/git repo.

Adbot
ADBOT LOVES YOU

Mario Incandenza
Aug 24, 2000

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

uG posted:

code:

    my $file = (-f $_[0] && $_[0] ~~ @exts) ? $_[0] : return;

This is how i'd do it
but please don't use smart-matching in production code, the feature is deprecated and also a bit too smart for its own good

  • Locked thread