|
The same sort of people who frown own sort {$a+=$b} @foo
|
# ? Nov 23, 2011 23:29 |
|
|
# ? May 17, 2024 13:35 |
|
tef posted:The same sort of people who frown own sort {$a+=$b} @foo Maybe I'm misunderstanding the construct, but wouldn't that always return that $a is larger than $b, unless $b was a sufficiently large negative number to return 0 or less? I get the feeling there's a side effect I'm missing.
|
# ? Nov 23, 2011 23:39 |
|
OriginalPseudonym posted:
Yeah, I don't understand what it's doing: code:
code:
In summary, I have no idea what this means or why you'd do it.
|
# ? Nov 24, 2011 00:11 |
|
OriginalPseudonym posted:
sort in a void context is a no-op code:
|
# ? Nov 24, 2011 02:22 |
|
tef posted:sort in a void context is a no-op Oh, void context. I was expecting something like this: code:
|
# ? Nov 24, 2011 02:38 |
|
I tackled Project Euler, Problem 13: http://projecteuler.net/problem=13 Can someone give me advice on how I could have handled this more efficiently? Bear in mind, I've been messing with Perl for about a week now. code:
|
# ? Nov 27, 2011 22:37 |
|
Hughmoris posted:I tackled Project Euler, Problem 13: http://projecteuler.net/problem=13 Here's how I'd do it, with some notes below. code:
Three argument open Filehandle as a scalar, not as a bareword Always check return value of file operations for success (or 'use autodie') Assign line to scalar on loop line Don't need to chomp in this case, newlines'll implicitly get stripped during the string->number conversion Use Math::BigInt so you don't get the scientific exponentation conversion Use say so you get newlines at the end of your output Treat the number as a string and substring it
|
# ? Nov 27, 2011 22:52 |
|
Hughmoris posted:Bear in mind, I've been messing with Perl for about a week now. Also, everything ^^^^ said. Look at every single sentence, if you don't get it, research and/or ask. But make no mistake, these things are important.
|
# ? Nov 27, 2011 22:57 |
|
Roseo posted:Here's how I'd do it, with some notes below. Thanks for taking the time to type all that out, I'll definitely take a look.
|
# ? Nov 27, 2011 23:05 |
|
Can you expand on "don't pre-declare variables"? Ever? Or just in this instance? For variables used a bunch of times throughout a function, I find it better to pre-declare them.
|
# ? Nov 28, 2011 12:14 |
|
Ninja Rope posted:Can you expand on "don't pre-declare variables"? Ever? Or just in this instance? He meant don't declare them all at the beginning of the script. It's more Perlish to declare them right before you use them, and definitely declare them in the smallest scope you need them. Consider: code:
|
# ? Nov 28, 2011 12:24 |
|
Ninja Rope posted:Can you expand on "don't pre-declare variables"? Ever? Or just in this instance?
|
# ? Nov 28, 2011 12:36 |
|
Since the OP is over 4 years old, I have to ask. Is 'Learning Perl' still the go to guide for inexperienced programmers trying to learn the language? I've peeked at 'Modern Perl' but it looks a little sparse for a rookie such as myself.
|
# ? Nov 30, 2011 05:32 |
|
Hughmoris posted:Since the OP is over 4 years old, I have to ask. Is 'Learning Perl' still the go to guide for inexperienced programmers trying to learn the language? I've peeked at 'Modern Perl' but it looks a little sparse for a rookie such as myself. That's way too old. Try looking on http://perl-tutorial.org
|
# ? Nov 30, 2011 10:00 |
|
Mithaldu posted:That's way too old. Try looking on http://perl-tutorial.org If you're talking about the O'Reilly book, the 6th edition just came out, and that covers 5.14, so it's at least up-to-date, though I haven't read it, so I can't vouch for whether it's good or not (I thought the older edition was okay).
|
# ? Nov 30, 2011 12:49 |
|
Sizzler Manager posted:If you're talking about the O'Reilly book, the 6th edition just came out, and that covers 5.14, so it's at least up-to-date, though I haven't read it, so I can't vouch for whether it's good or not (I thought the older edition was okay). I was talking about the "Learning Perl" linked in the OP, which was from 2001. Also, simple test to see if any learning resource is any good: Does the first actual Perl code example contain strict and warnings? Y/N
|
# ? Nov 30, 2011 13:00 |
|
Hughmoris posted:Since the OP is over 4 years old, I have to ask. Is 'Learning Perl' still the go to guide for inexperienced programmers trying to learn the language? I've peeked at 'Modern Perl' but it looks a little sparse for a rookie such as myself. code:
|
# ? Nov 30, 2011 13:09 |
|
Mithaldu posted:I was talking about the "Learning Perl" linked in the OP, which was from 2001. That's something I've been meaning to ask - in production code, I always use strict and use warnings, and I don't use bareword filehandles, I use the three-argument open, etc., but in short scripts (5-20 lines or so) that I might use one or more times to do something for myself, I generally don't bother with any of those things, because, for instance, it's convenient to just use $foo{$_} and create a hash without having to declare it first. Other than the obvious laziness of this, is there any argument against it besides "you might get into the habit of using sloppy code in important programs," which hasn't happened? I mean, you don't use strict and warnings on one-liners, and short scripts seem to me to just be long one-liners.
|
# ? Nov 30, 2011 16:00 |
|
Sizzler Manager posted:I mean, you don't use strict and warnings on one-liners, and short scripts seem to me to just be long one-liners.
|
# ? Nov 30, 2011 16:39 |
|
Quick question about using sigtrap. I want to change the subroutine used by sigtrap based on where in the program it currently is when it's killed. Or more specifically, I want to pass different arguments based on where the program is - My subroutine adapts based on the number of arguments specified, and uses it to gracefully close down connections, but if I start adding arguments to my sigtrap at the very start of my program, strict is going to throw a fit because the variables specified haven't been defined yet. Is there a way to redefine sigtrap's parameters halfway through the script? Is it as simple as running the use sigtrap line again? EDIT:- I found a better way. Rather than using the sigtrap module, I just redefined $SIG{'INT'} and $SIG{'QUIT'}: code:
Rohaq fucked around with this message at 17:46 on Dec 6, 2011 |
# ? Dec 6, 2011 16:53 |
|
I have a cgi script that runs and displays a button that links to a text file. When I click on the button, it loads in the browser, displaying the txt data. Is there an easy way to make the button download the text file, meaning, when the button is clicked, it goes straight to a "Save As" prompt? I wasn't sure if there was code to do this on the cgi script itself. I've googled and read that I'd have to do something to the header, but I wasn't quite sure how to go about that.
|
# ? Dec 8, 2011 16:41 |
|
stoops posted:I have a cgi script that runs and displays a button that links to a text file. When I click on the button, it loads in the browser, displaying the txt data. You can, but it's finicky. The long answer is that the server identifies the ".txt" extension as a specific MIME type ("text/plain", in this case). That MIME type is sent to the browser, which says "I use this associated program for this MIME type" and reacts accordingly. To that end, you can modify the MIME type that .txt files identify with (here's a list to choose from), but there isn't necessarily one catchall MIME type that every machine will "open" properly. For example, you could flip it to "application/octet-stream", but then the end browser may attempt to run the text file as an executable after it finishes downloading. If it's something you want to give a shot, you can use CGI::header to change the header of the current document being returned to the browser. The other option would be to modify Apache's config or .htaccess files to identify every .txt file in a certain directory as something that's not text/plain.
|
# ? Dec 8, 2011 18:15 |
|
I have a general question for you perl gurus. I'm decent in perl right now, but recently got a new job. One of the things I deal with extremely regularly is abuse. I work at a web hosting company and people are always either uploading nefarious files, or getting their sites compromised. Typically there is some base64 encoded strings in the php that will do things like kick off a bunch of UDP calls, or what have you. It's pretty easy to find abuse by catting the access logs at any given time, look for all php files then grep through each one looking for php's base64 decode function. However, recently we're running into abusive sites where the obfuscated or encoded functions are put outside of the <?php tags, or it's just in HTML. It looks like these are sitting, waiting to be curled from some other location we have control of in order to do *something* (usually more udp noise). The problem with this is, this makes it much harder to find. I'm trying to write a perl script that can grep through files and find a way to determine if a string of characters is base64 encoded. Right now my ideas are: - Open a file for reading in Perl - Count the line length of each line - If the line is greater then 100 characters count the occurrence of each character - If the occurrence of certain characters like X,Z,V account for a higher then average percent of characters within the string, then flag it for review. Anyone else have better ideas? This seems like it would be an incredibly slow job for Perl...especially since we serve content for about 2 million accounts.
|
# ? Dec 8, 2011 18:33 |
|
OriginalPseudonym posted:You can, but it's finicky. Or you can try sending a Content-Disposition header.
|
# ? Dec 9, 2011 04:03 |
|
Winkle-Daddy posted:I have a general question for you perl gurus. I'm decent in perl right now, but recently got a new job. One of the things I deal with extremely regularly is abuse. I work at a web hosting company and people are always either uploading nefarious files, or getting their sites compromised. Typically there is some base64 encoded strings in the php that will do things like kick off a bunch of UDP calls, or what have you. It's pretty easy to find abuse by catting the access logs at any given time, look for all php files then grep through each one looking for php's base64 decode function. Quite how fast it'll be, I have no idea. You could do an initial run and then set something up to scan files as they're uploaded though. It wouldn't help with dynamic pages pulling data from an external source though, but that doesn't seem to be something that you're checking for anyway.
|
# ? Dec 9, 2011 18:12 |
|
Winkle-Daddy posted:Anyone else have better ideas? This seems like it would be an incredibly slow job for Perl...especially since we serve content for about 2 million accounts. keep backups, run a (trained) spam filter over the diffs more seriously, it looks like you are trying to write a virus scanner for php.
|
# ? Dec 9, 2011 18:23 |
|
Rohaq posted:You can use regex to determine if something is base64 encoded, I suppose. Someone else seems to have solved that here: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data Sweet, this should work and make my life easier! Thanks man! tef posted:more seriously, it looks like you are trying to write a virus scanner for php. Eh, more like a way to determine the probability that certain strings are base64 encoded without looking through all of the files I'm searching manually. I don't think the solution that Rohaq wrote will be too hard because I won't be actively scanning every user directory, I'll probably just tail the last 10,000 or so lines of the apache logs when monitoring picks up unusual traffic.
|
# ? Dec 9, 2011 23:47 |
|
Winkle-Daddy posted:Eh, more like a way to determine the probability that certain strings are base64 encoded without looking through all of the files I'm searching manually. Which you are doing for the purposes of finding malicious code.
|
# ? Dec 10, 2011 12:23 |
|
Winkle-Daddy posted:Sweet, this should work and make my life easier! Thanks man! You should probably know that words like "you", "should", "probably", "know" and "that" are valid base64 strings, as is the empty string, so you'll want to set a minimum length. Either way this is technically a purely regular regex, so it should run in linear time. It won't, but that's Perl's problem.
|
# ? Dec 10, 2011 13:13 |
|
So I need to execute a script from within itself, and check to see if it exited with an error or not. I'm currently using backticks, since that waits for the script to finish before continuing the parent script, and apparently can capture the output, however it seems to grab the STDOUT output, rather than the exit code. Does anybody know a method where I can just capture an exit error code (as in on death, not STDERR output, since it's likely I'll have nonfatal warnings in there which don't necessarily mean that the script failed) from a script being run within another script, so I can see if it completed successfully?
|
# ? Dec 12, 2011 15:29 |
|
http://perldoc.perl.org/functions/system.htmlperldoc posted:The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below). See also exec. This is not what you want to use to capture the output from a command; for that you should use merely backticks or qx//, as described in `STRING` in perlop. Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).
|
# ? Dec 12, 2011 15:36 |
|
You can also continue to use backticks, then inspect the value of $?, which is populated with the exit word when the child process finishes.
|
# ? Dec 12, 2011 16:54 |
|
Anaconda Rifle posted:http://perldoc.perl.org/functions/system.html
|
# ? Dec 12, 2011 19:27 |
|
Ok, I need someone to point me in the right direction. I know how to search a string or file for a word and print out that word. What I want to do now is search a file for a word, and if it finds it, print out the entire sentence that contains that word. Whats a good way to approach this? *For example, the text file is a short story and I want to search for a character's name, and print out every sentence that has the name. Hughmoris fucked around with this message at 01:08 on Dec 13, 2011 |
# ? Dec 13, 2011 01:06 |
|
Hughmoris posted:Ok, I need someone to point me in the right direction. I know how to search a string or file for a word and print out that word. What I want to do now is search a file for a word, and if it finds it, print out the entire sentence that contains that word. Whats a good way to approach this? code:
Ursine Catastrophe fucked around with this message at 01:41 on Dec 13, 2011 |
# ? Dec 13, 2011 01:37 |
|
OriginalPseudonym posted:
If you want a very quick fix, you should use CPAN. code:
|
# ? Dec 16, 2011 05:06 |
|
Be careful using for(<>). This pre-loads the file into memory. If the file is huge, use while(<>).
|
# ? Dec 16, 2011 13:38 |
|
Quick question, I'm using while(<>) to iterate through a file line by line at the moment, is there a quick way to remove the line being processed from the file at the end of the loop, without messing up the while loop? I'd like to process a line, then remove it from the file after it's done.
|
# ? Dec 16, 2011 14:55 |
|
Rohaq posted:Quick question, I'm using while(<>) to iterate through a file line by line at the moment, is there a quick way to remove the line being processed from the file at the end of the loop, without messing up the while loop? I'd like to process a line, then remove it from the file after it's done. Tie::File, perhaps?
|
# ? Dec 16, 2011 15:52 |
|
|
# ? May 17, 2024 13:35 |
|
Anaconda Rifle posted:Tie::File, perhaps?
|
# ? Dec 16, 2011 16:58 |