CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
last | perl

 



princepawn
Novice

Sep 26, 2000, 6:07 AM

Post #1 of 3 (2031 views)
last | perl Can't Post

The "Wit from the Wall" quote had a regular expression that I found confusing. Could someone explain what this code segment is supposed to do:  

"last|perl -pe '$_ x=/(..:..)...(.*)/&&"'$1'"ge$1&&"'$1'"lt$2'

-- Larry Wall in <1991Apr29.072206.5621@jpl-devvax.jpl.nasa.gov>


japhy
Enthusiast / Moderator

Sep 26, 2000, 6:43 AM

Post #2 of 3 (2031 views)
Re: last &#0124; perl [In reply to] Can't Post

The 'last' Unix program returns information about when users logged on, and when they logged off. Here's some sample output:

chuck pts/2 adsl-63-194-86-4 Sat May 20 18:47 - 18:53 (00:06)
elongftp ftp 216-164-194-69.s Sat May 20 13:47 - 13:47 (00:00)
chuck pts/4 adsl-63-194-86-4 Sat May 20 13:30 - 18:47 (05:16)
chuck pts/4 adsl-63-194-86-4 Sat May 20 13:26 - 13:30 (00:03)


Now, the Perl program gets, as input, the output from the 'last' command. Then, it does the following to each line of input:

1. does the input match /(..:..)...(.*)/ ?
2. if so, is the SHELL variable $1 greater than or equal to the Perl's regex variable $1?
3. if so, is the SHELL variable $1 less than the Perl's regex variable $2?

The result of these three conditions is either 1 or 0. If it's 1, then the code simply state: $_ x= 1 (which means $_ = $_ x 1). The 'x' operator is the repetition operator -- 'foo' x 1 is 'foo', and 'foo' x 3 is 'foofoofoo'.

If it's 0, then the code is: $_ x= 0 (which is basically like saying $_ = '').

The real trick to it is that this is executed in sh (or some variant thereof). The part of the code that reads "'$1'" is doing the following: the " is a " in the code of Perl to run; the ' is a single quote CLOSING THE SINGLE QUOTE that started the Perl program; $1 is the shell variable $1 (the first argument to the program); ' RESUMES the string being sent to Perl as code; " is a double-quote in the Perl program.

I hope you understand this. It's damn wicked (and smart). It finds out who's been logged on DURING a specific time.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



princepawn
Novice

Sep 26, 2000, 9:24 AM

Post #3 of 3 (2031 views)
Re: last &#0124; perl [In reply to] Can't Post

Most of it makes more sense now. That was an excellent explanation.

I have printed out your discussion and will read it on the ferry in the next day or so.

Thanks a lot.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives