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
Fenderbender
Oct 10, 2003

You have the right to remain silent.

SA Support Robot posted:

Does anyone here use AnyEvent? I am madly in love with it and wish to project my love upon others.

:swoon:

That's pretty cool. For curiosity's sake, what has been your greatest use for it so far?

Adbot
ADBOT LOVES YOU

leedo
Nov 28, 2000

The IRC bot talk a few posts back reminded me to upload this IRC HTML formatting module I have been sitting on. So if you every need to export raw IRC to HTML give it a try!

http://search.cpan.org/~leedo/IRC-Formatting-HTML-0.01/

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
AnyEvent is pretty great. I'm using it as part of AnyEvent::XMPP. I love that I don't have to deal with POE. :toot:

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

SA Support Robot posted:

Does anyone here use AnyEvent? I am madly in love with it and wish to project my love upon others.

:swoon:

If you wish to project your love onto others then you should talk about actual, real practical applications. Personally i wouldn't have a clue about what it's useful for. :)

SA Support Robot
Apr 20, 2007

Fenderbender posted:

That's pretty cool. For curiosity's sake, what has been your greatest use for it so far?

Probably the most extensive use of it is for the forums' new search engine. There's an app called "indexmaster" that runs on each of the search servers. It gets information from the forums about what indexes need to be maintained and forks a worker for each one. The master decides when action is needed for a particular index, and sends tasks to the workers over a unix socket. The workers do database work, run indexing jobs, etc, and send progress updates back to the master.

The master process also has a tcp-based management interface that can be used to request index status information, check the search daemon status, kick off index jobs manually, alter the running configuration, etc. For example I can telnet in and tell a search server to put GBS into its own index. :cool:

Sartak posted:

AnyEvent is pretty great. I'm using it as part of AnyEvent::XMPP. I love that I don't have to deal with POE. :toot:

I also moved to it from POE... never did love the POE API. I had ended up using Danga::Socket for a lot of things, but ever since Brad Fitzpatrick went to Google, I've moved away from a lot of his stuff.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Use Devel::NYTProf for gently caress's sake. It's amazing and is the only Perl profiler that doesn't suck.

Watch Tim Bunce's NYTProf talk from the other day at OSCON.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Sartak posted:

Use Devel::NYTProf for gently caress's sake. It's amazing and is the only Perl profiler that doesn't suck.
Completely agreeing with this. (Heck, it even makes you want to refactor code so you can tell better what its output really means.)

Sartak posted:

Watch Tim Bunce's NYTProf talk from the other day at OSCON.
This makes me wonder though: Is there a rule that all Perl-related conferences have to have lovely microphones? I think OSCON 2009 is the first one where the quality of the videos afterwards was tolerable, but still below average.

Edit: Holy crap, NYTProf 3 sounds sexy.

Mithaldu fucked around with this message at 19:08 on Jul 24, 2009

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
The new treemap looks really useful.

leedo
Nov 28, 2000

Ok, so here is a potentially stupid Moose question.

I have a class that has a "config" property, and a "server" property. The creation of the server property requires that the config property be set so that I can use some of the config fields when setting up the server. Normally I would just set the server property to lazy like so:
code:
has => 'config' => (
  is => 'ro',
  isa => 'HashRef',
  required => 1,
);

has => 'server' => (
  is => ro,
  isa => 'HTTPD::Server',
  lazy => 1,
  default => sub {
    # build server with access to $self->config
  }
);
The problem here is that I need server to be built at initialization, not lazily. But, if I set server to not be lazy config is not defined yet. I tried using a builder but it still seems to fire off before config is defined.

Do I need to move this into a BUILD sub? Or is there some way to specify the order that properties are defined?

edit: I got this working by setting the server in a trigger on config. Still not sure if that is the best approach, though.

leedo fucked around with this message at 23:21 on Jul 30, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

leedo posted:

The problem here is that I need server to be built at initialization, not lazily. But, if I set server to not be lazy config is not defined yet.

Do I need to move this into a BUILD sub?

I'd just add the following to your code:

code:
sub BUILD {
    $self->server; # just initialize
}
That way your server is initialized after your client attribute, but before "new" returns.

leedo posted:

I tried using a builder but it still seems to fire off before config is defined.

builder and default are really the same thing. Seriously, the only difference is builder takes a method name, and default takes a code reference.

leedo posted:

edit: I got this working by setting the server in a trigger on config.

That seems okay too.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
This just came up in IRC, guess what this prints:

code:
my @x = ("a", "b", "c", "d");

$x[2] = undef;
print defined($x[2]) ? "defined, " : "undefined, ";
print exists($x[2]) ? "exists\n" : "doesn't exist\n";

delete $x[2];
print defined($x[2]) ? "defined, " : "undefined, ";
print exists($x[2]) ? "exists\n" : "doesn't exist\n";
That's right, magical undef :2bong: (word to Otto)

edited for clarity

Filburt Shellbach fucked around with this message at 00:23 on Jul 31, 2009

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
I'm not entirely clear on the internals, but it makes sense to me and follows the manual.

perldoc posted:

Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined.
Correct me if i'm wrong:
An array is basically a list of addresses pointing at memory entries that carry the values of the array contents. As such an index can either have no address at all (not exists) or the adress to a memory location which carries the value for undefined (exists).

Mithaldu fucked around with this message at 00:26 on Jul 31, 2009

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Didn't know until now that exists and delete could be used on things not hashes. They were always hash methods to me.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Mithaldu posted:

an index can either have no address at all (not exists) or the adress to a memory location which carries the value for undefined (exists).

That's right. It's the same way hashes work with exists versus defined.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
^^^^
Thanks. :)

Triple Tech posted:

Didn't know until now that exists and delete could be used on things not hashes. They were always hash methods to me.
Huh, i didn't even consider it in that way or rather, didn't know of exists before today.

When would i want to use exists specifically versus undefined? I understand the difference in what it does, but not the practical use.

leedo
Nov 28, 2000

Mithaldu posted:

^^^^
Thanks. :)
Huh, i didn't even consider it in that way or rather, didn't know of exists before today.

When would i want to use exists specifically versus undefined? I understand the difference in what it does, but not the practical use.

defined will autovivify the hash element, where as exists will not. At least that is how I always understood it.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
No, autovivification happens when you access a deep structure.

return $a{b}{c};

That autovivifies $a{b} to be a hashref, even if %a was empty.

You care about defined versus exists when undef is a valid value. For example, when your attribute can contain any Perl value (including undef) and you need to know when the attribute has a value or not, then you use exists.

leedo
Nov 28, 2000

Sartak posted:

No, autovivification happens when you access a deep structure.

return $a{b}{c};

That autovivifies $a{b} to be a hashref, even if %a was empty.

You care about defined versus exists when undef is a valid value. For example, when your attribute can contain any Perl value (including undef) and you need to know when the attribute has a value or not, then you use exists.

Wow, I could have sworn defined would create the hash key. But yeah I just did a little test and you're definitely right.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
FYI if anybody still hasn't heard, Perlmonks was hacked along with Mitnick and Kaminsky, admin/saint passwords from September 2008 were leaked but it's likely all accounts were affected.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Wow, nerds, way to be.

S133460
Mar 17, 2008
hello

Sartak posted:

This just came up in IRC, guess what this prints:

code:
my @x = ("a", "b", "c", "d");

$x[2] = undef;
print defined($x[2]) ? "defined, " : "undefined, ";
print exists($x[2]) ? "exists\n" : "doesn't exist\n";

delete $x[2];
print defined($x[2]) ? "defined, " : "undefined, ";
print exists($x[2]) ? "exists\n" : "doesn't exist\n";

That seems to behave as expected. What am I missing?

Mithaldu posted:

An array is basically a list of addresses pointing at memory entries that carry the values of the array contents. As such an index can either have no address at all (not exists) or the adress to a memory location which carries the value for undefined (exists).

Right. An array is a list of pointers to scalars. A scalar is kinda like an object, and there are several types of scalars (IV for integer values, PV for pointers [ie, strings], RV for references to other scalars, etc). Perl also creates a few special static shared scalars... &PL_sv_undef for representing undefined values, plus &PL_sv_yes and &PL_sv_no for storing the result of boolean evaluation. Perl saves memory this way.

An array is not a scalar. A hash is not a scalar. This is why you need a scalar reference (an RV) to pass around an array or hash. It also allows Perl to detect "list context" and do other weird Perl things.

code:
my @av;          # an empty array, has no scalar pointers yet

$av[3] = 'foo';  # extends @av, now has 4 scalar pointers with
                 #   elements 0-2 pointing to &PL_sv_undef and
                 #   element 3 pointing to a PV scalar 'foo'

undef $av[3];    # @av STILL contains 4 scalar pointers, but
                 #   they all point to &PL_sv_undef; if no more
                 #   references to the 'foo' PV exist, that
                 #   scalar will be garbage collected soon

exists $av[3];   # true because the size of the array has not
                 #   changed... there are still 4 pointers

$#av = 1;        # change the number of elements in @av then...
exists $av[3];   # false because @av is smaller than the index
Basically, undefined is a value that indicates no value is defined. :)

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

satest3 posted:

That seems to behave as expected. What am I missing?

You're not missing anything, you just expected it to work the way it actually does. I was certainly surprised when I learned that exists on a middle element of an array could be useful.

baquerd
Jul 2, 2007

by FactsAreUseless
I don't have an interpreter handy, but I expect:

undefined
exists
defined
exists

Am I right?

baquerd fucked around with this message at 16:32 on Aug 1, 2009

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

quadreb posted:

I don't have an interpreter handy, but I expect:

undefined
exists
defined
exists

Am I right?

No. http://codepad.org

http://codepad.org/Q8YbMVce even

baquerd
Jul 2, 2007

by FactsAreUseless

Otto Skorzeny posted:

No. http://codepad.org

http://codepad.org/Q8YbMVce even

Yeah, just read the doc, I thought delete would remove the element and shift the array. Ah well, never use it anyway.

CanSpice
Jan 12, 2002

GO CANUCKS GO

atomicstack posted:

FYI if anybody still hasn't heard, Perlmonks was hacked along with Mitnick and Kaminsky, admin/saint passwords from September 2008 were leaked but it's likely all accounts were affected.

The stunning thing is that they stored passwords in plaintext in their database.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Still lolin @ '>=6chars' btw

CanSpice
Jan 12, 2002

GO CANUCKS GO

CanSpice posted:

I'm writing a blog post outlining the steps I took to create my vessel in hopes that it helps other people.

Blammo. It's a lot of :words: but hopefully it helps someone out.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

CanSpice posted:

Blammo.

Cool! I've forwarded it on to sunnavy.

It looks irritating to have to run the builder twice on test fail, I wonder if printing the last 15 lines of the output would be enough.

Thanks for filing those bugs. Robustness owns. :twisted:

edit: sunnavy said that without --verbose, all the commands' output goes to build.log. Good enough for me.

Filburt Shellbach fucked around with this message at 04:16 on Aug 4, 2009

CanSpice
Jan 12, 2002

GO CANUCKS GO

Sartak posted:

Cool! I've forwarded it on to sunnavy.

It looks irritating to have to run the builder twice on test fail, I wonder if printing the last 15 lines of the output would be enough.

Thanks for filing those bugs. Robustness owns. :twisted:

edit: sunnavy said that without --verbose, all the commands' output goes to build.log. Good enough for me.

Yeah, I didn't know about that build.log file when I was doing this. I've added a footnote about that, prompted by sunnavy's comment on my post. Thanks for forwarding that on to him!

General question for the thread: is there a decent module out there for finding out how much memory a computer has? I have a Perl program that calls another app that writes out files, and I can limit those output files by size, and I want to do that by looking at how much memory the computer has. This code needs to run on at least 32- and 64-bit Linux and, if possible, some flavour of recent Solaris as well as OS X.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

CanSpice posted:

General question for the thread: is there a decent module out there for finding out how much memory a computer has? I have a Perl program that calls another app that writes out files, and I can limit those output files by size, and I want to do that by looking at how much memory the computer has. This code needs to run on at least 32- and 64-bit Linux and, if possible, some flavour of recent Solaris as well as OS X.

Sys::MemInfo looks like it's simple and builds on basically everything

CanSpice
Jan 12, 2002

GO CANUCKS GO

Otto Skorzeny posted:

Sys::MemInfo looks like it's simple and builds on basically everything

That looks perfect, thanks.

Captain Frigate
Apr 30, 2007

you cant have it, you dont have nuff teef to chew it
I'm a little shaky about how perl deal with namespaces and returning variables. Say I have a subroutine that generates a hash, with at least one hash inside the hash that I'm ultimately trying to generate, like so:

code:
sub make_hashes {
     %little_hash=("foo",0,"bar",0);
     %daddy_hash=("little_hash",%little_hash,"thing_1",0,"thing_2",0);
     return(%daddy_hash);
}

%test=makes_hashes;

What exactly do I have in %test? A copy of %daddy_hash including little_hash? Something else?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Man that code hurts so much... I hope someone else fields this. %test is a hash defined by the list returned by make_hashes(). So, I think at face value, your code does what you want.

Just know that it's poorly written, stylistically, and is poor Perl.

Captain Frigate
Apr 30, 2007

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

Triple Tech posted:

Man that code hurts so much... I hope someone else fields this. %test is a hash defined by the list returned by make_hashes(). So, I think at face value, your code does what you want.

Just know that it's poorly written, stylistically, and is poor Perl.

Why's that, and how do I fix it?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Why I'm glad you asked! :D It's because it doesn't look like this.

code:
sub make_hashes {
  my %little_hash = qw(foo 0 bar 0);

  my %daddy_hash = (
    little_hash => %little_hash,     # oh snap, problem here
    thing_1     => 0,
    thing_2     => 0,
  );

  return %daddy_hash;
}

my %test = makes_hashes;
A really important thing to note here is that this was just a superficial approach to improving your code. Other things could be changed like returning the hash via reference instead of by value or using a different methodology altogether. Having not merged hashes often in my work, I'm thinking there's probably a different and better way to go about solving your problem.

Also it's now really clear, by restyling the code, that your %daddy_hash has an uneven number of elements in its assignment. If you wanted nested hashes, that's an entirely different beast.

Erasmus Darwin
Mar 6, 2001

Captain Frigate posted:

code:
     %daddy_hash=("little_hash",%little_hash,"thing_1",0,"thing_2",0);
}

What you want here is to use \%little_hash instead of just %little_hash. With the slash there, $daddy_hash{little_hash} will contain a hash reference to %little_hash. Without the \, the contents of %little_hash get flattened into the list being used to define %daddy_hash, which throws things off since you've effectively doing this:
code:
%daddy_hash = ("little_hash", "foo",
               0, "bar",
               0, "thing_1",
               0, "thing_2",
               0);
And that's just all sorts of screwed up. I'm not sure exactly how Perl would handle this since you've got a bunch of repeat assignments to the 0 key, and you've got a mismatched key at the end.

I think Triple Tech tried to point this out with his "oh snap" comment, but I'm not sure he fully understood the degree of your misunderstanding. Or maybe I've misunderstood.

Also, if you use the \ notion, you need to be aware that a reference to %little_hash points to the same memory that %little_hash uses -- it's not a copy. So if you do '$little_hash{blahblah} = 7' after creating %daddy_hash, it'll still modify the contents of the hash in $daddy_hash{little_hash}. Of course once your function exits, there won't be a reference to %little_hash anymore, so you no longer have to worry about it being modified via things not directly accessing the contents of %daddy_hash (unless you pass the reference somewhere else).

Finally, Data::Dumper is your friend for testing various data structures. Just throw "use Data::Dumper;" at the beginning of your program and then stick "print Dumper(\%test);" at the end, and it'll print a nice, readable representation of what your data structure looks like.

Erasmus Darwin fucked around with this message at 14:35 on Aug 11, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Erasmus Darwin posted:

code:
%daddy_hash = ("little_hash", "foo",
               0, "bar",
               0, "thing_1",
               0, "thing_2",
               0);
And that's just all sorts of screwed up. I'm not sure exactly how Perl would handle this since you've got a bunch of repeat assignments to the 0 key, and you've got a mismatched key at the end.

When you have multiple values for the same key like this, the last one wins. It's useful when you have a function that takes named parameters and you want to specify the defaults for some parameters:

code:
sub send_mail {
    my %args = (
        from => $ENV{EMAIL},
        @_,
    );
    ...
Or you could specify @_ first and override certain parameters that were passed in (to make from always be $ENV{EMAIL} no matter what the caller tried to specify.

When you have an odd-sized hash, the last key gets value undef. So in this case, %daddy_hash looks like:

code:
%daddy_hash = (
    "little_hash" => "foo",
    0 => undef,
);

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Sartak posted:

useful stuff about named arguments

Whenever I have a func that takes named arguments, I always seem to end up using them in a nested hash or something else later and wind up doing something like this
code:
my %args = @_;
my $foo = $args{foo} || foo_default;
my $bar = $args{bar} || bar_default;
...
to avoid having $somethingelse{$args{foo}} = { baz => $args{baz}, fred => $args{fred},} issues and other trash like that later :(

Adbot
ADBOT LOVES YOU

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Be careful of using || as the "default" operator. What you really want is "defined-or" which was introduced in 5.10 as // (they lean).

Careful cuz you know why, right? truthiness of zero

  • Locked thread