The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:31:30 AM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Perl How To Fetch Last Modified Header Information  (Read 9774 times)
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« on: April 24, 2007, 07:46:33 PM »

Results 1 - 10 of about 194,000 for Perl How To Fetch Last Modified Header Information


LOL, just curious if this post will get ranked for it's Title.   Grin


Even though I've been coding perl for about 5 years, I didn't know how to get just the
Header information until recently.  Probably cuz I had no reason to get it, lol.  I mean, I
started in perl writing just perl/cgi's for a small IRC web site, I didn't have need for much else
than 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 it
apart, add to it, whatever.....




#!/usr/bin/perl
use LWP::Simple;
print "Content-type: text/html\n\n";
$|=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 Wink, TO A NORMAL DATE FORMAT
$lastmod_date = gmtime($modified_time);
$expires_date = gmtime($expires);

print "Fetching Headers -> $url... <BR>\n";
print "Last Modified: $lastmod_date GMT <BR>\n";
print "Content Type: $content_type <BR>\n";
print "Length: $document_length <BR>\n";
print "Expires: $expires_date <BR>\n";
print "Server: $server<P>\n\n";



Just upload to cgi-bin, (many hosts no longer require this), change permissions to 755 and
point your browser to your URL.


Bompa


« Last Edit: April 24, 2007, 07:49:27 PM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #1 on: April 24, 2007, 08:18:17 PM »

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
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: April 24, 2007, 10:31:17 PM »

Hey Bomps - in an effort to get even a nominal comfort with reading PERL, would you mind explaining just a couple pieces of syntax?

$|=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...

#  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!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #3 on: April 24, 2007, 10:44:48 PM »


$|=1; this flushes output


#  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  ROFLMAO Welcome to the weird and wonderful world of perl.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: April 24, 2007, 10:59:32 PM »

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?

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #5 on: April 25, 2007, 12:54:11 AM »

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 perl function, it's a function of the Perl module LWP:Simple.

It's sorta like if I use a php class that you wrote.  Only way I can know what will come
back is to know the class.

The only way to know what any particular perl module's functions might return is to be familar
with 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 perl special variable for 'autoflush'.  When set to any non-zero value, the buffers will flush after each print.


Bompa

« Last Edit: April 25, 2007, 01:14:39 AM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #6 on: April 25, 2007, 06:44:51 AM »

Here are two simple test scripts to show the different effects of the autoflush variable.

Code:
#!/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.

Code:
#!/usr/bin/perl

# Set autoflush

$| = 1;

for ( 1 .. 60 ) {
    print '.';
    sleep 1;
}

exit;

The write is made after every output command.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: April 25, 2007, 11:16:26 AM »

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
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: April 25, 2007, 11:20:16 AM »

It's sorta like if I use a php class that you wrote.  Only way I can know what will come
back is to know the class.

Of course this is true, EXCEPT that php uses 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 PHP coders will throw exceptions if the inbound params are not exactly as expected, so the "good" PHP coder will endeavor to trap/net his user into making sure the the params are dialed in. Is there a notion of throwing exceptions in PERL?

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #9 on: April 25, 2007, 04:38:12 PM »

For example, you could use "die" to throw exceptions:

Code:
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.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: April 25, 2007, 05:01:55 PM »

@ 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" ?

Code:
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  ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #11 on: April 25, 2007, 05:39:51 PM »

@ 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.

Code:
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?

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".

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 Perl 5.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
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #12 on: April 25, 2007, 05:44:57 PM »

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

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...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #13 on: April 25, 2007, 07:47:19 PM »

Dirk is way ahead of me with perl.  Cheesy

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, (\n), the above line would put each
line of the file into an element of @array and each element of @array would end with a \n.

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 

  ROFLMAO


Bompa
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #14 on: April 25, 2007, 07:56:29 PM »

Thanks for this Bomps, realy good for us perl phillistines  Grin

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!