[6527] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 152 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 21 02:11:35 1997

Date: Thu, 20 Mar 97 23:00:21 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 20 Mar 1997     Volume: 8 Number: 152

Today's topics:
     Re: /RE/ for Dummies - It is even hard for experts! <dbull@digex.net>
     An interesting socket puzzle <bermingh@concentric.net>
     Re: any non-forking server template code? (Rahul Dhesi)
     Re: Can a Perl script interact with Java applet? (Stinger)
     Re: Checking a '/' chr. is not present <web435@ix.netcom.com>
     DB_File.pm? (Jonathan R. Seagrave)
     Re: Elegant way to strip spaces off the ^ and $ of a va (Jeffrey)
     Help reading pipes "unbuffered"? (Paul D. Smith)
     Re: Help with Pattern Matching (Niksun)
     How Do I Send KeyStrokes From Win32 Perl To a Win16 App <mikeo@gsd.state.nm.us>
     interaction between local(), my(), and undef() (Patrick Duff)
     Non-Blocking sockets & status (Steve Hindle)
     Re: Pattern matching on stream <jay@rgrs.com>
     printing Opened File (Jeremy Coulter)
     regex for UNIX usernames needed! <signal@shreve.net>
     Re: regex in perl4 (David  Bell)
     running the first perl program <kchen@laurel.ocs.mq.edu.au>
     Re: Socket reading problem. Help, please <jay@rgrs.com>
     Re: Substitution question (David  Bell)
     Re: Substitution question <eric@NetTown.com>
     Re: Substitution question (Tad McClellan)
     Re: Telnet scripting with perl <jay@rgrs.com>
     Re: term 'regular expressions' considered undesirable <vladimir@cs.ualberta.ca>
     Re: term 'regular expressions' considered undesirable <vladimir@cs.ualberta.ca>
     Re: term 'regular expressions' considered undesirable (Jeffrey)
     Re: term 'regular expressions' considered undesirable (Ilya Zakharevich)
     Re: trouble matching multiple fields (David  Bell)
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 20 Mar 1997 21:14:42 -0500
From: Daniel Bull <dbull@digex.net>
To: Robert Schuldenfrei <sailboat@tiac.net>
Subject: Re: /RE/ for Dummies - It is even hard for experts!
Message-Id: <Pine.SUN.3.96.970320210843.15197A-100000@access5.digex.net>

Talk about your untutored neophytes, that's me with respect to Perl.  
Nonetheless, I recently needed to convert certain lines from all caps (or 
no caps, for that matter) to initial caps.  In addition, I wanted to have 
certain words in all lower case.  For instance, you probably wouldn't 
want "Massachusetts Institute Of Technology."  "Massachusetts Institute 
of Technology" would be more like it.  Anyway, here's what I hacked out 
in my ignorance.  When I detected a line which I wanted to process, I 
called a subroutine "fixcaps" which did what _I_ wanted, at least.  With 
some trepidation, here it is:


sub fix_caps {
   local ($local_add) = @_;
   @add_parts = ();                      # empty the array
   $local_add =~ s/^\s+//;               # remove leading spaces
   $add_parts = split(/\s+/,$local_add); # split into single words
   $a = @add_parts;                      # no of words in line
   $b = "";                              # holder variable
   for ($i = 0; $i < $a; $i++) {
      if ((length $add_parts[$i] > 3) && !($add_parts[$i] =~ /\bWITH\b/)) {
          substr($add_parts[$i],1) =~ tr/A-Z/a-z/;  # capitalize
      } else {
          if ($add_parts[$i] =~ /FOR|AND|OF|OR/) {
              $add_parts[$i] =~ tr/A-Z/a-z/; # all lc for small words
           }
      }
      $b = "$b "."$add_parts[$i]";    # put all back into a single line
   }
   $b =~ s/^\s+//;                    # remove leading spaces
   print MYFILEHANDLE "\"$b\",";      # write the line
}	

Modification to include short words in lower case (for|and, etc.) would 
be trivial.  The subroutine will change all words longer than 3 
characters to initial caps, except for the word "with," which is changed 
to all lower case.  Shorter words not in the exception list are left in 
caps.  You could make your own list of exceptions, for example the 
program name mentioned by one writer, which he wanted to keep in caps.  If
you have a different problem, that of changing all caps text to sentence
case (first word initial cap, others lower case, you have, well, a
different problem.

This is my hack, part of one of my first Perl scripts.  The 
"substr($foo,1) =~ tr/A-Z/a-z/;" part does the conversion to initial 
caps.  Hope you like it, and if you don't, be gentle :-}  I didn't have 
much of an idea what I was doing, but each task is a learning process, 
isn't it?

Dan Bull



------------------------------

Date: Fri, 21 Mar 1997 00:05:07 -0600
From: Chuck Bermingham <bermingh@concentric.net>
Subject: An interesting socket puzzle
Message-Id: <Pine.LNX.3.95.970320234918.132C-100000@ccchips.concentric.net>

Hi, friends!

I'm new to this newsgroup, so if I'm blowing my nose inappropriately,
please let me know!

Please take a look at this code, which I'm running on Linux (kernel
2.0.28 on Slackware, and based on an example from *Programming Perl*:

-----------Cut--------------------------

#!/usr/bin/perl -w
require 5.002;
use strict;
use Socket;
my($remote,$port,$iaddr,$paddr,$proto);
$remote = shift || chop($port=`hostname`);
$port = shift || 'daytime';
if($port =~ /\D/) {$port=getservbyname($port,'tcp')};
die "No port" unless $port;
$iaddr=inet_aton($remote) || die "No remote: $remote";
$paddr=sockaddr_in($port,$iaddr);
$proto=getprotobyname('tcp');
socket(SOCK1,PF_INET,SOCK_STREAM,$proto) || die "socket: $!";
connect(SOCK1,$paddr) || die "connect: $!";

my($line,$kid);
$kid=fork;
if($kid) {
  while($line = <SOCK1>) {
    print $line;
  }
} else {
  while($line = <STDIN>) {
    send SOCK1,$line,0;
  }
}
--------------Cut--------------

What it does is to create a bi-directional socket dialog between your
console and whatever port you connect to.  For instance, "client2
news.concentric.net nntp" will start up a "chat" with the news server, and
you can fool with the commands and stuff.  A little like telnet.  I picked
up this forking trick for telecomm from an old comm program I once saw on
Coherrent, for serial ports.

This works (believe it or not.  But if you substitute:

  print SOCK1 $line;

for the last executable statement (the "send", it hangs.

Weirder; if I press ctrl-D while the version with "print SOCK1..." is
running with two processes, the console behaves as if all the data had
been sent.

Anyone have any ideas?

--Chuck




------------------------------

Date: 21 Mar 1997 04:51:10 GMT
From: c.c.eiftj@42.usenet.us.com (Rahul Dhesi)
Subject: Re: any non-forking server template code?
Message-Id: <5gt43u$c7g@samba.rahul.net>

In <5gsr58$hgm$1@csnews.cs.colorado.edu> Tom Christiansen
<tchrist@mox.perl.com> writes:

>Hm... that sounds like what you want is a pre-forked server, which is
>a different beast entirely.   I don't know an example of that in Perl.

Yes, this is the type of server I was thinking of.
-- 
Rahul Dhesi <dhesi@spams.r.us.com>
a2i communications, a quality ISP with sophisticated anti-junkmail features
** message body scan immune to fake headers ***   see http://www.rahul.net/
>>> "please ignore Dhesi" -- Mark Crispin <mrc@CAC.Washington.EDU> <<<


------------------------------

Date: Fri, 21 Mar 1997 00:58:51 -0500
From: uminger@mcs.drexel.edu (Stinger)
Subject: Re: Can a Perl script interact with Java applet?
Message-Id: <uminger-ya02408000R2103970058510001@news.erols.com>

In article <33318914.5D96@trumpet.calpoly.edu>, Vinh Pham
<vpham@trumpet.calpoly.edu> wrote:

>    Mr. Williamson, thank you for your response.
>      Many books and programmers suggest using Perl to do pattern
> matching, an operation that I will need in the search capability of my
> online Classified Ads system. Although Perl is an interpreter and will
> take up more resources to run, it has a text string manipulation
> capability that is unmatched by compiled programming languages like C++.
> While Java's portability and compactness are indisputable, can you give
> me your opinion on its string manipulation features compared to Perl?
>                                                                 Thanks ,
> 

I've used both and I can honestly say that Perl has much better string
capability than java.

a statement that would tokenize a string in perl into say 4 items, would take
five statements in java.  Not to mention regular expression matching.  When
doing text processing prel is a much preferred method (unless of course
your admin doesn't let you run CGI scripts, in which case you have to do it
in a java applet)


------------------------------

Date: 21 Mar 1997 01:05:17 GMT
From: "Bill Brunning" <web435@ix.netcom.com>
Subject: Re: Checking a '/' chr. is not present
Message-Id: <01bc3594$53ce1d20$ccd823c7@stygian>

Paul Denman <pdenman@ims.ltd.uk> wrote in article
<01bc3522$f454f560$9d02a8c0@192.168.2.1.ims.ltd.uk>...
> Hello,
> 
> I have written a username/password script, and need to check that
> the following characters are not in either;   ":", "/" and " " (space).
> 
> I am using the syntax:
> 
> if (($FORM{username} !~ /:/) && ($FORM{username} !~ / /)) {
> 	...
> }
> 
> I can't work out how to check for the '/' character. (!~ /// doesn't
work).
> Can anyone help me on this?
> 
> Thanks,
> 
> Paul Denman
> pdenman@ims.ltd.uk
> 

Paul:

Try this ...

    if ($FORM{username} !~ m#[: /]#) {
        # whatever...
    }

The m# tells Perl that you're using #'s as delimiters, so you
can safely embed a slash in the regexp without confusing the
interpreter.  Enclosing colon, space, and slash inside the [ ]'s
checks for all 3 chars in one shot.

HTH.

Bill Brunning --- bill@stygian.com


------------------------------

Date: 20 Mar 1997 23:55:20 GMT
From: jrs@abiogenesis.com (Jonathan R. Seagrave)
Subject: DB_File.pm?
Message-Id: <jrs-0801040252350001@dialup06.nas.com>

Hi.  This little sample program works fine with MacPerl 5.1.3.r2 but
doesn't work under NeXTStep 3.2 with perl 5.00301.  according to the NeXT
there's no DB_File.pm file.  In fact, there *is* no DB_File.pm in with the
other .pm files.
Although I can find in in with the perl src, it didn't get installed with
'make install'

I guess what I really care to know is this:  Which of the /DB_.*/ packages
can be used and expected to be available to any system running perl5?  Is
it unusual that DB_File is unavailable on my NeXT?  I do have
db.1.85.tar.gz installed on the NeXT.  After rereading the Blue Camel book
(and each time is appears anew!) I'm still confused.  It seems as though
DB_File should be available.

Thanks alot for any help.  Here's the sample program:


#!/usr/local/bin/perl
use DB_File;
use Fcntl;

my $db_file = "phone.db";

tie(%phone_db, DB_File, $db_file, O_RDWR | O_CREAT, 0644)
or die "Problem tying $db_file: $!";

print "Phone Numbers:\n\n";

while (($name, $number) = each %phone_db) {
    print "$name, $number\n";   
}
untie(%phone_db);


#  Jonathan :)      jrs@abiogenesis.com


------------------------------

Date: 20 Mar 1997 15:42:26 GMT
From: jfriedl@tubby.nff.ncl.omron.co.jp (Jeffrey)
To: Russ Allbery <rra@cs.stanford.edu>
Subject: Re: Elegant way to strip spaces off the ^ and $ of a variable containing a sentence.
Message-Id: <JFRIEDL.97Mar21004226@tubby.nff.ncl.omron.co.jp>


[mail & post] [sorry for the late followup]

Russ Allbery <rra@cs.stanford.edu> wrote:
|> > I've always been slightly irked that you can't do this in one pass, but
|> > it's still pretty darn simple and elegant.
|> 
|> You can do it in one pass.  It's just slow.
|> 
|> 	$variable =~ s/^\s+(.*?)\s+$/$1/;

That requires both leading and trailing whitespace, so isn't really valid.
Change the pluses to stars and you've got your in-one-shot approach.

FYI, I discuss several approaches on p291 of my book, including the above
and a few others.

	Jeffrey
----------------------------------------------------------------------------
Jeffrey Friedl <jfriedl@ora.com>
See my Jap<->Eng dictionary at http://www.wg.omron.co.jp/cgi-bin/j-e
O'Reilly's Regular Expression book: http://enterprise.ic.gc.ca/~jfriedl/regex/


------------------------------

Date: 20 Mar 1997 19:57:35 -0500
From: psmith@baynetworks.com (Paul D. Smith)
Subject: Help reading pipes "unbuffered"?
Message-Id: <p5pvwu3vuo.fsf@baynetworks.com>

Argh.  I'm so frustrated with this program I'm trying to interface with
in a perl script I could scream.  Unfortunately, I can't avoid it, I
don't have the source to fix it.  While I attempt to get the vendor to
fix it, hopefully someone here can give me a hint.

I'm invoking a subprocess, a command.  I need to know if it fails.
Unfortunately (problem #1) under some conditions it fails, but still
returns a success (0) error code.

So, I started grabbing STDERR and reading that with a simple loop to
look for error messages:

  while (<FD>) {
    print $_;
    /: Error:/o and $error = 1;
  }

Fine, that works great.  However, under some circumstances the program
wants to prompt the user.  No problem, you say?  Well, unfortunately
(problem #2) some genius decided to print the prompt on STDERR instead
of STDOUT (I verified this by running the program myself with
2>/tmp/foo; the prompt goes to the file :-/).  And of course the prompt
doesn't end in a newline.

So, my while (<FD>) is waiting for the line to be finished before
printing it, and the program is waiting for some input.  "Help!  The
script is hung!".

Argh.

So, while I bitch to the vendor, can anyone suggest some ways to handle
this?  I can run this from the command line and it seems to work:

  program 3>&1 1>&2 2>&3 | tee /tmp/err.log

Swap STDOUT/STDERR and pipe to tee: the STDERR stuff both prints on the
screen (including the prompt) and goes to the log file.  But, I'd like
to avoid that if I can.

I've already written my own fork/exec sub in perl: is there any way I
can read from the file descriptor in an "unbuffered" way, rather than a
line at a time?  Do I need to use sysread?  Can anyone provide a small
example?

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@baynetworks.com>         Network Management Development
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
     These are my opinions--Bay Networks takes no responsibility for them.


------------------------------

Date: Fri, 21 Mar 1997 04:50:11 GMT
From: niksun@lconn.net (Niksun)
Subject: Re: Help with Pattern Matching
Message-Id: <3332136f.31432285@news.inetw.net>

What's a matter with:

 ...
$i = 1;  # use this to return line number
foreach $line(@lines) {
	if ($line =~ /$pattern/) {
		print "Pattern found in line $i";
	}
	$i++;
}
 ...

-------------------

On 19 Mar 1997 04:19:43 GMT, bill@sover.net.no.junkmail (Bill) wrote:

>In article <332DAAE6.4F14@toy.mem.ti.com>, "M. Wick" wrote:
>>I've tried grep, index, and the matching sequence and nothing seems to
>>be working.  I'm opening a file, reading in a line, and trying to
>>match a pattern from within the line.  I have found that I can match
>>the line if the parameter starts at the beginning on the line.  I want
>>to find the pattern in the middle of the line, and nothing seems to be
>>working correctly.


------------------------------

Date: Thu, 20 Mar 1997 13:48:04 -0700
From: Michael Owens <mikeo@gsd.state.nm.us>
Subject: How Do I Send KeyStrokes From Win32 Perl To a Win16 Application, Running On Win95?
Message-Id: <3331A284.3958@gsd.state.nm.us>

I want to start a Win16 application, get it to execute some commands
from its menu, then shut it down.  The app does not do OLE and the
limited DDE it has does not cover my needs; however, it does respond to
keyboard input.  Is there a way to send keystrokes from my Perl script
to this app?  I've read the Win32 FAQ and could not find anything there.

Thanks is advance.

Mike Owens
mowens@state.nm.us


------------------------------

Date: Fri, 21 Mar 1997 00:59:03 GMT
From: pduff@airmail.net (Patrick Duff)
Subject: interaction between local(), my(), and undef()
Message-Id: <3332dc53.344873875@news.airmail.net>

While thinking about "local", "my" and "undef" recently, I started
wondering about their possible interaction.  For example, if you have
a "my" variable and "undef" it, then use the same variable name later
on, is it still a "my" variable?

To test what happens, I wrote the following code and executed it in
Perl (Win NT build 302, v5.003):

    local($Variable) = 'one';
    print("1. Variable = $Variable\n");
    {  my($Variable) = 'two';
       print("2. Variable = $Variable\n");
       undef($Variable);
       print("3. Variable = $Variable\n");
       $Variable = 'three';
       print("4. Variable = $Variable\n");
    };
    print("5. Variable = $Variable\n");

I got the following output:

   1. Variable = one
   2. Variable = two
   3. Variable =
   4. Variable = three
   5. Variable = one

If I drop the "my()" function and run it again, the last line changes
to:

   5. Variable = three

So the "my-ness" of a variable survives an "undef".  As the above
example demonstrates, this is probably what you want in some cases.
Of course, you can force a "my" variable to go away completely by
starting a block before the "my" call and ending it after the "undef"
call, but this isn't always what you want to do.  I can't help
wondering if there is a way to completely blow away a variable name so
the next time you use it in the same block it is completely fresh,
without any left-over context.

Something else to ponder:  Suppose the above example was complexified
a bit so that $Variable was tainted sometimes and not others.  Would
the state of its taintedness survive an "undef"?

--
regards, Patrick Duff (pduff@airmail.net)



------------------------------

Date: 20 Mar 1997 21:19:11 GMT
From: shindle@mail.goodnet.com (Steve Hindle)
Subject: Non-Blocking sockets & status
Message-Id: <5gs9kf$16ha$1@news.goodnet.com>
Keywords: Perl Sockets non-blocking

 Hi all,

   I have some code that reads/writes sockets in non-blocking mode.
Everyting works fine, except I cant detect a disconnected socket.  The
code uses read and write to do the I/O and I had expected to get an error
return from read when the socket disconnects.  Unfortunately, I just get a 
normal return with a 0 return :(

   I then tried getsockopt with SO_ERROR before the read() - This doesnt
indicate any error when the client disconnects/broken circuit either :(

Also - I get a _VERY_ strange error with getsockopt:
  # Now we can read the port....Note: dont send the FD to the writer
  # until AFTER we recieve some data from it...It may disconnect :)
  if ( defined( $socket[$i]) ){
    if ( !($buffer = getsockopt( $socket[$i] , SOL_SOCKET, SO_ERROR)) ) {
      $buffer = "";
    }
    else {
      print "getsockopt ok($err):$err_msg\n" unless ($err == 11);
    }

=>  Next line gives:Argument "" isn't numeric in ne at Reader.pm line 111 
=>  if ($buffer != 0 ) {
      print "Buffer:$buffer\n";
      }



------------------------------

Date: 21 Mar 1997 00:24:50 -0500
From: Jay Rogers <jay@rgrs.com>
Subject: Re: Pattern matching on stream
Message-Id: <t6x3etplsv1.fsf@fluffy.rgrs.com>

pjscott-remove-to-email@euclid.jpl.nasa.gov (Peter Scott) writes:

> This sounds a bit odd but it would be useful.  I am wondering if
> there is such a thing as a streaming pattern match, which is the
> best term I can come up with.  Let me illustrate.  A common task
> is to scan a file until some pattern is found and set a variable,
> so the code is something like:
> 
> 	while (<INPUT>) {
> 		$found = 1, last if /foo/;
> 	}

I just released Net::Telnet which has the capability to waitfor a
pattern in the input stream or timeout if it does appear within a
specified time.  Of course this makes more sense if you're reading
from a socket or pipe, but it also works with a file.

Consider the following code:

    ## Create a file with three lines.
    open FH, ">file";
    print FH "The first line\nThe second line\nThe third line\n";
    close FH;
    
    ## Get a read/write filehandle to it.
    $fh = new FileHandle "+<file";
    
    ## Attach it to a "stream" object.
    use Net::Telnet;
    $file = new Net::Telnet (-fhopen => $fh);
    
    ## Search for the second line and print out the third.
    $file->waitfor('/second line\n/');
    print $file->getline;

--
Jay Rogers
jay@rgrs.com


------------------------------

Date: Fri, 21 Mar 1997 04:38:16 GMT
From: vss@xtra.co.nz (Jeremy Coulter)
Subject: printing Opened File
Message-Id: <33320fe6.8118784@202.37.101.7>

Hi There. 

I am using the following  code in an attempt to open an HTML file for
viewing.
When I run the script from the command line, it works. But what I call
the script from a Button event, POST, it dosn't siplay anything.

Is something wrong with my code ????

P.S. the file has <p>This is a test</p>  in it.

Cheers Jeremy Coulter


# CODE
sub DoShowPage
{
open(COUNTER, "< on.htm") || die "cant access";
@lines=<COUNTER>;
close(COUNTER);

print "<Head><Title>Opps!</Title></Head>";
print "<Body><H1>Opps!</H1></Body>";
foreach $line (@lines) {
print "@lines\n";

}
print "</body></Html>";
}



------------------------------

Date: Thu, 20 Mar 1997 21:25:34 -0600
From: Brian <signal@shreve.net>
Subject: regex for UNIX usernames needed!
Message-Id: <3331FF71.200EFBEB@shreve.net>

Has anyone written a thourough regular expression that I can use to
filter valid UNIX usernames with?

I am using: /^[a-zA-Z]{1,8]$/

but feel I need more than that.......I want to use this so the user wont
enter something they shouldn't, and I am sure someone has already done
this before...........if you have something like that, please email it
to me.  Thanks.

signal@shreve.net


------------------------------

Date: 20 Mar 1997 17:26:16 -0700
From: dbell@azstarnet.com (David  Bell)
Subject: Re: regex in perl4
Message-Id: <5gskj8$46v@web.azstarnet.com>

In article <3331B48D.64F2@aur.alcatel.com>,
Bruno Pagis  <pagib@aur.alcatel.com> wrote:
>I want to extract the second field (the one conaining Ys)
>in the following lines:
>X:Y:Z:
>XXX:YYYY:Z:WW:Q
>ZZ:YY:DD:SSSSSS:HHHHH
>
>And I'm stuck. In perl5, I would do
>/^.*?:(.*?):.*$/
>but what about perl4 where non-greedy modifiers don't exist.

(posted and CC'd)

Use a character class -- [^:] will match anything except a colon, so 

	/^[^:]*:([^:]*):/

(but you should upgrade to Perl5 anyway)

Dave


------------------------------

Date: Fri, 21 Mar 1997 15:47:01 +1100
From: Keping Chen <kchen@laurel.ocs.mq.edu.au>
Subject: running the first perl program
Message-Id: <333212C5.1C9B@laurel.ocs.mq.edu.au>

Hi, there

I am self-teaching CGI/FORMS, and running the first program. There is
one problem. My program runs command line okay but not from the browser
Netscape. the file ext. is *.cgi. I build cgi-bin subdirectory under my
homepage public_html, file and directory are worldreadle and can be
executed. The linkage no problem.

Netscape display:

501 Not Implemented

We are sorry to be unable to perform the method POST to non-script at
this time or to this document.

----------------------------------------------------
If I return the form page and submit again, browser dispaly

Data Missing

This document resulted from a POST operation and has expired from the
cache. If you wish you can repost the form data to recreate the document
by pressing the reload button. 

we use UNIX networking and I am wandering about I have no permission to
use perl on networked environment. Thanks for your tips!

thanks
http://www.ecr.mu.oz.au/~kchen/moon.html/


------------------------------

Date: 21 Mar 1997 00:37:15 -0500
From: Jay Rogers <jay@rgrs.com>
To: Alexander Osipov <shu@iis.nsk.su>
Subject: Re: Socket reading problem. Help, please
Message-Id: <t6x2099lsac.fsf@fluffy.rgrs.com>

Alexander Osipov <shu@iis.nsk.su> writes:

> For me it prints "Connected" and doesn't read something from socket.
> As I think it should read 'login: ' at least.
> 
> What's wrong?
> 
> my ($remote,$port, $iaddr, $paddr, $proto, $line);
> $remote = "java";                             
> $port   = 9999;     # telnet port             
> $iaddr   = inet_aton($remote) || die "no host: $remote";
> $paddr   = sockaddr_in($port, $iaddr);
> $proto   = getprotobyname('tcp');
> socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
> 
> connect(SOCK, $paddr)    || die "connect: $!";
> print "Connected\n";                               
> 
> $line = <SOCK>;         # HANGS HERE. WHY?
> 
> print "Line = $line\n"; 
> close (SOCK)            || die "close: $!";

I just uploaded Net::Telnet to CPAN which can help you communicate
with this port - especially if it's a telnet port as you suggest.

Here's how you'd login, issue the "who" command, and get it's output:

    $java = new Net::Telnet (-host => "java",
                             -timeout => 9999,
                             -prompt => '/[$%#>] $/');
    $jave->login($username, $passwd);
    @lines = $java->cmd("/usr/bin/who");

--
Jay Rogers
jay@rgrs.com


------------------------------

Date: 20 Mar 1997 18:09:03 -0700
From: dbell@azstarnet.com (David  Bell)
Subject: Re: Substitution question
Message-Id: <5gsn3f$l3g@web.azstarnet.com>

(posted and CC'd)

In article <3331D73A.6902@bc.sympatico.ca>,
Karl Bridge  <wholething@bc.sympatico.ca> wrote:
>
>When I use the perl substitution:
>	$line =~ s/\<a href.*\>//g;
>It strips out the entire line from the anchor tag.

Yes, the "." means "match anything" (essentially).  What you really want to
say is "match anything except >".  So, instead of "." use a character class
"[^>]" that matches what you want:

	$line =~ s/<a href[^>]*>//g;

You don't need to escape the angle brackets in Perl.

Dave


------------------------------

Date: Thu, 20 Mar 1997 19:53:17 +0000
From: Eric <eric@NetTown.com>
To: wholething@bc.sympatico.ca
Subject: Re: Substitution question
Message-Id: <333195AD.4170B9A0@NetTown.com>

Karl Bridge wrote:
> 
> Hi, I am trying to strip tags out of an HTML file and reformat a section
> of the file to present it in a different manner.
> 
> I seem to be having great difficulty with something I would have thought
> would be quite simple. We are developing on a Novell IntranetWare Web
> server running a subset of perl 4 commands that Novell has implemented.
> 
> I would like to take a line of HTML:
> <i>and</i> <b><a href=http://www.xxx.com>testing</b></a>
> and strip out the tag <a href=http://www.xxx.com>.
> 
> When I use the perl substitution:
>         $line =~ s/\<a href.*\>//g;
> It strips out the entire line from the anchor tag.
> 
> Any help would be appreciated.
> 
> KB

The problem is that * is "greedy", which means it starts searching from
the end of the string ($) backwards. What you need to do is use the
non-greedy operator *?. It starts from the current position and moves
forward.

$line =~ s/<a href.*?>//g; 

Note also that < is not a metacharacter, so it doesn't need to be
escaped. Check out the quotemeta built in function.

If you need, the script I wrote to test this is running at
http://nettown.com/site_perl/doc/eg/news.getlink.cgi

I hope this helps, good luck.
--
Eric
<mailto:eric@NetTown.com>
[http://NetTown.com/site_perl/]
have a good day!


------------------------------

Date: Thu, 20 Mar 1997 22:26:30 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Substitution question
Message-Id: <ml2tg5.hl.ln@localhost>

Karl Bridge (wholething@bc.sympatico.ca) wrote:
: Hi, I am trying to strip tags out of an HTML file and reformat a section
: of the file to present it in a different manner.

: I seem to be having great difficulty with something I would have thought
: would be quite simple. We are developing on a Novell IntranetWare Web
: server running a subset of perl 4 commands that Novell has implemented.

: I would like to take a line of HTML:
: <i>and</i> <b><a href=http://www.xxx.com>testing</b></a>
: and strip out the tag <a href=http://www.xxx.com>.

: When I use the perl substitution:
: 	$line =~ s/\<a href.*\>//g;
                   ^         ^
                   ^         ^

What are those for?

< and > are not special (in a pattern match). They do not need escaping.


$line =~ s/<a href[^>]+>//gi; # for camel carcass perl
                  ^^^^     ^
                  ^^^^     ^ <A HREF  too?


$line =~ s/<a href.+?>//gi;   # for a perl of non-historical interest (perl 5)
                   ^


I changed it to one or more too. It's faster and you don't really
want to match <a href>  do you?


: It strips out the entire line from the anchor tag.


--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming
    tadmc@flash.net


------------------------------

Date: 20 Mar 1997 23:38:04 -0500
From: Jay Rogers <jay@rgrs.com>
To: jkekoni@cc.hut.fi
Subject: Re: Telnet scripting with perl
Message-Id: <t6x67yllv0z.fsf@fluffy.rgrs.com>

"Joonas Kekoni" <jkekoni@cc.hut.fi> writes:

> So i think using the telnet program is not an option. I am not familiar
> with tcpip-programming, so i thought it could be easier to find a ready
> "telnet script" and
> then change it to fit my purpose.

I just released Net::Telnet which should do what you want.  Is has an
easy to use interface, especially for those unfamiliar with TCP
programming.  You can also use it to communicate with ports other than
TELNET.  It also has interactive features that make it easier to
communicate with the program at the other end of the telnet session.

It's located at:

    $CPAN/modules/by-module/Net/Net-Telnet-3.00.tar.gz

To find a CPAN site near you see ftp://ftp.cis.ufl.edu/pub/perl/CPAN/SITES

--
Jay Rogers
jay@rgrs.com


------------------------------

Date: 20 Mar 1997 18:49:23 -0700
From: Vladimir Alexiev <vladimir@cs.ualberta.ca>
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <omk9n2f1zw.fsf@tees.cs.ualberta.ca>

In article <E77HB5.7Bp@nonexistent.com> abigail@ny.fnx.com (Abigail) writes:

> ++ Previously, Rahul Dhesi wrote:
> ++ >   /a.*b.*c/	# greedy, needs backtracking, slow, not regular expression
My comments on the above comments: 
- right
- depends on the implementation
- maybe
- wrong. This is as regular as it gets.

> ++ >   /a.*?b.*?c/  # non-greedy, no backtracking, fast, regular expression
- right
- often would need backtracking, eg for the string axbbc the matcher will
  first pick x for the first .*?, but then will have to reconsider and pick
  xb.
- sometimes
- yes it is regular. The greedy/nongreedy distinction doesn't affect
  regularity, but only what parts of the string are mathched to what part of
  the regexp.

> ++ Regular expressions are capable of supporting r* (Kleen
> ++ closures), r+ (positive closures), and r? (whatever this is called).

A shortcut for r|empty. The non-greedy modifier ? is not part of the
formal-language definition of regexp.

> ++ the \1 and related patterns.  This back-referencing ability allows
> ++ Perl regular expressions to recognise balanced and nested constructs,

No, it doesn't. But it's true that \1 is non-regular.

> AFAIK the class of "all languages minus a regular one" isn't regular,

What exactly do you mean by the above construction? Because the complement of
a regular language R (the set of all strings not in R) is certainly regular.
Just take the corresponding automaton and "invert" it: make all accepting
states non-accepting and vice versa.

> (You can do intersection and subtraction of RE's with look-a-head too.)

The intersection of two regular languages is regular. Take the two automata A1
and A2 and make their product A3. The states of A3 are pairs <q1,q2> of states
of A1 and A2 and the transitions of A3 are <q11,q21> -a-> <q12,q22> iff
q11 -a-> q12 in A1 and q21 -a-> q22 in A2. The accepting states of A3 are
<q1,q2> where q1 is accepting in A1 and q2 is accepting in A2.

By the intersection and complement results, it also follows that the
difference of two regular languages is also regular. The same holds for union,
though a more direct proof is available: take the disjoint union of A1 and A2
(except merge their initial states); this automaton is non-deterministic, but
it still recognizes a regular language.

But, this is not a theory group :-)


------------------------------

Date: 20 Mar 1997 18:54:26 -0700
From: Vladimir Alexiev <vladimir@cs.ualberta.ca>
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <omiv2mf1rh.fsf@tees.cs.ualberta.ca>

In article <332F2B4F.75E9@cs.berkeley.edu> Steve Fink <sfink@cs.berkeley.edu> writes:

> My apologies for putting this on a perl newsgroup, but it sorta kinda
> relates.

You shouldn't apologize for educating people :-) Although I've read somewhere
that lecturers at 16th century universities usually started with "Esteemed
colleagues, please kindly allow me to bore you with the material I have so
carefully prepared".

> It seems like intersection could be done by... er... taking the
> cartesian product of the power sets of the states in the two NFAs

If you start with DFAs, you can simply take the cartesian product of the sets
of states, not the power sets.

> For all I know, regular languages may not be closed under intersection.

They are.


------------------------------

Date: 20 Mar 1997 16:29:55 GMT
From: jfriedl@tubby.nff.ncl.omron.co.jp (Jeffrey)
To: c.c.eiftj@33.usenet.us.com (Rahul Dhesi)
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <JFRIEDL.97Mar21012955@tubby.nff.ncl.omron.co.jp>


[mail and post]

Rahul Dhesi <c.c.eiftj@33.usenet.us.com> wrote:
|>    /a.*b.*c/	# greedy, needs backtracking, slow, not regular expression
|>    /a.*?b.*?c/  # non-greedy, no backtracking, fast, regular expression

There's no fundamental difference in the above two, other than in which of
the plausable matches will be selected. Both do backtrack, (and, for that
matter, will do so exactly an equal number of times for a non-match). There
are times when the first will backtrack much more than the second, and
times when the second will backtrack much more than the first.

All other things being equal, the first (the one you say is slow) will be
faster than the second, but this is due to some of Perl's internal
optimizations. In any case, knowledge of the likely data (and the
requirements of the task, of course) often make one better than the other.

|> Perhaps it's now too late, and we need to find a new term for what
|> we used to call regular expressions.

Perhaps it's best to differentiate between the calculus phrase ``regular
expression'', coined by Dr. Stephen Kleene in the 40s, and the use of the
same term in modern computer science. There are many situations in language
where the meanings of words have changed. Some examples:

  When I was growing up, ``How are you doing?'' was not a statement.

  A Japanese futon has no relationship to what is called a "futon" in
  American English.

  Tennis shoes are rarely used for tennis.

  The little eraser-head pointing devices on laptops are called mice.

Ah, well, I could go on forever. But basically, the term ``regular
expression'' has no implied meaning to most people that first come across
it in Perl or whatnot, so little confusion arises. It would have been nice
had some other word been used from the start (perhaps one giving more of a
hint as to what the thing is all about), but for the most part we're stuck
with history. I suppose you could start a crusade to use a different
phrase. How about 'whachamacallit'? :-)

	Jeffrey
----------------------------------------------------------------------------
Jeffrey Friedl <jfriedl@ora.com>
See my Jap<->Eng dictionary at http://www.wg.omron.co.jp/cgi-bin/j-e
O'Reilly's Regular Expression book: http://enterprise.ic.gc.ca/~jfriedl/regex/



------------------------------

Date: 21 Mar 1997 04:41:03 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <5gt3gv$4v7$1@mathserv.mps.ohio-state.edu>

[A complimentary Cc of this posting was sent to Vladimir Alexiev 
<vladimir@cs.ualberta.ca>],
who wrote in article <omk9n2f1zw.fsf@tees.cs.ualberta.ca>:
> In article <E77HB5.7Bp@nonexistent.com> abigail@ny.fnx.com (Abigail) writes:
> 
> > ++ Previously, Rahul Dhesi wrote:
> > ++ >   /a.*b.*c/	# greedy, needs backtracking, slow, not regular expression
> My comments on the above comments: 
> - right
> - depends on the implementation
> - maybe
> - wrong. This is as regular as it gets.
> > ++ the \1 and related patterns.  This back-referencing ability allows
> > ++ Perl regular expressions to recognise balanced and nested constructs,

> No, it doesn't. But it's true that \1 is non-regular.

- depends on the implementation
	$balanced = study /(
			    [^\(\)]+
			   |
			    \(
			      (?>$balanced)
			    \)
			   )*/x ;
- maybe

Ilya

P.S. study and (?>...) are not implemented yet, but close. As is
an optimization of (a+|b)* for most a and b.


------------------------------

Date: 20 Mar 1997 17:52:05 -0700
From: dbell@azstarnet.com (David  Bell)
Subject: Re: trouble matching multiple fields
Message-Id: <5gsm3l$ecv@web.azstarnet.com>

(posted and CC'd)

In article <3331CBDF.3B4C@exmachina.com>,
Paul S R Chisholm  <psrc@exmachina.com> wrote:
>I have lines of the form:
>	name field=value field=value
>and want to match the results with a minimum of fuss. I first tried: 
>	m/^(\w+)(?:\s+(\w+)=(\S+))*$/
>(match the first word, then successive field=value pairs).

You want to match them in pairs, and associate each field with a value...
sounds like a hash would be the best choice here (assuming you don't care
what order the fields are in).  I'd just match the name with it's own 
regex (for readability):

	my ($name) = m/^(\w+)/;
	my %fields = m/(\w+)=(\S+)/g;

now you can just step through the keys of the hash and do anything you
want with them.

>
>Yes, I've read Jeffrey Friedl's MASTERING REGULAR EXPRESSIONS (do you
>think I could have gotten into this much trouble by myself?) (great
>book, by the way).

Agreed!

Dave


------------------------------

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V8 Issue 152
*************************************

home help back first fref pref prev next nref lref last post