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
Ninja Rope
Oct 22, 2005

Wee.

Ninja Rope posted:

I don't want to talk out my rear end without testing it, but I'm pretty sure that's not necessary. I've got some broadcast socket code somewhere I'll try and dig up later today and let you know how I got that working.

Okay, so I see what's going on here. When you create the broadcast socket with a remote address of 255.255.255.255, it will only send data to and receive data sent from that address, as if it were a connected socket. In your case, you should create the socket without specifying a remote address, and then specify the destination address as a packed sockaddr in your call to send(). Like this:

code:
$socket = IO::Socket::INET->new(
    LocalPort => $local_port,
    Proto     => 'udp',
    Broadcast => 1,
) or die "Socket failed: $!";
$broadcast_sockaddr = pack_sockaddr_in( $local_port, inet_aton( '255.255.255.255' ) );
$socket->send( $message, 0, $broadcast_sockaddr ) or die $!;
Note, you might receive the same broadcast packet you just sent, so be ready to filter that out of necessary.

Adbot
ADBOT LOVES YOU

JawnV6
Jul 4, 2004

So hot ...

Ninja Rope posted:

Okay, so I see what's going on here. When you create the broadcast socket with a remote address of 255.255.255.255, it will only send data to and receive data sent from that address, as if it were a connected socket. In your case, you should create the socket without specifying a remote address, and then specify the destination address as a packed sockaddr in your call to send(). Like this:
<snip>
Note, you might receive the same broadcast packet you just sent, so be ready to filter that out of necessary.

Ah! That makes sense, thanks. When I get home I'll screw around with it some more, thanks for the help!

Erasmus Darwin
Mar 6, 2001

syphon^2 posted:

Doing it this way means I have to rip out a bunch of code an put it in a separate .pl file though. :( (or can Win32::Process::Create execute Perl code within the same Perl executable?)

You can stick it in the same script if you want. Just invoke the child processes with /child as one of the arguments and check @ARGV when you start up.

quote:

EDIT: I just thought of something else... Win32::Process::Create is like fork, in that I can't share variables, right?

Yup. A quick and easy workaround is to just have the child processes write their results to textfiles that're checked by the parent process.

Ninja Rope
Oct 22, 2005

Wee.

JawnV6 posted:

Ah! That makes sense, thanks. When I get home I'll screw around with it some more, thanks for the help!

If you get stuck again on this let me know. I've got some code I use that does exactly this, but I'd rather not post it here.

JawnV6
Jul 4, 2004

So hot ...

Ninja Rope posted:

If you get stuck again on this let me know. I've got some code I use that does exactly this, but I'd rather not post it here.

I ended up closing and re-opening, it's working fine from watching it on wireshark. Thanks.

Ninja Rope
Oct 22, 2005

Wee.

JawnV6 posted:

I ended up closing and re-opening, it's working fine from watching it on wireshark. Thanks.

Doesn't this introduce a timing issue, though? You can't guarantee how fast you can close and reopen the socket, or even that you can at all, if the OS runs out of sockets or your process out of FDs.

Aquarium of Lies
Feb 5, 2005

sad cutie
:justtrans:

she/her
Taco Defender
I hope this is the right thread for this.


The long story is I'm tried to install Catalyst on some shared hosting. The server I'm on provides perl 5.8.4, and I ran into some problems working with CPAN with that so I decided to install perl 5.10.0 in my user directory.

I get it to compile and install perfectly fine. I'm not too sure on the specifics, but it also seems to set my @INC just fine:

code:
[atlantic]$ which perl
/home/kylebox/local/bin/perl
[atlantic]$ perl -e 'print join("\n",@INC)'
/home/kylebox/local/lib/perl5/5.10.0/i686-linux
/home/kylebox/local/lib/perl5/5.10.0
/home/kylebox/local/lib/perl5/site_perl/5.10.0/i686-linux
/home/kylebox/local/lib/perl5/site_perl/5.10.0
My next goal is to set up CPAN, upgrade my modules, then install Catalyst. Now here's where the problem lies. I use a basically stock setup for CPAN, except I have
code:
  'make_install_arg' => q[UNINST=1],
  'makepl_arg' => q[PREFIX=~/local UNINST],
  'mbuild_install_arg' => q[--uninst 1],
  'mbuildpl_arg' => q[--install_base ~/local],
Most modules install without a hitch; for example, CGI:
code:
cpan[1]> install CGI
...
Installing /home/kylebox/local/lib/perl5/5.10.0/CGI.pm
Installing /home/kylebox/local/lib/perl5/5.10.0/CGI/Carp.pm
Installing /home/kylebox/local/lib/perl5/5.10.0/CGI/Fast.pm
Installing /home/kylebox/local/lib/perl5/5.10.0/CGI/Util.pm
Installing /home/kylebox/local/lib/perl5/5.10.0/CGI/Pretty.pm
Installing /home/kylebox/local/lib/perl5/5.10.0/CGI/Cookie.pm
...
but some don't install under the an @INC directory; for example, upgrading constant:
code:
cpan[2]> upgrade constant
...
Installing /home/kylebox/local/lib/perl5/constant.pm
Unlinking /home/kylebox/local/lib/perl5/5.10.0/constant.pm (shadowing?)
...
but lib/perl5 isn't in @INC:
code:
cpan[3]> i constant
Strange distribution name [constant]
Module id = constant
    DESCRIPTION  Define compile-time constants
    CPAN_USERID  SAPER (Sebastien Aperghis-Tramoni <maddingue@free.fr>)
    CPAN_VERSION 1.16
    CPAN_FILE    S/SA/SAPER/constant-1.16.tar.gz
    UPLOAD_DATE  2008-09-21
    DSLIP_STATUS Supf? (standard,comp.lang.perl.*,perl,functions,)
    MANPAGE      constant - Perl pragma to declare constants
    INST_FILE    (not installed)
Now from here I originally edited .bash_source to add export PERL5LIB=$HOME/local/lib/perl5, which seemed to do the job. It fails, however, when I try to access a perl script via my browser. error.log:
code:
[Sat Oct 25 13:17:45 2008] [error] [client 76.214.139.45] Can't locate constant.pm in @INC
  (@INC contains: /home/kylebox/local/lib/perl5/5.10.0/i686-linux /home/kylebox/local/lib/perl5/5.10.0
  /home/kylebox/local/lib/perl5/site_perl/5.10.0/i686-linux /home/kylebox/local/lib/perl5/site_perl/5.10.0 .)
  at /home/kylebox/local/lib/perl5/5.10.0/CGI.pm line 33.
[Sat Oct 25 13:17:45 2008] [error] [client 76.214.139.45] BEGIN failed--compilation aborted at
  /home/kylebox/local/lib/perl5/5.10.0/CGI.pm line 33.
I know I can use use lib in my scripts to fix this error, but it just seems to me this is a hackish solution for CPAN's installing to local/perl5 instead of local/perl5/5.10.0/. Also, my local dev machine installation doesn't have this problem (I own it, so perl 5.10.0 was installed as root)

Any ideas on how to stop CPAN from installing certain modules in lib/perl5/? It's happened on new installations (ie not an upgrade) and it hasn't happened on all upgrades.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
local::lib is worth having a look at. It manages all the environment twiddling to let CPAN install into your home dir, and having Perl find it.

Aquarium of Lies
Feb 5, 2005

sad cutie
:justtrans:

she/her
Taco Defender
Huh. Well.

Since perl had properly set up @INC for the installation, I decided to see if it also worked for CPAN. The only change I made is I removed PREFIX and --install_base from Config.pm and now it works perfectly. :)

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
There's going to be another Frozen Perl in Minneapolis on February 7th. I'll be there. I've submitted a talk for Devel::REPL and Carp::REPL that will probably be accepted (though I won't find out until November 20th).

Anyone else planning on going?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Are there any more of these Perl conferences listed somewhere? I know there's the YAPC website, but Frozen Perl wasn't listed on there. Am I missing any else?

I want to eventually have my work send me there. Or at the very least I just tank it myself with vacation days.

Hmm, how much for plane and stay in Sydney...

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
If you're logged into the conference site you can see something like this in your main page:



(Since the conference I'm browsing from is Frozen Perl 2009 it doesn't link it)

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I signed up, but it's kind of gay there isn't a master list of these things in order. So far the only things on my radar are Frozen Perl and OSDC Australia, which is a $3000 package (hotel, flight). Does that sound reasonable for work to expense or is it too much? Hmm maybe they'll only expense the days that I'm actually at the conference...

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
If they can be convinced of a trip to OSDC, now is probably the best time to pay - the Aussie dollar has tanked in the last few months relative to the USD.

German Joey
Dec 18, 2004

Sartak posted:

There's going to be another Frozen Perl in Minneapolis on February 7th. I'll be there. I've submitted a talk for Devel::REPL and Carp::REPL that will probably be accepted (though I won't find out until November 20th).

Anyone else planning on going?

i just looked at this and it seems like Carp::REPL owns. is there any chance that its pure perl?

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
It is almost pure perl. It depends on PadWalker to get (and set!) lexical information for the entire stack. PadWalker is implemented in C but it's so loving useful that it may eventually get cored.

The only other tricky bit is hooking into die. You can do this by sticking a code reference in $SIG{__DIE__} and it'll be run before the program aborts.

code:
$SIG{__DIE__} = \&repl;
That's it. :D

ashgromnies
Jun 19, 2004
So I am trying to do something that I think should be pretty simple, but it's just not coming to my mind.

I have a reference to an element in an array and I want to completely remove it from the array. How can I delete the array's reference to it without knowing its position in the array? I don't think you can do this with delete.


Ex:

code:
my @arr = ("my", "balogna", "has", "a", "first", "name", "and", "it's", "not", "oscar", "meyer");

foreach my $el(@arr){
    undef $el if $el eq 'not';
}
which obviously won't work the way I want it to - it leaves undef in the array.

Neither does this work correctly...

code:
my @arr = ("my", "balogna", "has", "a", "first", "name", "and", "it's", "not", "oscar", "meyer");

foreach my $el(@arr){
    $el = () if $el eq 'not';
}
Ideas?

edit: of course, I can always follow that loop up with @arr = grep { $_ } @arr to resolve it but I'm looking for something cleaner.

ashgromnies fucked around with this message at 22:14 on Oct 31, 2008

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender
@arr = grep { $_ ne 'not' } @arr;

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
ShoulderDaemon's way is probably best. It's the clearest which is the most important thing for code.

Anyway, the operator you're looking for is splice. Here's how you can use it:

code:
for (my $i = 0; $i < @arr; ) {
    if ($arr[$i] eq 'not') {
        splice @arr, $i, 1;
    }
    else {
        ++$i;
    }
}
One indicator that this code sucks is the C-style for loop.

ashgromnies
Jun 19, 2004
Sorry, I should have clarified, I need to do some work on the value. I am passing an array ref recursively to a function, and want to erase elements from it when they've been processed.

Maybe I can explain the entire thing and it will make more sense...

I have a list of directory names belonging to various projects in an array. Every project has a root directory with a special application file in it that lets you know it's the root. Projects may be infinitely nested within eachother.

So what I'm trying to do is find the nearest application file to every directory in the array.

Here's the code I have thusfar:

code:
sub find_application_ymls {
    my ($directories_ref, $application_ymls_ref) = @_;

    # base case - return if no directories left in list
    # to process.
    return if !scalar @{$directories_ref};

    foreach my $dir(@{$directories_ref}){
        my $yml_path = "$dir/application.yml";
        if(-e $yml_path){
            # found one
            push @{$application_ymls_ref}, $yml_path;
            # delete $dir from array
            undef $dir;
        } else {
            # wasn't at this level, go one up
            $dir =~ s/\/[^\/]+$//;
        }       
    }

    # wipe out undef
    @{$directories_ref} = grep { $_ } @{$directories_ref};

    # recurse
    &find_application_ymls($directories_ref, $application_ymls_ref);
}
actually I should really use a hashref for application_ymls_ref to prevent duplicates from appearing.

Anyways, this works, I was just wondering if there's a better way.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

ashgromnies posted:

Sorry, I should have clarified, I need to do some work on the value. I am passing an array ref recursively to a function, and want to erase elements from it when they've been processed.

code:
sub find_application_yml {
    my ($application_ymls_ref, $dir) = @_;

    my $yml_path = "$dir/application.yml";
    if(-e $yml_path){
        # found one
        push @{$application_ymls_ref}, $yml_path;
        # delete $dir from list
        return 0;
    } else {
        # leave $dir in list
        return 1;
    }       
}

# Process until we're done.
while ( length @{$directories_ref} ) {
    # Process everything in the list, producing a new list of dirs to process.
    @{$directories_ref} = map { s/\/[^\/]+$// } grep { find_application_yml( $application_ymls_ref, $_ ) } @{$directories_ref};
};
Untested.

Mario Incandenza
Aug 24, 2000

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

quote:

map { s/\/[^\/]+$// }
This is kinda ugly. Use File::Basename, it's a core module and all:
code:
use File::Basename qw/dirname/;

map { dirname($_) }

Roseo
Jun 1, 2000
Forum Veteran

ashgromnies posted:

So I am trying to do something that I think should be pretty simple, but it's just not coming to my mind.

I have a reference to an element in an array and I want to completely remove it from the array. How can I delete the array's reference to it without knowing its position in the array? I don't think you can do this with delete.



code:
my @arr = ("my", "balogna", "has", "a", "first", "name", "and", "it's", "not", "oscar", "meyer");

my @temparr = @arr;
my @arr = ();

foreach my $el(@temparr){
     push (@arr, $el) unless $el eq 'not';
}
It's not one line, but it's simple and concise.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ashgromnies posted:

Anyways, this works, I was just wondering if there's a better way.

Don't think of @{$directories_ref} as an array, think of it as a queue:

code:
sub find_application_ymls {
  my ($directories_ref, $application_ymls_ref) = @_;

  while (my $dir = shift @{$directories_ref}) {
    my $yml_path = "$dir/application.yml";
    if (-e $yml_path) {
      # found one
      push @{$application_ymls_ref}, $yml_path;
    } else {
      # wasn't at this level, go one up
      $dir =~ $s/\/[^\/]+$//;
      push @{$directories_ref}, $dir;
    }       
  }
}

ashgromnies
Jun 19, 2004

rjmccall posted:

Don't think of @{$directories_ref} as an array, think of it as a queue:


loving ace. I always forget about queues working in Perl all the time :)

I also didn't know about File::Basename... is there a good core module reference anywhere?

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Triple Tech posted:

:( It's a shame that Devel::NTYProf doesn't work on Windows.

2.07, released today, does!

There Will Be Penalty
May 18, 2002

Makes a great pet!

ashgromnies posted:

loving ace. I always forget about queues working in Perl all the time :)

I also didn't know about File::Basename... is there a good core module reference anywhere?

perlmodlib(1)

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Sartak posted:

2.07, released today, does!

Hm, using ActivePerl, neither cpan nor cpanplus show it. How would i go about getting it?

Nevermind, selecting inode as mirror showed it.

Also, NYTProf doesn't work with Coro at all. Throws bunches of errors and trying to run nytprofhtml on the out file results in it trying to read the file, then crashing without even so much as a peep. Procmon shows it accessing the file and then directly jumping to the vsjitdebugger.

Mithaldu fucked around with this message at 22:41 on Nov 1, 2008

toadee
Aug 16, 2003

North American Turtle Boy Love Association

I'm having difficulty trying to get the module HTTP::GHTTP to have a timeout value. I'm using GHTTP because I'm trying to pull down a ton of very small objects simultaneously, and it's from all accounts the most lightweight of perl HTTP modules.

From the module's CPAN page:

quote:

Doing timeouts is an exercise for the reader (hint: lookup select() in perlfunc).

Ok, so I have the following:

code:
my $r = HTTP::GHTTP->new($url);

$r->set_async;
$r->set_chunksize(1024);
$r->prepare;

my $stat;
while ($stat = $r->process) {
   select(undef, undef, undef, 15);
}
Which seems to just sit there and not actually timeout after 15 seconds when things go wrong. I'm at a loss as to what else to try, mostly because I'm stupid, I'm sure.

EDIT: and by stupid I mean I got that while loop with select statement from someone elses suggestion online and stuck it in there without having a clue why it would work, so maybe that's why it doesn't.

toadee fucked around with this message at 12:13 on Nov 8, 2008

TiMBuS
Sep 25, 2007

LOL WUT?

Well, select(undef, undef, undef, 15); should timeout for 15 seconds, so if its hanging indefinitely then it's probably something to do with the $r->process method call. Perhaps the request is timing out? Maybe the async call is being messed up by select causing a halt? Dunno. Make it print for every iteration to see whats going on.
Use warn instead of print so that you can output to stderr and you wont have buffering issues.

toadee
Aug 16, 2003

North American Turtle Boy Love Association

TiMBuS posted:

Well, select(undef, undef, undef, 15); should timeout for 15 seconds, so if its hanging indefinitely then it's probably something to do with the $r->process method call. Perhaps the request is timing out? Maybe the async call is being messed up by select causing a halt? Dunno. Make it print for every iteration to see whats going on.
Use warn instead of print so that you can output to stderr and you wont have buffering issues.

hmm, yes, I see, it just sits there until $stat gets returned, which is what it looked like it would do when I saw it, but you know, people on the internet giving perl advice never lie...

So maybe I just need a better understanding of how select() works? Reading the perldoc for it I don't see how this becomes useful in working a timeout value into GHTTP.

TiMBuS
Sep 25, 2007

LOL WUT?

I just installed ghttp to have a look. It seems like it is looping as expected, its just when I add sleep/select into the loop, it runs stupidly slow. I'm guessing the request is being affected by the main process being halted..

Oh and yeah, select is used to select a specific handle to use for standard IO operations. Select is blocking, so it will wait until the filehandle is available or until the specified timeout value is hit. The last parameter to select is a timeout value.

toadee
Aug 16, 2003

North American Turtle Boy Love Association

TiMBuS posted:

I just installed ghttp to have a look. It seems like it is looping as expected, its just when I add sleep/select into the loop, it runs stupidly slow. I'm guessing the request is being affected by the main process being halted..

Oh and yeah, select is used to select a specific handle to use for standard IO operations. Select is blocking, so it will wait until the filehandle is available or until the specified timeout value is hit. The last parameter to select is a timeout value.

Hmm, well then, any suggestions for something very lightweight that will make an HTTP request that I can assign a timeout value to? LWP is way way too big but nothing else I've found has an option for timeout, which seems bizarre since I can think of a lot of reasons why you wouldn't want a script that is in need of a screamingly fast and light HTTP module to sit around waiting for a dead server.

TiMBuS
Sep 25, 2007

LOL WUT?

Well I don't really see why select is needed at all for a timeout..
I'd just do something like:
code:
my $starttime = time;
my $stat; 
while ($stat = $r->process) {
  #You should probably do more than just call 'last' btw.
  last if time > $starttime + 15;
}
Which seems to work just fine for me..

e: Oooh I see now, you didnt know at all that select with an undefined handle acted pretty much exactly like sleep. I should have realized that in the first place (I thought you were trying to use it to wait while you were in your loop, you know to save CPU). I was a bit confused there heh.

But yeah the above code should handle timeouts for you.

TiMBuS fucked around with this message at 12:54 on Nov 8, 2008

toadee
Aug 16, 2003

North American Turtle Boy Love Association

oh! duh! Alright, yes, that does work... thanks!

Voltaire
Sep 20, 2003
The lunatic is in my head...
I'm having difficulty writing a program that determines whether a 7 character input is a character. I am not allowed to use arrays, reverse, split, join; just basic perl functions.

So far I have

code:
use strict;
use warnings;
use diagnostics;

print "Enter in a 7 character item: ";
chomp (my $input = <> );

$input =~ s/\\s//g;
if ($input = [9,8,7,6,5,4,3,2,1,0] == [0,1,2,3,4,5,6,7,8,9])
{
        print "PALINDROME!\n";
}
else
{
        print "NOT A PALINDROME!\n";
}
Obviously I'm missing something here...

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Voltaire posted:

[9,8,7,6,5,4,3,2,1,0] == [0,1,2,3,4,5,6,7,8,9]

This doesn't work; equality for array references just checks if they point to the same array. You need to iterate over the elements.

This seems terribly obtuse for a homework question. Job interview?

Voltaire
Sep 20, 2003
The lunatic is in my head...

Sartak posted:

This doesn't work; equality for array references just checks if they point to the same array. You need to iterate over the elements.

This seems terribly obtuse for a homework question. Job interview?

Yes it is for a job interview. They very specifically said no arrays or use of reverse. Urghhhh....

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Voltaire posted:

7 character input is a character.
You mean palindrome?

Voltaire posted:

if ($input = [9,8,7,6,5,4,3,2,1,0] == [0,1,2,3,4,5,6,7,8,9])
What the hell are you even trying to do there?

Also, they're right. All you need for this is one regexp and some ifs.

Lastly, try reading the PBP sometime, if you're going to program in it. Any coworkers will be grateful for that.

Adbot
ADBOT LOVES YOU

Voltaire
Sep 20, 2003
The lunatic is in my head...

Mithaldu posted:

You mean palindrome?
What the hell are you even trying to do there?

Also, they're right. All you need for this is one regexp and some ifs.

Lastly, try reading the PBP sometime, if you're going to program in it. Any coworkers will be grateful for that.

Yes a palindrome. it can be numbers, letters, spaces.

i have. im not even sure where to start with this one without using split and join.

  • Locked thread