|
It's also a handy idiom if you want to merge two hashes:code:
|
![]() |
|
![]()
|
# ? May 21, 2024 18:35 |
|
atomicstack posted:It's also a handy idiom if you want to merge two hashes: gently caress! I could've used this many times before!
|
![]() |
|
Welp, I just got a new job doing full time perl development, so expect more embarrassing code snippets from me soon!
|
![]() |
|
Congratulations. What city are you in?
|
![]() |
|
Triple Tech posted:Congratulations. What city are you in? I'm in Chicago. I've actually been seeing quite a few (3-4 a month) Chicago perl jobs showing up jobs.perl.org.
|
![]() |
|
Found an interesting bug today at work. It would have slipped under my nose if other parts of the code had looser validation. What's the bug?code:
|
![]() |
|
Sartak posted:Found an interesting bug today at work. It would have slipped under my nose if other parts of the code had looser validation. What's the bug? if $is_special{$type} is true then $1 was preserved from the most recent regex match instead of being undefined or something, right?
|
![]() |
|
Does the bitwise-and bind too tightly causing you to regexp bind on a "1"??
|
![]() |
|
Triple Tech posted:Does the bitwise-and bind too tightly causing you to regexp bind on a "1"?? =~ binds tighter than &&. See perlop(1). And && is not a bitwise AND. ![]()
|
![]() |
|
There Will Be Penalty posted:And && is not a bitwise AND. I like saying bitwise because it makes me feel smarter. Clearly it doesn't make me look smarter. I had the page open as I wrote that post and I still got it wrong!
|
![]() |
|
There Will Be Penalty posted:if $is_special{$type} is true then $1 was preserved from the most recent regex match instead of being undefined or something, right? That's right. The value of $1 was "1.1". Thankfully the called code threw a clear exception so I had no trouble zeroing in on this region of code. It could have easily been debugging hell if no exception were thrown or the value of $1 were a valid type. I wouldn't have given the code a second glance if I were any less sure that was the location of the bug. Here's how I rewrote it: code:
Filburt Shellbach fucked around with this message at 02:03 on Feb 3, 2009 |
![]() |
|
Ugh, it's so wordy... It'd either preprocess the type, at the risk of removing the ability to parse "strings" as "strings" (as opposed to "string"). Also, instead of the sub, use a do block and remove the "singular type" concept in place of just modifying type directly. Edit: 3) Farm out the parsing to a subroutine so that the complexity isn't subordinate to a larger workflow. Triple Tech fucked around with this message at 02:17 on Feb 3, 2009 |
![]() |
|
Sartak posted:That isn't as clear as I'd like it to be. Anyone want to take a stab at rewriting it better? How's this look? code:
Changing the last bit to an else is just cosmetic obviously, but I think it looks a bit better having the two possible returns parallel each other. That being said, I've done plenty of cases with "if (foo) { return blah; } return blahblah;", so either/or. Edit: Crap. Nevermind. Just realized the problem with it. Edit 2: Ok, fixed it by adding in $original_type. Erasmus Darwin fucked around with this message at 03:48 on Feb 3, 2009 |
![]() |
|
I have a long line with thousands of characters. I want to find each occurrence of the string abc(\d+) and do something with it. For example, I may have: abc123 blah blah abc456 blah blah abc789 blah What's the best way to attempt this? Futile attempt: code:
|
![]() |
|
Can you elaborate on what exactly you want to do to it?
|
![]() |
|
mister_gosh posted:I have a long line with thousands of characters. I want to find each occurrence of the string abc(\d+) and do something with it. code:
code:
ShoulderDaemon fucked around with this message at 01:47 on Feb 17, 2009 |
![]() |
|
^^^^ Thanks!!!! vvvv
mister_gosh fucked around with this message at 02:13 on Feb 17, 2009 |
![]() |
|
code:
Filburt Shellbach fucked around with this message at 02:11 on Feb 17, 2009 |
![]() |
|
I'm getting a file like thiscode:
code:
|
![]() |
|
Aggregate the entries into a hash, keyed by ID. The value of each key is a stack that you push new values on to. Then you render the stacks at the end with a list seperator.
|
![]() |
|
perlreftut will tell you exactly how to do this
|
![]() |
|
Since the file comes out sorted, is there a way to do it with out a hash?
|
![]() |
|
You can iterate over the sorted keys of the hash (and sort each list of entries before printing).
|
![]() |
|
Xae posted:Since the file comes out sorted, is there a way to do it with out a hash? Yes. Just keep track of what the number is for the line you're printed so you can print a new prefix or just a comma as appropriate. Something like this (untested): code:
|
![]() |
|
I have a (dumb?) question about fork I want to do code:
If it does wait, how do I run a whole bunch of children in parallel? I don't quite understand open as a pipe, even after reading perlipc.
|
![]() |
|
fork() will not wait for the child to complete; for that you would want system. If you fork child processes, you should also wait for them. Note that forking an arbitrary number of child processes at once is not a good idea. Opening a command as a pipe essentially fork/execs it with either its STDOUT piped to the opened handle or the opened handle piped to its STDIN; in fact, you can achieve the same effect (and more) by doing exactly that.
|
![]() |
|
rjmccall posted:fork() will not wait for the child to complete; for that you would want system. If you fork child processes, you should also wait for them. Note that forking an arbitrary number of child processes at once is not a good idea. Thanks. Am I heading for trouble if I just try that loop, or do I somehow want to cap the number of child processes I run at a time?
|
![]() |
|
Shoes posted:Am I heading for trouble if I just try that loop, or do I somehow want to cap the number of child processes I run at a time? In practice, you're probably okay if you're not forking off more than, say, ten or twenty processes. If this might be significantly higher than that, you definitely need to ration your process use.
|
![]() |
|
Shoes posted:
There's a bug here. You want == rather than =. With =, you'll be assigning 0 to $pid and then your conditional will always evaluate to false. Also, make sure you've got an exit statement at the end of the code being executed by your child process, otherwise both the parent and the child process will continue forking new processes.
|
![]() |
|
rjmccall posted:In practice, you're probably okay if you're not forking off more than, say, ten or twenty processes. If this might be significantly higher than that, you definitely need to ration your process use. I'm guaranteed that this is going to be a list of a few hundred items. Is there a way to call 10 or 20 at a time?
|
![]() |
|
Shoes posted:I'm guaranteed that this is going to be a list of a few hundred items. Is there a way to call 10 or 20 at a time? http://forums.somethingawful.com/showthread.php?threadid=2961749#post349626854
|
![]() |
|
Erasmus Darwin posted:http://forums.somethingawful.com/showthread.php?threadid=2961749#post349626854 Thanks a bunch.
|
![]() |
|
Shoes posted:I'm guaranteed that this is going to be a list of a few hundred items. Is there a way to call 10 or 20 at a time? I would suggest Parallel::ForkManager. I've used it at my job a few times because it's dead simple and Just Works.
|
![]() |
|
I'm currently working on a cross-platform gui flashcard application that i'm thinking about adding to CPAN. Flashcard sets would be stored as individual modules. However i'm also thinking about making it possible to link the items in the flashcard sets to audio bits, which would optimally be present in the form of audiobanks available as modules on cpan. As i'm doing this with wxPerl i have one restriction though: The audio bits need to be plain wav files and actually present as files; i can't simply feed them to Wx::Sound as a variable. Right now i'm pondering though exactly how to accomplish this. The ways i can think of would be as follows: - Have the audiobank module be a hash which contains serialized (Storable, FreezeThaw or something like that) versions of the audio files. When the sound files are required they are unserialized, dumped with File::Temp and the filename forwarded. Problem: Not very elegant. - Simply add the .wavs as files to the module package and figure out the path to the files, then forward that. Problem: I have no idea if there is a module for that and i don't want to do it the brute way. Do any of you have better ideas as to how to accomplish this or other comments? Maybe i'm thinking about it in the wrong way completely?
|
![]() |
|
File::ShareDir and the like are useful for installing arbitrary data files in a place that your module can find them.
|
![]() |
|
Oh, that is excellent, thank you. ![]()
|
![]() |
|
So I want to automate typing in passwords for a ton of servers. All the servers have the same password so I can do something like ask for it once and use that one password for each system call. There are two different commands. One is a stop and the other is a start. The start requires three different passwords for different parts of the system. I have researched and crossed out the following solutions 1) unix expect/send - This would have been perfect, but alas it's not on the system 2) installing a perl module using cpan; more specifically the one that simulates expect. The perl modules installed are the very basic and just like #1 getting something installed on test and production is horrible Now there are two ways I think I can do this 1) installing Expect module in a private library instead of having to call CPAN to install it. I think 2) Holy poo poo, crazy town. This one will be very tricky, but with the lack of resources, I just came up with it after looking through the commands available to me in unix. I will be forking off spawned instances of the commands and piping the command into a tee file. I will examine the output file and wait for the time it asks for a password. Now that I write about this, this is getting really convulated and I think #1 would be best. So I just want some final thoughts for a really easy solution that I might be overlooking. I can tell you it's a SunOS system, but I cannot change for the presence or non-presence of a command.
|
![]() |
|
I would bundle Expect.pm and IO::Pty/IO::Tty with your script. Just stick them into lib/ and have your script use lib 'lib';
|
![]() |
|
Xae posted:I'm getting a file like this code:
|
![]() |
|
![]()
|
# ? May 21, 2024 18:35 |
|
I made a private lib for the above problem i have it in (my home directory)/scripts/perl_lib The script is located in scripts I put in use code:
The library works, but one of the two IOs say that it is missing something called Constant.pm code:
Sartak: If you want, can I msg you and holy drat you are Sartak who I watched play nethack. Plus I saw the article you submitted on reddit today. Let me know if you ever do a new LP so I can comment heh.
|
![]() |