Bompa

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


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 ";
$|=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 Applause, 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  Applause 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

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

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

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

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  Applause

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

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

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

 .  Applause

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

perl

 s 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

learn

 ed 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 

  Applause


Bompa

thedarkness

Thanks for this Bomps, realy good for us

perl

  phillistines  Applause

Cheers,
td

perkiset

quote author=Bompa link=topic=97.msg588#msg588 date=1177555639

ah well, back to reading a zillion post I dont understand 


:Applause

Yikes what you

PERL

  boys deal with. And you're confused about... what exactly in other languages?  Applause

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+Search



LOL

nutballs

Applause

um ok. so, how that happen. i need that for "other" projects.

Bompa

quote author=nutballs link=topic=97.msg1020#msg1020 date=1178554410

Applause

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


Perkiset's Place Home   Politics @ Perkiset's