Bompa
Results 1 - 10 of about 194,000 for
PerlHow To Fetch Last Modified Header InformationLOL, just curious if this post will get ranked for it's Title. Even though I've been coding perlfor about 5 years, I didn't know how to get just theHeader information until recently. Probably cuz I had no reason to get it, lol. I mean, I started in perlwriting justperl/cgi's for a small IRC web site, I didn't have need for much elsethan parsing some form data. Over the last year or so, I have found reason, (one if for the pure fun of it), to use more and more perl; namely LWP, (perl's browser imitator).Perl's <>use LWP::Simple> Head function returns five values in a list. Here's my snippet. Tear itapart, add to it, whatever..... #!/usr/bin/ perluse LWP::Simple; print "Content-type: text/html "; $|=1; $url = 'technorati.com'; # USE 'HEAD' TO GET JUST THE HEADERS OF THE URL ($content_type, $document_length, $modified_time, $expires, $server) = head($url); # CONVERT THE EPOCH TIMES, (number of seconds since perk was born , TO A NORMAL DATE FORMAT $lastmod_date = gmtime($modified_time); $expires_date = gmtime($expires); print "Fetching Headers -> $url... <BR> "; print "Last Modified: $lastmod_date GMT <BR> "; print "Content Type: $content_type <BR> "; print "Length: $document_length <BR> "; print "Expires: $expires_date <BR> "; print "Server: $server<P> "; Just upload to cgi-bin, (many hosts no longer require this), change permissions to 755 and point your browser to your URL. Bompa thedarkness
Nice Bomps,
You just helped me solve something thats been bugging me for a while. A question, how do you guys go about implementing your Last-Modified header on your webservers? Cheers, td perkiset
Hey Bomps - in an effort to get even a nominal comfort with reading
PERL, would you mind explaining just a couple pieces of syntax?quote author=Bompa link=topic=97.msg448#msg448 date=1177469193 $|=1; ... an assigment? that looks like "String Pipe Equals 1" to me. And beyond that, I don't see anything later in the code that ...erm... references this or anything later... quote author=Bompa link=topic=97.msg448#msg448 date=1177469193 # USE 'HEAD' TO GET JUST THE HEADERS OF THE URL ($content_type, $document_length, $modified_time, $expires, $server) = head($url); I this like a multi-element array filling function - except that your filling all the variables on the left from the return value(s) of the function on the right? (OW my head hurts from the implications of that if true) Thanks! thedarkness
$|=1; this flushes output quote author=perkiset link=topic=97.msg464#msg464 date=1177479077 quote author=Bompa link=topic=97.msg448#msg448 date=1177469193 # USE 'HEAD' TO GET JUST THE HEADERS OF THE URL ($content_type, $document_length, $modified_time, $expires, $server) = head($url); I this like a multi-element array filling function - except that your filling all the variables on the left from the return value(s) of the function on the right? (OW my head hurts from the implications of that if true) LOL, just like me when I worked this syntax out a few months ago, that is exactly what it is Welcome to the weird and wonderful world of perl.Cheers, td perkiset
Oh FFS... so functions can have multiple return values ... yikes ... is there SOME kind of indication other than just "knowing this" that that is the behavior of a function?
And re the $|1 bit - that has to be a construct of more than just a random choice for <those> characters to represent "clear the buffers..." (oh please tell me it is...) - is that actually 3 different commands combined to create that affect? Bompa
quote author=perkiset link=topic=97.msg474#msg474 date=1177480772 Oh FFS... so functions can have multiple return values ... yikes ... is there SOME kind of indication other than just "knowing this" that that is the behavior of a function? Nope. LOL It's not a normal builtin perlfunction, it's a function of thePerlmodule LWP:Simple.It's sorta like if I use a phpclass that you wrote. Only way I can know what will comeback is to know the class. The only way to know what any particular perlmodule's functions might return is to be familarwith that particular module. FROM THE DOC: --------------------------------------------------------------- head($url) Get document headers. Returns the following 5 values if successful: ($content_type, $document_length, $modified_time, $expires, $server) Returns an empty list if it fails. In scalar context returns TRUE if successful. ---------------------------------------------------------------- Bottom line is that one needs to know that it's returning a list containing five elements. Otherwise one would just be getting a 0 or 1 (in scalar context). quote And re the $|1 bit - that has to be a construct of more than just a random choice for <those> characters to represent "clear the buffers..." (oh please tell me it is...) - is that actually 3 different commands combined to create that affect? $| is a perlspecial variable for 'autoflush'. When set to any non-zero value, the buffers will flush after each print.Bompa dirk
Here are two simple test scripts to show the different effects of the autoflush variable.
#!/usr/bin/ perl# 0 is the default value, so the next line is only for clarification. # Actually it's not necessary. $| = 0; for ( 1 .. 60 ) { print '.'; sleep 1; } exit; You have to wait 60 seconds before you see any output. #!/usr/bin/ perl# Set autoflush $| = 1; for ( 1 .. 60 ) { print '.'; sleep 1; } exit; The write is made after every output command. perkiset
I see... and it looks as though the $ is the variable name prefix and the pipe symbol is to represent output in this case... so the output mechanism is either streaming or flushed at exit... that makes send in a
PERL& Birkenstock kind of way.Thanks! /p perkiset
quote author=Bompa link=topic=97.msg479#msg479 date=1177487651 It's sorta like if I use a phpclass that you wrote. Only way I can know what will comeback is to know the class. Of course this is true, EXCEPT that phpuses an old Cism, single function return values only - so at the very least, if I don't know what's coming back I can trap a single <anything> and see what it is.Additionally, good PHPcoders will throw exceptions if the inbound params are not exactly as expected, so the "good"PHPcoder will endeavor to trap/nethis user into making sure the the params are dialed in. Is there a notion of throwing exceptions inPERL?/p dirk
For example, you could use "die" to throw exceptions:
my $file = 'test.txt'; open my $FILE, '<', $file or die "$!"; If the file is missing you get an error message: "No such file or directory at test.txt line..." BTW, "$!" is another example of a Global Special Variable. We use it to print the last system call error. perkiset
@ exceptions:
Is die() a flat sort of function ie., if you die deep in a subroutine could you trap the die, do <something> and discard the "death" ? quote author=dirk link=topic=97.msg559#msg559 date=1177544292 my $file = 'test.txt'; open my $FILE, '<', $file or die "$!"; In this case, what is the "my" keyword? Is that something referencing scope (global vs local) or something deeper, like threads? Would you mind spelling out the structure of the second line as well? Sheesh I feel like a loser dirk
quote author=perkiset link=topic=97.msg567#msg567 date=1177545715 @ exceptions: Is die() a flat sort of function ie., if you die deep in a subroutine could you trap the die, do <something> and discard the "death" ? Die is only a simple function. Error handling is another issue. quote author=dirk link=topic=97.msg559#msg559 date=1177544292 my $file = 'test.txt'; open my $FILE, '<', $file or die "$!"; quote author=perkiset link=topic=97.msg567#msg567 date=1177545715 In this case, what is the "my" keyword? Is that something referencing scope (global vs local) or something deeper, like threads? If you declare a variable with "my" the variable is only visible in the context where it was declared, for example a subroutine, an "if block" or a "for block". quote author=perkiset link=topic=97.msg567#msg567 date=1177545715 Would you mind spelling out the structure of the second line as well? It's the three argument form of open which was introduced in Perl5.6.open: opens the file '<': the file is opened for read $file: the variable under which the filename is defined or die "$!": error handling, is not necessary when you open a file but it's recommended perkiset
quote author=dirk link=topic=97.msg576#msg576 date=1177547991 If you declare a variable with "my" the variable is only visible in the context where it was declared, for example a subroutine, an "if block" or a "for block". Got it quote author=dirk link=topic=97.msg576#msg576 date=1177547991 open: opens the file '<': the file is opened for read $file: the variable under which the filename is defined or die "$!": error handling, is not necessary when you open a file but it's recommended Again thanks - makes much more sense. I assume then that there are multiple verbs to apply in the first position such as close & such, as well as items like '>' for write and maybe "+" for append or something? appreciate it dirk... Bompa
Dirk is way ahead of me with
perl.I use as many parenthesis as possible and hardly ever leave things to their default value; that's mostly cuz I can't *remember* their default values. LOL To open a file for read only: open(IN, '< myfile.txt'); or using a variable for the filename: $filename = 'myfile.txt'; open(IN, "< $filename"); For writing to a file (writeover), just reverse the direction of the angle bracket: open(OUT, '>myfile.txt'); For appending use two angle brackets: open(OUT, '>>myfile.txt'); The IN and OUT are filehandles and could be any word/name one desires; the all-caps is just a convention for easier reading. The > and the >> include the "create if does not exist" capacity. Lots of code I've seen uses FH for the filehandle no matter if reading or writing; I find that difficult to read. The only other verb that I use is 'close' to close the file. close IN; close OUT; close FH; Reading a file is usually done by enclosing the filehandle with perls line reading operator; angle brackets.@array = <IN>; Assuming no previous changes to the default line terminator, ( ), the above line would put each line of the file into an element of @array and each element of @array would end with a . For years I used the chomp function to remove thos line endings, (during a loop for example). Recently I learned this gem while reading the file in:open(IN, '< myfile.txt'); chomp(@array = <IN>); close IN; I think there is a 'read' command, but I think it's just used for reading binary files and one would need to desginate starting byte and all that shit. ah well, back to reading a zillion post I dont understand Bompa thedarkness
Thanks for this Bomps, realy good for us
perlphillistinesCheers, td perkiset
quote author=Bompa link=topic=97.msg588#msg588 date=1177555639 ah well, back to reading a zillion post I dont understand : Yikes what you PERLboys deal with. And you're confused about... what exactly in other languages?Frigging dope I am around here... Bompa
http://www.google.com/search?hl=en&q=
Perl+How+To+Fetch+Last+Modified+Header+Information&btnG=Google+SearchLOL nutballs
um ok. so, how that happen. i need that for "other" projects. Bompa
quote author=nutballs link=topic=97.msg1020#msg1020 date=1178554410 um ok. so, how that happen. i need that for "other" projects. Well, it just happened by itself. Obviously Google is crawling this site. I think it was #1 the other day, but I see now it's down somewhere around 30 or 40. Only 250,000 serps so it should stay somewhere near the top. My point was that if we put some thought into the Topics making them like a real search query, some of them will bring traffic naturally. ya think? Bompa |
Thread Categories
Best of The Cache Home | ||
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads