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
Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Just got mail about the dorms. Bring a pillow! Last year I ended up using a couple of shirts in a pillowcase. :doh:

Adbot
ADBOT LOVES YOU

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Using Moose... Let's say I'm constructing an object, but only some values are sane. Like... You can specify the age of a Person you create, but the age has to be an int. If it's not an int, set a default age. How do I accomplish this with Moose? I don't want a type error that age isn't an int. I want the constructor to encapsulate the scrubbing of dirty constructor parameters.

Otherwise I'm thinking just pass the parameters on to some custom facade method, assert that they are clean, and then internally call new().

Edit: Hrm, I guess I could just do this in BUILDARGS...

Triple Tech fucked around with this message at 16:11 on Jun 15, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
I'd use a type coercion. You're kind of abusing BUILDARGS by using it to transform attributes.

The benefit of using a type coercion is that it's introspectable and works with accessors as well.

code:
package Person;
use Moose;
use Moose::Util::TypeConstraints;

subtype 'Person::age'
     => as 'Int';

coerce 'Person::age'
    => from 'Any'
    => via { /^(\d+)$/ ? $1 : 18 };

has age => (
    is     => 'rw',
    isa    => 'Person::age',
    coerce => 1,
);

use Test::More tests => 6;

is(Person->new(age => 10)->age, 10);
is(Person->new(age => "ten")->age, 18);
is(Person->new(age => [1 .. 5])->age, 18);

is(Person->new->age(10), 10);
is(Person->new->age("ten"), 18);
is(Person->new->age([1 .. 5]), 18);
The reason I used a new "Person::age" subtype is that to avoid polluting the Int/Any types with a coercion. Types and coercions are global, so it's important to have this kind of namespacing.

MooseX::Types makes package-scoped types the default (I have my personal reasons for avoiding it, but that shouldn't discourage you, most of the in-crowd are using it now)

leedo
Nov 28, 2000

Sartak posted:

Miyagawa has written a quick blog post about building native OS X applications out of Perl programs and their dependencies. It uses Platypus to do the heavy lifting.

http://remediecode.org/2009/06/binary-builds-for-os-x-leopard-users.html

I'm pretty excited by this, and hope it eventually becomes as easy to package up apps for Windows and Linux.

Wow, that was really awesome. I just built an .app in 5 minutes for my latest waste of time:

http://github.com/leedo/buttesfire-web/tree/master

It launches a POE::IRC client and lets you interact with it using Safari or FF. It uses long polling to stream the data to the browsers. The great part is that most of the client development is done on the javascript side, so I can just refresh the browser to test out new things, no need to reconnect. (Yes I recreated CGI::IRC, ok)

leedo fucked around with this message at 06:59 on Jun 16, 2009

Captain Frigate
Apr 30, 2007

you cant have it, you dont have nuff teef to chew it
Really quick question here. I am trying to write a quick script to fix some video file headers and I wanted to make sure my code does what I think it does. (I've never used perl before, they just told me to use it.) I can't actually do the testing myself, because I don't have the software to play these videos myself, so I just had to kind of wing it. It's supposed to move to the directory, glob all the *.dat files, and replace an int a certain number of bytes in with a new value. If someone could give this a quick look I'd be grateful.

code:
#!perl
$directory = $argv[0];
$n = $argv[1];
$value = $argv[2];
chdir "$directory" or die "cannot chdir to $directory: $!"; #move to the desired directory
my @video_files = glob "*.dat"; #gather all the video files

foreach $filename (@video_files){
	unless(open(FILE, "+< $filename>")) {
		die "Cannot create filehandle: $!";}
	seek(FILE,4*$n,0); #skip to the nth int
	my $buf;
	my $number_read = read(FILE, $buf, 4); #read in the int
	my $number = unpack "i", $buf; #extract the value
	print FILE pack("i", $value); #replace the old value
	close FILE
}

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
The call to read() advances the position of the filehandle, so you'll need to call seek() again before print(). And probably also remove the > after $filename.

Captain Frigate
Apr 30, 2007

you cant have it, you dont have nuff teef to chew it

atomicstack posted:

The call to read() advances the position of the filehandle, so you'll need to call seek() again before print(). And probably also remove the > after $filename.

If I don't care what was there before, I wouldn't need to read at all, would I?

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Not really, no.

huge sesh
Jun 9, 2008

Help, I'm retarded.




I can't figure out how to filter out unwanted elements using XML::Simple. I was hoping to eliminate the <Placemark>s that aren't <name>'d "route":
code:
$ref->{kml}[0]->{Document}[0]->{Placemark} = grep {$_->{name}[0] eq "Route"} $ref->{kml}[0]->{Document}[0]->{Placemark};

perl xml.pl
Pseudo-hashes are deprecated at xml.pl line 10.
Use of uninitialized value in string eq at xml.pl line 10.
Here's the relevant kml and what Data::Dumper thought about the data structure:
http://codepad.org/8wLUbQsv

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Is anyone going to the Moose class on Sunday? Looks like I'll be teaching most of it!

Rapportus
Oct 31, 2004
The 4th Blue Man

huge sesh posted:

Help, I'm retarded.




I can't figure out how to filter out unwanted elements using XML::Simple. I was hoping to eliminate the <Placemark>s that aren't <name>'d "route":
code:
$ref->{kml}[0]->{Document}[0]->{Placemark} = grep {$_->{name}[0] eq "Route"} $ref->{kml}[0]->{Document}[0]->{Placemark};

perl xml.pl
Pseudo-hashes are deprecated at xml.pl line 10.
Use of uninitialized value in string eq at xml.pl line 10.
Here's the relevant kml and what Data::Dumper thought about the data structure:
http://codepad.org/8wLUbQsv

There's probably a more elegant way to write this but this should work. Had to make the arg to grep an array and build the result into a temp array before overwriting the original:

code:
my @temp = ();
push (@temp, grep {$_->{name}[0] eq "Route"} @{$ref->{kml}[0]->{Document}[0]->{Placemark}});
@{$ref->{kml}[0]->{Document}[0]->{Placemark}} = @temp;

S133460
Mar 17, 2008
hello

huge sesh posted:

I can't figure out how to filter out unwanted elements using XML::Simple. I was hoping to eliminate the <Placemark>s that aren't <name>'d "route":
code:
$ref->{kml}[0]->{Document}[0]->{Placemark} = grep {$_->{name}[0] eq "Route"} $ref->{kml}[0]->{Document}[0]->{Placemark};

perl xml.pl
Pseudo-hashes are deprecated at xml.pl line 10.
Use of uninitialized value in string eq at xml.pl line 10.
Here's the relevant kml and what Data::Dumper thought about the data structure:
http://codepad.org/8wLUbQsv

The warning about Pseudo-hashes is a clue. The "Placemark" node is an array reference, but grep expects a list. So you are giving grep a list with one scalar (the "Placemark" array reference) and trying to access that as a hash. Perl thinks you are trying to treat the array ref as a pseudo-hash.

Here's one way to do what you want...

code:
do { $_ = [ grep { $_->{name}[0] eq 'Route'} @$_ ] } for $xml->{kml}[0]->{Document}[0]->{Placemark};

S133460 fucked around with this message at 16:56 on Jun 18, 2009

huge sesh
Jun 9, 2008

satest3 posted:

The warning about Pseudo-hashes is a clue. The "Placemark" node is an array reference, but grep expects a list. So you are giving grep a list with one scalar (the "Placemark" array reference) and trying to access that as a hash. Perl thinks you are trying to treat the array ref as a pseudo-hash.

Here's one way to do what you want...

code:
do { $_ = [ grep { $_->{name}[0] eq 'Route'} @$_ ] } for $xml->{kml}[0]->{Document}[0]->{Placemark};

Thanks, this solved it. I need to do my homework and read about references more :kiddo:

CanSpice
Jan 12, 2002

GO CANUCKS GO

Sartak posted:

Is anyone going to the Moose class on Sunday? Looks like I'll be teaching most of it!

Hadn't planned on it. I'm probably going to the arrival dinner though.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I'm at YAPC now, in the Resnik house, wondering what to do until that arrival dinner...

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
The Moose talk went well! I'm in my Welch dorm room now relaxing.

I'll be wearing a red shirt tomorrow if anyone wants to stalk me before my Tuesday morning talk. Which I have to finish writing... . .

S133460
Mar 17, 2008
hello
I'm sure many of us who could not attend would like to read your talks should they be made available. *wink wink*

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
There are some suprisingly cute/young guys here... But sometimes it smells. It's absolutely disgusting. It's like goon central.

Also, I went to this packed talk about git... And this guy has his bag on an otherwise free chair. I'm like, excuse me, is anyone sitting here? And he makes his face like GAWD and begrudgingly removes his bag. Uhh, what? You gross goon, move your loving bag.

Other than those things... I guess it's pretty neat to hang out with like minded folk. I just wish they'd get haircuts and shower.

leedo
Nov 28, 2000

I came in in for the end of the most recent Git talk. it seems like things are really running behind schedule, unless I'm reading things wrong. They cut off the Perl 6 regex talk before he finished, but the Git talk went like 15 minutes over.

Anyways, I'm sticking around for the next Git talk. I'm bearded and in a plain white t-shirt and jeans.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

leedo posted:

I'm bearded and in a plain white t-shirt and jeans.

That describes no one. Are you on IRC or something?

leedo
Nov 28, 2000

Triple Tech posted:

That describes no one. Are you on IRC or something?

I am! I just joined #yapc as "lee_" which appears looks pretty full.

edit: i think i'm going to head over to the mod_perl6 talk, though. It seems like they skipped one of the git talks, but more likely I am just very confused.

leedo fucked around with this message at 20:39 on Jun 22, 2009

leedo
Nov 28, 2000

Did anyone else get sick from that dinner last night? I woke up with horrible stomach pain and a fever this morning. Now I am just hoping I don't puke on this flight back to Chicago. Not a great way to end the trip :(

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
n/m

Triple Tech fucked around with this message at 21:12 on Jun 24, 2009

Schweinhund
Oct 23, 2004

:derp:   :kayak:                                     
How do I print out all the elements of (what I believe is) a hash?

Basically I've been doing this:

$booklist['1']{'name'}= "The Bible";
$booklist['1']{'author'} = "Jesus";
$booklist['2']{'name'}= "Hop on Pop";
$booklist['2']{'review'}= "Great book!";

how do I go through and print every key & value for booklist without specifying every specific key?

I tried something like this
code:
for ($i=0;$i<10;$i++)
{
    while ( my ($key, $value) = each(%booklist[$i]) ) {
        print "$key => $value\n";
    }	
}
but it won't work.

Schweinhund fucked around with this message at 19:33 on Jun 25, 2009

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
code:
foreach my $book (@booklist) {
  say "$_ => $book->{$_}" for sort keys %$book;
}

Schweinhund
Oct 23, 2004

:derp:   :kayak:                                     

atomicstack posted:

code:
foreach my $book (@booklist) {
  say "$_ => $book->{$_}" for sort keys %$book;
}

works. thank you

S133460
Mar 17, 2008
hello
I just benchmarked the cookie parser I wrote today (called WWWhat::Cookie)...

code:
Benchmark: running CGI::Cookie::XS, CGI::Simple, WWWhat::Cookie for at least 10 CPU seconds...
CGI::Cookie::XS: 10.2537 wallclock secs (10.20 usr +  0.01 sys = 10.21 CPU) @ 13978.94/s (n=142725)
CGI::Simple: 10.6632 wallclock secs (10.62 usr +  0.01 sys = 10.63 CPU) @ 471.87/s (n=5016)
WWWhat::Cookie: 10.6166 wallclock secs (10.01 usr +  0.59 sys = 10.60 CPU) @ 52111.89/s (n=552386)

                   Rate     CGI::Simple CGI::Cookie::XS  WWWhat::Cookie
CGI::Simple       472/s              --            -97%            -99%
CGI::Cookie::XS 13979/s           2862%              --            -73%
WWWhat::Cookie  52112/s          10944%            273%              --
Muahahaha. The test cookie string was my cookies from the SA frontpage.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Just got home! I had a lot of fun. I think my favorite talk was Brock Wilcox's (awwaiid's) CGI::Inspect. Did anyone else see that poo poo?

satest3 posted:

I'm sure many of us who could not attend would like to read your talks should they be made available. *wink wink*

http://sartak.org/talks/yapc-na-2009/extending-moose/extending-moose.pdf It has my notes to myself, so it should be comprehensible even without me. If not, I did a bad job.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
I wrote a bit about how my class and talk went, which talks I really liked, and some other stuff.

The only one of the three goons there I met was Triple Tech, and that was only briefly. I spent all of my time with the Moose crowd because I am not at all outgoing.

CanSpice, your lightning talk was pretty interesting but the penis mention was a little gratuitous. We all have dirty minds..

Triple Tech, you PMed me a complaint about YAPC. Wanna talk about it here or in private or never?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

Sartak posted:

Triple Tech, you PMed me a complaint about YAPC. Wanna talk about it here or in private or never?

Nope, that's fine.

CanSpice
Jan 12, 2002

GO CANUCKS GO

Sartak posted:

CanSpice, your lightning talk was pretty interesting but the penis mention was a little gratuitous. We all have dirty minds..

That and I had things timed way too tightly, assuming I'd a) start on time and b) not screw up. I should've signed up for a 20-minute talk instead and given myself a little more time to screw up instead of standing up there trying to do a Matt Trout impression.

Captain Frigate
Apr 30, 2007

you cant have it, you dont have nuff teef to chew it
so im working with what are essentially files with binary data preceded by short header portions, and i need to read in some info on the files. normally i would just read and then unpack, but one of the things i need to unpack is an unsigned long long int, and i'm just not sure how to go about that because it doesn't look like unpack does that. is there any way i can do this without worrying about decoding the binary itself and fiddling with endianness?

wigga please
Nov 1, 2006

by mons all madden
Having some trouble with code running much too slow, here's a description of my problem (crossposted from a botting forum):

quote:

(It tests a random opponent hand and a random river (could be changed to more opponents and to both turn and river) against a hardcoded flop & turn and hardcoded pocket cards)

code:
use Games::Cards::Poker qw(:all);

my $players; # number of players to get hands dealt
my $hand_size; # number of cards to deal to each player
my @hands     = ();# player hand data
my @our_hand;
my @best_hands = ();
my @deck      = Shuffle(Deck());
my @board	  = qw(As Ks Td Js);
my $score;
my $our_score;
my $winner;
my $tied;
my $lost;

my $totalwins = 0;
my $totallosses = 0;
my $totalties = 0;

for (1..100){
$players   = 1; # number of players to get hands dealt
$hand_size = 2; # number of cards to deal to each player
@hands     = ();# player hand data
@our_hand  = qw(8d Ts);
@best_hands = ();
@deck      = Shuffle(Deck());
@board	  = qw(As Ks Td Js);
$score = 7462;
$our_score = 7462;
$winner = 0;
$tied = 0;
$lost = 0;
prepboard();
deal_opponents($players);
find_winner($players);
count_results();
#display_winner();
}
$prwin=($totalwins + ($totalties / 2))/100;
printf("$totalwins | $totalties | $totallosses ==> prwin $prwin ");

sub prepboard {
	foreach $board(@board) {
		@deck = RemoveCard($board,@deck);
	}
	foreach $our_hand(@our_hand) {
		@deck = RemoveCard($our_hand,@deck);
	}
	push(@board, pop(@deck));
}

sub deal_opponents {
	$players = $_[0];
	for($i=0; $i<$players; $i++) {
		push(@{$hands[$i]}, pop(@deck)) foreach(1..$hand_size);
#		printf("Player$i hole cards:\t @{$hands[$i]}\n");
		push(@{$hands[$i]}, @board);
	}
#	printf("\nOur hole cards:\t\t @our_hand\n");
	push(@our_hand, @board);
#	printf("\nBoard cards:\t\t @board\n");
#	printf("---------------------------------------\n");
}

sub find_winner {
	$players = $_[0];
	while($players--) {
		push(@{$best_hands[$players]}, BestHand(BestIndices(@{$hands[$players]}),@{$hands[$players]}));
#		printf("Player $players 's best hand:@{$best_hands[$players]}\n",
#									ScoreHand(@{$best_hands[$players]}));
		if (ScoreHand(@{$best_hands[$players]}) < $score) {
			$score = ScoreHand(@{$best_hands[$players]});
			$winner = $players;
		}
	}
	
	@our_best = BestHand(BestIndices(@our_hand),@our_hand);
#	printf("\nOur best hand:@our_best\n");
	$our_score = ScoreHand(@our_best);
	if ($our_score == $score) {
		$score = $our_score;
		$tied = 1;
	}
	elsif ($our_score < $score) {
		$score = $our_score;
		$winner = -1;
	}
	else {$lost = 1}

}

sub count_results {
	if ($winner == -1) {
		$totalwins++;
	}
	elsif ($tied == 1) {
		$totalties++;
	}
	elsif ($lost == 1) {
		$totallosses++;
	}
}

sub display_winner {
	printf("---------------------------------------\n");
	$handName = HandName($score);
	$vhandName = VerboseHandName($score);
	if ($winner !=-1) {
		printf("Player $winner wins with $handName, $vhandName\n");
		printf("Losses:$totallosses\nTies: $totalties");
	}
	else {
		printf("We win with $handName, $vhandName\n");
		printf("Wins: $totalwins");
	}
}
Dprof analysis:



tl;dr: my poo poo is slow, why are some routines (Games::Cards::Poker::SortCards) called 8300 times?

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
Try going over it with NYTProf, it will tell you where they're called from and how often.

wigga please
Nov 1, 2006

by mons all madden

Mithaldu posted:

Try going over it with NYTProf, it will tell you where they're called from and how often.

Thanks, that looks like a better debugger (as you may have noticed I have no Perl skills at all, this is the first time I'm actually debugging past console warnings).

This
code:
sub ScoreHand { # returns the score of the passed in @hand or ShortHand
964	62088	156ms	3µs	my @hand = @_; return(7462) unless(@hand == 1 || @hand == 5); my $shrt;
965	62088	31.2ms	503ns	my $aflg = 0; $aflg = 1 if(ref($hand[0]) eq 'ARRAY'); my $aref;
966	20696	0s	0s	if( $aflg ) { $aref = $hand[0]; }
967	20696	31.2ms	2µs	else { $aref = \@hand; }
968	20696	15.6ms	754ns	if(@{$aref} == 1) { $shrt = $aref->[0]; }
969	20696	78.0ms	4µs	else { $shrt = ShortHand($aref); }
# spent 1.89s making 20696 calls to Games::Cards::Poker::ShortHand, avg 91µs/call
970	20696	0s	0s	if( $slow ) { return(SlowScoreHand($shrt)); }
971	20696	62.4ms	3µs	else { return( $zdnh{$shrt}); }
is what seems to be the problem, the ShortHand sub shouldn't be called at all as normally hands are already in this format. I'll take a look at how I can solve this.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
If you're rather new to Perl, please make sure you have use strict, use warnings and use diagnostics active. You might have scoping problems due to missing "my"s. Trying to get a copy of the book "Perl Best Practices" might be a good idea too.

wigga please
Nov 1, 2006

by mons all madden

Mithaldu posted:

If you're rather new to Perl, please make sure you have use strict, use warnings and use diagnostics active. You might have scoping problems due to missing "my"s. Trying to get a copy of the book "Perl Best Practices" might be a good idea too.

Thank, that already sped things up a little. I'll need a lot more speed though :smith:

edit: looks like Perl can't do this speed-wise, optimized scopes (5% speed increase) but still 40% of cycles are used for sorting. Might be best to just call poker-eval (which is in C++) from Perl.

wigga please fucked around with this message at 17:08 on Jul 5, 2009

Captain Frigate
Apr 30, 2007

you cant have it, you dont have nuff teef to chew it

Captain Frigate posted:

so im working with what are essentially files with binary data preceded by short header portions, and i need to read in some info on the files. normally i would just read and then unpack, but one of the things i need to unpack is an unsigned long long int, and i'm just not sure how to go about that because it doesn't look like unpack does that. is there any way i can do this without worrying about decoding the binary itself and fiddling with endianness?

anyone have an answer for this? could i just read in two different long ints and then add the second to the first after padding it with zeros?

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

bloodytourist posted:

Thank, that already sped things up a little. I'll need a lot more speed though :smith:

edit: looks like Perl can't do this speed-wise, optimized scopes (5% speed increase) but still 40% of cycles are used for sorting. Might be best to just call poker-eval (which is in C++) from Perl.

Try running it through nytprof into html, then compress that and upload it at drop.io so we can have a look.

Adbot
ADBOT LOVES YOU

wigga please
Nov 1, 2006

by mons all madden
Here's the pl + NYTProf report, I also included my pity attempt accessing Timmy's PSim DLL in case anyone knows how to handle this without the painstaking labor of writing an xs wrapper.

http://drop.io/qwdbbh7

  • Locked thread