Xref: utzoo comp.unix.questions:31461 comp.lang.perl:5370 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!zaphod.mps.ohio-state.edu!mips!pacbell.com!ucsd!ucrmath!rearl From: rearl@watnxt3.ucr.edu (Robert Earl) Newsgroups: comp.unix.questions,comp.lang.perl Subject: Re: Wanting to convert awk to perl Message-ID: Date: 19 May 91 10:59:43 GMT References: <1991May19.064619.2493@colorado.edu> Sender: news@ucrmath.ucr.edu Followup-To: comp.unix.questions Organization: (EVIL!) Lines: 117 In-reply-to: frechett@spot.Colorado.EDU's message of 19 May 91 06:46:19 GMT In article <1991May19.064619.2493@colorado.edu> frechett@spot.Colorado.EDU (-=Runaway Daemon=-) writes: | So I wrote a script that is in my .forward. It grabs username from the | From: line of incoming mail then prints out | MAIL from username wherever the cursor currently is, and then the cursor | promptly returns to the M in MAIL. This of course means that it doesn't | mess up my screen at all. Just recently I mangaged to get the csh script | and the awk scripts all into one csh script. I like it in general, but | I feel that it could be done faster in perl. I can get the various | awk routines into perl easily enough but I get stuck trying to emulate | other stuff.. I will include my script here and put current comments in | double ##. | | #!/bin/csh | # informe Written by Ian Frechette frechett@spot.coloraod.edu | | # set username to the name of the person who this is to output to... | set username = frechett | # find Username's ttys | set ttys = (`find /dev -user $username -print`) | ## Figuring out how to do find in perl is puzzling me... and I can't even | ## get a system command into a varible that perl can recognize. You'd use open("find ...|") and read from the pipe filehandle, but (not surprisingly) it's not necessary in perl. :-) | # Extract address from incoming message. | ## first awk routine.. The problems that I had with awk here, was that there is | ## no way to pass a variable out of awk.. Thus the set address = `` nonsense. | ## I would like to remedy that. | set address = `awk '/^From:/ { test = 0 ;for(i = 1 ; i <= NF ; ++ i) { if ($i ~ /@/) { if ($i ~ /^ /dev/null Woah. That's a good one, wait until you see the perl version of it. | # Make sure no errors are returned or mail will bounce. | exit(0) | I would like to know if any perl wizards out there would be interested in | giving me some hints on what to do or demonstrating what needs to be | changed... Any help at all would be appreciated, and comments regarding the | script itself I might find interesting.. Thanks.. Here it is in Perl, maybe someone else will improve on this... #!/usr/bin/perl # # informe -- by Ian Frechette, # perl translation by Robert Earl rearl@watnxt3.ucr.edu # $username = 'frechett'; # Get numerical userid for the stat() $uid = (getpwnam($username))[2]; # this warn should be die, but this is in a .forward, so... opendir(DEV, "/dev") || warn "opendir failed: $!\n"; # slurp in all the entries in /dev and return those that match our uid @ttys = grep((stat("/dev/$_"))[4] == $uid, (readdir(DEV))); closedir(DEV); while (<>) { # get rid of newline chop; # look for "From:" followed by zero or more spaces, followed by either: # 1: with <> brackets being optional, or # 2: the first field not containing a space. s/^From: *(]*@[^>]*)>?|([^ ]+))/$address = ($2 || $3)/e; } $msg = "MAIL from $address"; # easy, eh? $msg .= "\b" x length $msg; # A loop instead of using 'tee' for $tty (@ttys) { open(TTY, ">/dev/$tty") || warn "Can't open $tty: $!\n"; print TTY $msg; close TTY; } exit 0; -- ______________________________________________________________________ \ robert earl / "Love is a many splintered thing" rearl@watnxt3.ucr.edu \ --Sisters of Mercy rearl@gnu.ai.mit.edu /