[8784] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2401 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 23 10:07:38 1998

Date: Thu, 23 Apr 98 07:00:38 -0700
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, 23 Apr 1998     Volume: 8 Number: 2401

Today's topics:
    Re: 1st cgi script doesn't work <BBrunekreeft@ovd.nl>
    Re: Array / List question <jdf@pobox.com>
        chdir in Perl on NT <sesek@intercortex.com>
    Re: chdir in Perl on NT <barnett@houston.Geco-Prakla.slb.com>
    Re: chdir in Perl on NT <jdf@pobox.com>
        duplicate scripts? <ratparker@msn.com>
        help with s/$foo/$bar/go; oddness (Sheppard)
    Re: help with s/$foo/$bar/go; oddness <jdf@pobox.com>
        RE: How to timeout TCP connect? web@calarts.edu
    Re: if ($string eg "y") or if ($string == "y") (I R A Aggie)
    Re: memory efficiency question <qdtcall@esb.ericsson.se>
    Re: memory efficiency question <jdporter@min.net>
    Re: msql <Tony.Curtis+usenet@vcpc.univie.ac.at>
    Re: Newbie Question re:mkdir for nested directories (Martien Verbruggen)
    Re: PB : sendmail subroutine <Tony.Curtis+usenet@vcpc.univie.ac.at>
    Re: Perl for NT (Jason Gloudon)
    Re: Perl for NT <merlyn@stonehenge.com>
    Re: Perl lets you change constant value (Andrew M. Langmead)
    Re: Perl on NT getting Directory Information <barnett@houston.Geco-Prakla.slb.com>
    Re: print multiple lines before or after match ? (Xuming Wang)
    Re: print multiple lines before or after match ? (Andrew M. Langmead)
    Re: Problem with Perl read on NT (Martien Verbruggen)
    Re: Problem with Perl read on NT <$_=qq!fearless\@NOSPAMio.com!;y/A-Z//d;print>
    Re: problem: num/str related <cnwetzel@linguistik.uni-erlangen.de>
    Re: Problems with 'wait' under perl5.004 <rgw@star.le.ac.uk>
    Re: Question on: printf %o STDOUT <qdtcall@esb.ericsson.se>
    Re: Regular Expression to match a valid IP address <jdporter@min.net>
    Re: Removing spaces from a variable, how? <jdf@pobox.com>
    Re: Short circuit predicates based on existence or defi <jdporter@min.net>
        socket input problems web@calarts.edu
    Re: socket input problems <jdf@pobox.com>
    Re: spoiled noobie (Jason Gloudon)
        suid perl scripts yet again (Norman Gaywood)
    Re: There *IS* a visual IDE for Perl! <merlyn@stonehenge.com>
        Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: Thu, 23 Apr 1998 13:20:17 +0200
From: "Bert Brunekreeft" <BBrunekreeft@ovd.nl>
Subject: Re: 1st cgi script doesn't work
Message-Id: <6hn811$nqh$1@news.worldonline.nl>

Your script returns

Hello from www.best.com
You're visiting from: 195.240.74.130
Your brower is:       Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)


to me, so it seems to be working. I'm using IE 4.01.

Bert Brunekreeft




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

Date: 23 Apr 1998 09:13:23 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: Array / List question
Message-Id: <vhs0k4n0.fsf@mailhost.panix.com>

? the platypus {aka David Formosa} <dformosa@st.nepean.uws.edu.au>
writes:

> In <soncz8of.fsf@mailhost.panix.com> Jonathan Feinberg
> <jdf@pobox.com> writes:
>>The use of grep in void context is now officially considered
>>harmful. Please use foreach instead.
> 
> Who officially decleared it?

Here's an excerpt from one Randal post
(<8cenaq6vs8.fsf@gadget.cscaper.com>):

   Ouch. Please don't do this.  It's considered bad form to use map or
   grep in a void context.  Use the equivalent foreach instead:

	   $sum = 0;
	   for (@nums) { $sum += $_ }

Here's another (<8csp1xydui.fsf@gadget.cscaper.com>):

   I'm no longer "randal the clever hack author".  I'm trying to stay a
   little more mainstream these days.  map and grep in a void
   context... Ick. :-)

And, finally, the blue Camel, pg 548:

   Avoid using grep, map, or backticks in a void context...

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY
Who thanks DejaNews for saving his ass


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

Date: Tue, 21 Apr 1998 18:06:43 +0100
From: Tomaz Sesek <sesek@intercortex.com>
Subject: chdir in Perl on NT
Message-Id: <353CD223.385E303A@intercortex.com>

I've got a simple problem but it's causing me quite a bit of trouble:
My perl scripts are in in the directory 'cgi' on the web site. when i
want to output a HTML page, say 'home.html' which is in root directory
as a result, I would need to change the working directory to '../'.

I do this, the chdir() function returns TRUE but perl does not find
home.html. Of course, I can read the file directly as '../home.html' but
in that case all references to any images or other html files are wrong.
Of course, that can be solved by putting '<BASE HREF="blobby.com"> but I
don't like it - you have to be very careful when programming HTML pages
within subdirectories, etc...

So, why doesn't my chdir() work OK? Do I need to change any access
rights? I am running NT 4 with IIS4 but the same was the case on an SGI
Solaris box...

Thanx.

Tomaz
--
------------v---v------------------------------------------------
   |    Tomaz Sesek, BA (Cantab)    Tel: +386 (61) 132 32 58
   |    INTERCORTEX AG, Dunajska 21 Fax: +386 (61) 132 40 93
   |o   SI-1000 Ljubljana           E-mail: sesek@intercortex.com




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

Date: Thu, 23 Apr 1998 08:17:48 -0500
From: Dave Barnett <barnett@houston.Geco-Prakla.slb.com>
Subject: Re: chdir in Perl on NT
Message-Id: <353F3F7C.CACD9A2B@houston.Geco-Prakla.slb.com>

Tomaz Sesek wrote:
> 
> I've got a simple problem but it's causing me quite a bit of trouble:
> My perl scripts are in in the directory 'cgi' on the web site. when i
> want to output a HTML page, say 'home.html' which is in root directory
> as a result, I would need to change the working directory to '../'.
> 
> I do this, the chdir() function returns TRUE but perl does not find
Are you sure that your script is starting in the directory you think it
is?  It sounds like you think it is starting in one directory, but yet
it is really running from another.

Have a look at the CWD module.  That has functionality to allow you to
determine your current working directory (at least on unix).  It should
be part of the standard dist., and has documentation built in.  (perldoc
CWD)

> home.html. Of course, I can read the file directly as '../home.html' but
> in that case all references to any images or other html files are wrong.
> Of course, that can be solved by putting '<BASE HREF="blobby.com"> but I
> don't like it - you have to be very careful when programming HTML pages
> within subdirectories, etc...
> 
> So, why doesn't my chdir() work OK? Do I need to change any access
> rights? I am running NT 4 with IIS4 but the same was the case on an SGI
> Solaris box...
> 
HTH.

Dave

-- 
"Security through obscurity is no security at all."
		-comp.lang.perl.misc newsgroup posting

----------------------------------------------------------------------
Dave Barnett                 U.S.: barnett@houston.Geco-Prakla.slb.com
DAPD Software Support Eng    U.K.: barnett@gatwick.Geco-Prakla.slb.com
----------------------------------------------------------------------


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

Date: 23 Apr 1998 09:38:06 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: chdir in Perl on NT
Message-Id: <son4k3ht.fsf@mailhost.panix.com>

Tomaz Sesek <sesek@intercortex.com> writes:

> So, why doesn't my chdir() work OK? 

Ask perl, and it will tell you:

   use CGI::Carp qw( fatalsToBrowser );
   chdir $some_directory 
      or die "I can't chdir to $some_directory: $!";

The magic variable $! contains a helpful error message following the
failure of most Perl builtins.

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY


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

Date: Thu, 23 Apr 1998 11:21:46 GMT
From: "Matt Parker" <ratparker@msn.com>
Subject: duplicate scripts?
Message-Id: <evF%.649$Gt1.430893@news2.teleport.com>

Hello, I have a classified ad type of script written in pearl where each
category is independent of the others (separate HTML forms) but they each
run of the same .cgi scripts in my cgi-bin. I was wondering if there would
be any advantage at all in creating multiple copies of these .cgi scripts so
that the same ones are not being accessed simultaneously and sometimes with
heavy traffic - to distribute the burden somewhat?

I will appreciate any advice.

Matt Parker
matt@skydesigns.com





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

Date: Thu, 23 Apr 1998 02:16:13 GMT
From: xie@netcom.com (Sheppard)
Subject: help with s/$foo/$bar/go; oddness
Message-Id: <xieEruGz2.7ns@netcom.com>

#!/usr/local/gnu/bin/perl

   # the object is to globally replace all instances of the bar character (|)
   # with the colon character (:).   however...

   $text = "now|is|the|time";

   $copy = $text;
   $copy =~ s/|/:/go;
   print "text = $copy \n";
   # prints "text = :n:o:w:|:i:s:|:t:h:e:|:t:i:m:e:"    not what I expected!

   $copy = $text;
   $copy =~ s/\|/:/go;
   print "text = $copy \n";
   # prints "text = now:is:the:time"    this is what I want... but I want to do it
   #                                    from within a string.

   $token = "|";
   $new_token = ":";

   $copy = $text;
   $copy =~ s/$token/$new_token/go;
   print "text = $copy \n";
   # prints "text = :n:o:w:|:i:s:|:t:h:e:|:t:i:m:e:"    not what I expected again!


   $token = "\\|";  # what the hell?

   $copy = $text;
   $copy =~ s/$token/$new_token/go;
   print "text = $copy \n";
   # prints "text = now:is:the:time"    this is what I want... but I want to do it
   #                                    from within a string whose contents *may or may not*
   #                                    contain the bar character.


   $fix_bar = "\\|";

   $token = "now|";           # we'll force it to have one...
   $new_token="who:has:";

   $copy = $text;
   $copy =~ s/$token/$new_token/go;
   print "text = $copy \n";
   # prints:
   #        text = who:has:who:has:|who:has:iwho:has:swho:has:| \   manual line wrapping here
   #               who:has:twho:has:hwho:has:ewho:has:|who:has: \   but you can see that it
   #               twho:has:iwho:has:mwho:has:ewho:has:             inserted the token between
   #                                                                every pair of characters !?
   # ok, that didn't work, lets try a replacement on the token string first...

   $new_bar = "\\" . $fix_bar;
   $token =~ s/$fix_bar/$new_bar/go;
   print "token is: $token\n";
   # token is: now\\|

   $copy = $text;
   $copy =~ s/$token/$new_token/go;
   print "text = $copy \n";

   # output even worse the the previous one...

   # needless to say, I haven't figured this one out.  Any help would be appreciated.

-Michael



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

Date: 23 Apr 1998 09:33:25 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: help with s/$foo/$bar/go; oddness
Message-Id: <u37kk3pm.fsf@mailhost.panix.com>

xie@netcom.com (Sheppard) writes:

>    $copy =~ s/|/:/go;
>    print "text = $copy \n";
>    # prints "text = :n:o:w:|:i:s:|:t:h:e:|:t:i:m:e:"    not what I expected!

That's because you're forgetting that | is a regex metacharacter, and
that you're matching either a null expression or a null expression,
which happily matches at every possible point in the string.  (Also,
the /o modifier isn't sensible there.)

>    $token = "|";
>    $new_token = ":";
>    $copy =~ s/$token/$new_token/go;
>    # prints "text = :n:o:w:|:i:s:|:t:h:e:|:t:i:m:e:" 
>    # not what I expected again!

$token, when interpolated, is *still* a regex metacharacter here.  You
just need to read about the quotemeta builtin.  Summary:

     $copy =~ s/\Q$token\E/$new_token/go;

See: perlre, perlfunc quotemeta, and perlfaq6: "How can I quote a
variable to use in a regexp?"

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY


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

Date: Wed, 22 Apr 1998 21:24:51 -0600
From: web@calarts.edu
To: John Chambers <jc@ral1.zko.dec.com>
Subject: RE: How to timeout TCP connect?
Message-Id: <6hm8pi$ev9$1@nnrp1.dejanews.com>

John Chambers <jc@ral1.zko.dec.com> wrote:

>My little web explorer has had a problem for which maybe some expert here
>can suggest a solution (or a FAQ or RTFM would be fine).  Everything works
>except that a fair number of sites cause a very long (sometimes hours) hang
>in the connect() call.  I've tried every sort of timeout mechanism, including
>the ALRM signal, and nothing has worked.  Setting up an ALRM handler
>itself works; when the alarm goes off, the handler wakes up, tells me
>it was called, attempts to close the socket - and it hangs, never to return
>until TCP decides much, much later to give up.

Have you tried putting the connect() call inside an eval statement, and then
using the ALRM handler to die() out of the eval?  Here's a little snippet of
code I used that seems to work fine:

----
                eval {
                        local $SIG{ALRM} = sub { die "ftimedout\n" };
                                # NB \n required
                        alarm $fingertimeout;
                        $rawfinger = `finger $queryname\@$curhost`;
                        alarm 0;
                };
                die if $@ && $@ ne "ftimedout\n";       # propagate errors
----

Now, I realize this is a little different than your situation, since I'm
making a system call to `finger` rather than rolling my own socket connection,
so I don't know if this will work in your case.  But since it wasn't clear
from your post if you'd tried this sort of solution, I thought I'd suggest it.

Good luck.

-jason

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


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

Date: Thu, 23 Apr 1998 09:41:46 -0500
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: if ($string eg "y") or if ($string == "y")
Message-Id: <fl_aggie-2304980941470001@aggie.coaps.fsu.edu>

In article <353E81CD.167E@fhcrc.org>, Yeu-Perng Nieh <ynieh@fhcrc.org> wrote:

+  Does anyone know the difference b/w  
+ if ($string eg "y") and if ($string == "y")?

It is detailed in the documentation that came with your perl distribution.

James

-- 
Consulting Minister for Consultants, DNRC
The Bill of Rights is paid in Responsibilities - Jean McGuire
To cure your perl CGI problems, please look at:
<url:http://www.perl.com/CPAN-local/doc/FAQs/cgi/idiots-guide.html>


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

Date: 23 Apr 1998 13:41:31 +0200
From: Calle Dybedahl <qdtcall@esb.ericsson.se>
Subject: Re: memory efficiency question
Message-Id: <isd8e8ydck.fsf@godzilla.kiere.ericsson.se>

dwaring@nwsr.com (David Waring) writes:

> When reading from a filehandle, does
> while <FILEHANDLE>
>  read the whole file into MEMORY at once or one line at a time? 

One line at a time. 

> Just wondering if this is more memory efficient than;
> @myarray=<FILEHANDLE>

It is. A lot.
-- 
		    Calle Dybedahl, UNIX Sysadmin
       qdtcall@esavionics.se  http://www.lysator.liu.se/~calle/


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

Date: Thu, 23 Apr 1998 13:38:39 GMT
From: John Porter <jdporter@min.net>
Subject: Re: memory efficiency question
Message-Id: <353F45E4.50C9@min.net>

Calle Dybedahl wrote:
> 
> dwaring@nwsr.com (David Waring) writes:
> 
> > When reading from a filehandle, does
> > while <FILEHANDLE>
> >  read the whole file into MEMORY at once or one line at a time?
> 
> One line at a time.
> 
> > Just wondering if this is more memory efficient than;
> > @myarray=<FILEHANDLE>
> 
> It is. A lot.

It's also worth pointing out that 
	while (<F>) 
is more memory-efficient that
	for (<F>)
since the latter is essentially equivalent to
	@a = <F>;
	for ( @a )
(except the temporary array is anonymous.)

John Porter


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

Date: 23 Apr 1998 11:28:51 +0200
From: Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at>
Subject: Re: msql
Message-Id: <7xemyoring.fsf@beavis.vcpc.univie.ac.at>

Re: msql, Brad <blegge@iafrica.com> said:

Brad> Help Please.  Is there anyone out there who knows how
Brad> to use perl to connect to msql through odbc. I need to
Brad> be able to access msql from perl. They have got the
Brad> Lite engine but I cannot use it. Is there an ODBC
Brad> component that can be used to connect through. Or
Brad> maybe a perl library file written to connect to msql

You'll want the DBI and presumably the Msql-Mysql-modules

available from CPAN.

-- 
Tony Curtis, Systems Manager, VCPC,      | Tel +43 1 310 93 96 - 12; Fax - 13
Liechtensteinstrasse 22, A-1090 Wien, AT | http://www.vcpc.univie.ac.at/

"You see? You see? Your stupid minds! Stupid! Stupid!" ~ Eros, Plan9 fOS.


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

Date: 23 Apr 1998 03:12:32 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Newbie Question re:mkdir for nested directories
Message-Id: <6hmbj0$b1t$1@comdyn.comdyn.com.au>

In article <353CD2BE.7D32@itron.com>,
	Laura Morgan <laura.morgan@itron.com> writes:
> I'm trying to use the mkdir command to create nested directories.  It
> only seems to work when I create one directory at a time down the tree. 
> I'm runing Perl for WIN32.

use File::Path;
mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);

# perldoc File::Path

Martien
-- 
Martien Verbruggen                  | 
Webmaster www.tradingpost.com.au    | Very funny Scotty, now beam down my
Commercial Dynamics Pty. Ltd.       | clothes.
NSW, Australia                      | 


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

Date: 23 Apr 1998 13:31:38 +0200
From: Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at>
Subject: Re: PB : sendmail subroutine
Message-Id: <7x90owrcyt.fsf@beavis.vcpc.univie.ac.at>

Re: PB : sendmail subroutine, affix <affix@usa.net> said:

affix> Hi, I dont understand why my message isn't sent.  I
affix> have the right paht for the mailprogram (mail) and i
affix> have made the changes in the sub routine.  The last

affix> open(MAIL,"|$mailprog $CONFIG{'recipient'}");

Oh dear, goodbye system security!

I suggest moving to the CGI module and use Mail::Mailer to
send the message.

-- 
Tony Curtis, Systems Manager, VCPC,      | Tel +43 1 310 93 96 - 12; Fax - 13
Liechtensteinstrasse 22, A-1090 Wien, AT | http://www.vcpc.univie.ac.at/

"You see? You see? Your stupid minds! Stupid! Stupid!" ~ Eros, Plan9 fOS.


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

Date: Thu, 23 Apr 1998 12:10:50 GMT
From: jgloudon@hyssop.bbn.com.bbn.com (Jason Gloudon)
Subject: Re: Perl for NT
Message-Id: <slrn6jubtu.9rf.jgloudon@hyssop.bbn.com>

Nathan Stanford <nathan@cyberservices.com> wrote:
>Does Tom Phoenix have to put down everyone when they ask a question.  This

You are completely wrong. I wish people would stop casting stones before
they try carrying them.

--
Jason Gloudon


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

Date: Thu, 23 Apr 1998 12:58:19 GMT
From: Randal Schwartz <merlyn@stonehenge.com>
To: "Nathan Stanford" <nathan@cyberservices.com>
Subject: Re: Perl for NT
Message-Id: <8cemyou24a.fsf@gadget.cscaper.com>

>>>>> "Nathan" == Nathan Stanford <nathan@cyberservices.com> writes:

Nathan> This is a Perl Misc and Perl is a leading CGI Scripting Lang.

It's also a leading sysadm tool, and a leading
rapid-application-development tool, and a great way to do cross
platform prototypes, and a dozen other things.  There are specific
groups for CGI stuff.  CLPM is the catchall for the *other*
non-specific areas.  That's why it's called "misc"!

Nathan> Since the person is wanting to know about Perl for NT vs Perl
Nathan> for Unix, he seems be in the right place.  This is Perl (hmmm)
Nathan> and misc (Misc hmm)

And Tom answered that for the parts of the problem that were specific
to Perl, and pointed the requestor off to CIWAC for the parts that were
specific to CGI.

What's YOUR problem with that?

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 131 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

Date: Thu, 23 Apr 1998 01:05:54 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Perl lets you change constant value
Message-Id: <EruDpu.7E6@world.std.com>

irishman@technologist.com writes:


>Call me a pedant, but I think constants should stay constant. Note that
>$CONST-- also works and produces the result 39. Any other operation that I
>tried returns "Modification of a read-only value attempted at test.pl line 3."
>which is what I expect.

Besides the info mentioned in the other posts. You might also be
interested in the "constant" pragma.

use constant CONST => 40;

This gives you the performance advantages from constants, as well as
the semantics. (with "use constant" you avoid symbol table lookups,
and it is subject to the compilers constant folding.)

-- 
Andrew Langmead


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

Date: Thu, 23 Apr 1998 08:20:19 -0500
From: Dave Barnett <barnett@houston.Geco-Prakla.slb.com>
Subject: Re: Perl on NT getting Directory Information
Message-Id: <353F4013.79B07B60@houston.Geco-Prakla.slb.com>

Nathan Stanford wrote:
> 
> How can I get the dir information in Perl on NT?
> 
perldoc -f opendir
perldoc -f readdir
perldoc File::Find
	(part of std dist., but maybe not on NT...?)

Should shed some light on the subject.

> I want to search all of the files in the root and sub dirs with out telling
> it which ones to search manuelly.
> 
> Need to do this in NT
> 
> --
> 
> Nathan Stanford,  Web Autor
> The Perl N.uT. and  Web Desinger
> http://www.nettaxi.com/citizens/nathan/

Dave

-- 
"Security through obscurity is no security at all."
		-comp.lang.perl.misc newsgroup posting

----------------------------------------------------------------------
Dave Barnett                 U.S.: barnett@houston.Geco-Prakla.slb.com
DAPD Software Support Eng    U.K.: barnett@gatwick.Geco-Prakla.slb.com
----------------------------------------------------------------------


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

Date: Thu, 23 Apr 1998 04:21:33 GMT
From: xuming@NO__SPAM.email.unc.edu (Xuming Wang)
Subject: Re: print multiple lines before or after match ?
Message-Id: <353ebef9.22463028@152.2.25.74>

Mike Hammernik <mhammer@execpc.com> wrote:

>I would like to print the matched line and the next x lines whether that
>might be 2 or 10 additional lines.  Conversely it seems to make sense to
>be able to do it in reverse.  $prev is one line but how to print the
>previous 2 or 10 lines. 

I guess you need to save certain lines first.  since you want print in
reverse, it's a stack (push and pop).

I wrote a script based on another poster's reply:

#! perl -w

$lines = 2;  # how many lines need to be printed besides matched line

while (<DATA>) {
	shift @foo if @foo > $lines;
	push @foo, $_;
	if (/four/) {
		print pop @foo while @foo;
	}
}
__END__
one
two
three
four
five

---------
output:
four
three
two

if you want print 2 lines including the matched line, you can reverse
the two statements: push, shift

then output is:
four
three

HTH!

xuming



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

Date: Thu, 23 Apr 1998 13:42:40 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: print multiple lines before or after match ?
Message-Id: <ErvCr6.EFy@world.std.com>

tadmc@flash.net (Tad McClellan) writes:

>Mike Hammernik (mhammer@execpc.com) wrote:

>: I put the information in and forgot to ask the question.

>: I would like to print the matched line and the next x lines whether that
>: might be 2 or 10 additional lines.  

>-----------------------
>#!/usr/bin/perl -w

>$x = 4;   # number of lines of trailing context to be printed

>while (<DATA>) {
>   if (/three/) {
>      for ($i=0; $i<$x; $i++) {      # might go reading past the end of the
>         print STDOUT scalar <DATA>; # file if there are not at least $x
>      }                              # lines left...

But what if one of these lines contain the search phrase, then it
doesn't get its trailing context.

How about:

#!/usr/bin/perl -w
$context = 4;
while(<DATA>) {
   $lines = $context if /three/;
   print if $lines-- > 0;
}
__DATA__
one
two
three
four
four
three
two
one
-- 
Andrew Langmead


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

Date: 23 Apr 1998 04:36:07 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Problem with Perl read on NT
Message-Id: <6hmgfn$bo1$1@comdyn.comdyn.com.au>

In article <893242950.450790@ajatar.dragon.net.au>,
	"Noel Sampol" <noels@chilli.net.au> writes:
> Sorry, this is not a suggestion but a simple query...

You should start a new thread then, with an appropriate subject. Not
followup to someone else's post.

> Can UNIX CGI scripts run without modification on NT running a Perl
> interpreter?

Depending on how they are written. If they use anything OS specific,
then the answer is 'No'. If they don't do anything OS specific, then
the answer is 'Yes'.

A lot of the modules on CPAN are useable on Unix, NT, Mac, and
whatever else runs perl. But it all depends on the code.

some rules of thumb:

- if the code spawns external processes (exec() or system() or
  backticks), it almost certainly is not portable, but it might still
  be.

- if people use OS specific path/directory separators, it almost
  certainly is not portable, but it might be between certain
  platforms.

- if the code uses OS specific modules, it normally can't be ported.
  (POSIX, Win32::.*)

- if the code uses system calls that are not available on all
  platforms (fork(), alarm(), getpgrp(), pipe() readlink(), symlink(),
  syscall()?) or signals, it probably isn't portable.

- If the code uses System V IPC functions (msg.*, shm.* and sem.*
  functions), it probably won't port.

- if the code relies heavily on what gets returned by functions like
  stat() or the -X functions (specifically the times), it probably
  won't port very well.

- if the code uses any of the getpw.* functions, it probably won't port
  very well. 

- A whole bunch of the networking functionality might also
  need reviewing before porting.

This list is not complete by far, but it gives you an idea what to
look for, maybe.

> If not, is it just the first line such as '#!/usr/bin/perl' - what would you
> change this to hypothetically if the NT Perl interpreter was in directory
> '\bin\test' ?

NT doesn't care about this first line. Unix shells do. You will have
to configure your NT box appropriately if you want associations
(that's what they're called on NT, right) to work.

[snipped NT specific questions that would better be asked on a group
that knows about this]

Martien
-- 
Martien Verbruggen                  | 
Webmaster www.tradingpost.com.au    | I'm just very selective about what I
Commercial Dynamics Pty. Ltd.       | accept as reality - Calvin
NSW, Australia                      | 


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

Date: Thu, 23 Apr 1998 06:16:34 -0700
From: "Creede Lambard" <$_=qq!fearless\@NOSPAMio.com!;y/A-Z//d;print>
Subject: Re: Problem with Perl read on NT
Message-Id: <6hneuv$ofj@bgtnsc02.worldnet.att.net>

Yep, I shoulda said "WinPerl" instead of "NT". Second time I've fallen
into this trap today. Aargh . . .

Igor Krivokon wrote in message <01b41e87$8b0317e0$ced6d6cf@igork>...
>Creede Lambard <$_=qq!fearless@NOSPAMio.com!;y/A-Z//d;print> wrote in
>article <6hml62$bqt@bgtnsc02.worldnet.att.net>...
>>
>> Martien Verbruggen wrote in message
>> <6hmgfn$bo1$1@comdyn.comdyn.com.au>...
>>
>> >NT doesn't care about this first line. Unix shells do.
>
>That's true
>
>> 95% true. NT cares about any Perl switches that normally go on the
>> shebang line (e.g. #!/usr/bin/perl -w) but as for the location of the
>> Perl executable, yes, it gets that from the PATH environment
variable.
>>
>
>Well, this is about 50% true, probably :-)
>Yes, those switches do work, but this is perl who cares about, not NT.
>
>Look, if you have #!/blah/blah/perl -w
>statement as the first line of your script.pl, and you run it like
>perl script.pl
>...and yes, switch -w still does work.
>As far as I understand, in this case it has nothing to do with OS,
>perl itself emulates UNIX shell's behaviour.
>
>Regards,
>
>Igor Krivokon
><igor.k@usa.net>
>
>
>




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

Date: Thu, 23 Apr 1998 15:05:40 +0200
From: Christian Wetzel <cnwetzel@linguistik.uni-erlangen.de>
Subject: Re: problem: num/str related
Message-Id: <353F3CA4.7A88@linguistik.uni-erlangen.de>

Xah wrote:
> 
> I've been debugging a script for 3 hours, and am in severe pain now. I've
> narrowed the script to the following. The problem lies in the two _elsif_
> lines. I assume the problem is related to conversion between numbers and
> strings. If anyone can help out, greatly appreciated.
> 
> The script is pretty much self-explainatory. Try typing "scriptName 199801
> 199803" without the quotes in a unix prompt, where scriptName is the same of
> the  script. The problem is that it will not pass the verifyArgument
> subroutine, stucking at the two elsif lines.

[Following script indented for better readability by me]
 
> #!/usr/local/bin/perl

Where is the -w switch? It would have given the warning
"Use of uninitialized value at /path/to/script line xy",
leading to the reason for the problem.

> use strict;
> 
> sub verifyArgument 
> {
>    my($d1,$d2)= @_;
>    print "$d1, $d2, \n";
>    if (undef($d1) or undef($d2)) 
         ^^^^^         ^^^^^
That's the problem. You mean !defined($d1) and !defined($d2)
here. undef() explicitely removes a variable value and
defined() is to test whether a variable is defined. 
No number conversion problem here.

>    {
>       return "start date or end date are not given.";
>    }  
>    elsif ($d2 < $d1) 
>    {
>       return "end date must be later than start date.";
>    }
>    elsif (("$d1" !~ m@^\d{6}$@) or ("$d2" !~ m@^\d{6}$@))
                       ^       ^                ^       ^
I would have taken '/' as delimiter instead of '@', but 
that's just my personal opinion on readability.

>    {
>       return "date must be of the form yyyymm. e.g. 199803 for 1998 March.";
>    }
>    elsif ('true') 
>    {
>       return 'true';
>    };

No need for the ';' after a closing '}' in perl (and even
_before_). 

> };
> 
> MAIN:{

There is no need to declare a MAIN block. Just remove 'MAIN:{'
and the last closing parent, and it works the same. You need
labels like MAIN: only for some loop controls (see perlsyn page).

> # verify and assign arguments.
>    my($arg1,$arg2)=@ARGV;
>    
>    my($temp1)=verifyArgument($arg1,$arg2);
>    if ($temp1 ne 'true') 
>    {
>       print "$temp1\n\n";
>       print "problem !!!\n\n";
>       print "$arg1, $arg2\n\n"

If verifyArgument returns with an error because $d1 and/or
$d2 were not defined, you try to print undefined values
here. Better than returning with $temp from verifyArgument
would be to 'die' in the subroutine directly, giving the
reason of the abort, and returning to the main part only
after successful verification.

>    };
> };
> 
> __END__

Hope that helped,
   Christian


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

Date: Thu, 23 Apr 1998 14:41:53 +0100
From: Richard West <rgw@star.le.ac.uk>
Subject: Re: Problems with 'wait' under perl5.004
Message-Id: <353F4521.1DED6D6D@star.le.ac.uk>

Michael Francis FIR LDN wrote:
> 

> The module worked correctly under perl5.001, we have now upgraded to
> version 5.004. The symptom is that wait always returns -1 even though
> the process has spawned a number of child processes. This only occurs
> following a sigalarm . Does anybody know why this is the case? and if
> there is any way around this?
> 
> Thanks in advance
> 
> Mike

I'm getting a similar problem, in slightly different circumstances. I'm
using quoted execution to run various jobs, and their return status
always seems to be -1. I'm running them as children of a process which
is a child of a process with signal handlers...this might have something
to do with it, I'm still investigating.

Perl v5.004, Solaris 2.5.

Ever clueless,

Richard.

-- 
*-----------------------------------------------------------------------------*
 Richard West                                      Internet:
rgw@star.le.ac.uk
 Department of Physics & Astronomy, University of Leicester, Leicester
LE1 7RH
*-----------------------------------------------------------------------------*


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

Date: 23 Apr 1998 13:39:25 +0200
From: Calle Dybedahl <qdtcall@esb.ericsson.se>
Subject: Re: Question on: printf %o STDOUT
Message-Id: <isg1j4ydg2.fsf@godzilla.kiere.ericsson.se>

jessicam@llnl.gov writes:

> How do I get the value from STDOUT and assign it to a variable
> without redirecting to a file? I'm trying to get the mode of a file
> in octal ( i.e. 100640 ) from printf %o and I want to save the
> STDOUT value without writing it to a file.

If I managed to guess at your problem correctly, you want sprintf. The 
documentation is in the usual place.
-- 
		    Calle Dybedahl, UNIX Sysadmin
       qdtcall@esavionics.se  http://www.lysator.liu.se/~calle/


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

Date: Thu, 23 Apr 1998 13:55:02 GMT
From: John Porter <jdporter@min.net>
Subject: Re: Regular Expression to match a valid IP address
Message-Id: <353F49B9.36FA@min.net>

I don't think that's right, Kevin.
Your method says that 256.0 is valid, but it's not, afaik.

John Porter


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

Date: 23 Apr 1998 08:56:09 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: Removing spaces from a variable, how?
Message-Id: <yawwk5fq.fsf@mailhost.panix.com>

jgloudon@hyssop.bbn.com.bbn.com (Jason Gloudon) writes:

> Jonathan Feinberg <jdf@pobox.com> wrote:
> .
> >Speed.
> .
> 
> Odd how often the speed of s/// over tr/// for this purpose is discussed
> and how often it gets benchmarked.

I actually posted my response well before any of the others showed up
at my news server.  Due to a problem panix.com has had for a few days,
many of my messages didn't get propagated until yesterday, three days'
worth, all at once.  I'm not flooding!

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY
Ducking


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

Date: Thu, 23 Apr 1998 13:33:50 GMT
From: John Porter <jdporter@min.net>
Subject: Re: Short circuit predicates based on existence or definedness, not truth
Message-Id: <353F44C3.7194@min.net>

Andy Glew wrote:
> 
>...
> Please email responses; I don't read these newsgroups.

Heh. Loser.

But on a related note: why doesn't perl allow one to test
the existence of variables?  At least package variables;
since symbol tables are just like hashes, it would be
easy enough to check for an entry in the symbol table.
Well, I guess that's just it: it IS easy.  No special
primitive required.  But, OTOH, when one has summoned
@main::foo into existence, it suddenly appears that
$main::foo exists as well...  Maybe it's not so easy.

John Porter


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

Date: Wed, 22 Apr 1998 21:58:35 -0600
From: web@calarts.edu
Subject: socket input problems
Message-Id: <6hmaor$i7v$1@nnrp1.dejanews.com>

I'm trying to write a little FTP client, but I'm stuck on one little
particular:  How can I tell when the server has finished sending it's data and
is simply waiting for more input from me?  My main loop now looks something
like this (SOCK is the filehandle for a tcp socket connection):

----
        $|=1;
        select(SOCK); $| = 1; select(STDOUT);
        print "ready for first query: ";
        while (<STDIN>) {
                print "sending query: $_ ...\n";
                print SOCK "$_\n";
                print "sent query: $_\n";
                print "getting response: \n";
                while( $response=<SOCK> ) {
                        print $response;
                }

                print "ready for next query: ";
        }
        close (SOCK)            || die "close: $!";

----

Now, I'm sure a lot of you see the problem:  the while( $response=<SOCK> )
loop never finishes.  Since the connection is still open, even after the
server has sent all it's going to send, the loop just keeps waiting for the
next line.

But what's the solution?  I tried using something like
read(SOCK, $response, 1024)
but the same thing happened.  read() just kept waiting for those 1024
characters.  Is there some way to peek into SOCK's input buffer to see how
many characters are actually waiting there, so that I can read that many
characters in, and no more?

And yes, I know that there are CPAN modules that would handle all the details
for me, obviating the need for mucking about with socket handles and the like,
but:

1.  I'm doing this as much to teach myself about socket programming as
anything else, and

2.  Although I'm currently working on an IRIX system, I'm hoping to port my
finished product to MacPerl, and I doubt that the CPAN modules work on a Mac,
since MacOS isn't exactly POSIX-compliant...

-jason

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


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

Date: 23 Apr 1998 09:42:20 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: socket input problems
Message-Id: <ra2ok3ar.fsf@mailhost.panix.com>

web@calarts.edu writes:

> Now, I'm sure a lot of you see the problem:  the while( $response=<SOCK> )
> loop never finishes.  Since the connection is still open, even after the
> server has sent all it's going to send, the loop just keeps waiting for the
> next line.

You might want to check out perlfaq8, "Can I use perl to run a telnet
or ftp session?", which has some sample code that's similar to what
you're trying to do.  If *that* code doesn't work, please tell us
about it.  I'm sure other folks will post specific suggestions
regarding the code you posted.  I hope this helps.

-- 
Jonathan Feinberg   jdf@pobox.com   Sunny Brooklyn, NY


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

Date: Thu, 23 Apr 1998 12:11:53 GMT
From: jgloudon@hyssop.bbn.com.bbn.com (Jason Gloudon)
Subject: Re: spoiled noobie
Message-Id: <slrn6jubvu.9rf.jgloudon@hyssop.bbn.com>

Ronald J Kimball <rjk@coos.dartmouth.edu> wrote:
>
>Oh good, I was wondering when people would take a break from bashing Windows
>and start in on Macintosh.

Well when they both tend to crash with about equal frequency :)


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

Date: 23 Apr 1998 12:51:09 +1000
From: norm@turing.une.edu.au (Norman Gaywood)
Subject: suid perl scripts yet again
Message-Id: <6hmaat$obp@turing.une.edu.au>
Keywords: suid perl

I've read the perlsec man page and hunted through Deja News without
much success. So here goes:

By default, perl builds on my Digital Unix 4.0 (DU) system with secure
setuid scripts disabled. Fair enough I say, the perl guys that put it
together would know. But when looking to find out why, I had a look at
the Configure script.

To my eye there is a problem here. To test suid script secureness,
Configure creates a small script on the fly like this:

echo "#!$ls" >reflect
chmod +x,u+s reflect

Then tries to run it later with

su $ans -c './reflect >flect'

But, depending on your file creation mask, su may or may not be able to
get to the script to execute. So the test fails at a point that is
not relevent to what it is testing.

I setup to allow Configure to get past this problem. I also enabled the
/dev/fd pseudo file system in DU hoping that would allow suid scripts.
But alas, the test still fails. So I assume that my kernel has no
suid script support.

The next question in Configure asks if I want suid script emulation.
It has a default anwser of no. If I ignore the advice and say yes,
suidperl is built. But when I try to run a suid script with a
"#!/usr/local/bin/suidperl -Tw" line, suidperl shouts:

YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!
FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!

I can deal with wrapper scripts but I would really like to get setuid
scripts to run. So how do I disable suid scripts in the kernel that don't
seem to be supported in the kernel?

Aother note. My linux systems seem to have no suid support in the
kernel (even though /dev/fd is in place), according to the Configure
test. Configure also wants to say no to suid script emulation, but if
you build suidperl, it seems to work quite well.

-- 
Norman Gaywood -- School of Mathematical and Computer Sciences
University of New England, Armidale, NSW 2351, Australia
norm@turing.une.edu.au     http://turing.une.edu.au/~norm
Phone: +61 2 6773 2412     Fax: +61 2 6773 3312


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

Date: Thu, 23 Apr 1998 13:05:48 GMT
From: Randal Schwartz <merlyn@stonehenge.com>
To: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: There *IS* a visual IDE for Perl!
Message-Id: <8caf9cu1ru.fsf@gadget.cscaper.com>

>>>>> "Jonathan" == Jonathan Feinberg <jdf@pobox.com> writes:

Jonathan> I downloaded it, tried it with the first program I had at hand (which
Jonathan> was some sample code I used in this newsgroup), and it gave some
Jonathan> spurious error message.  It turns out that PB can't handle programs
Jonathan> with the __DATA__ construct in it.  I tried to Edit->Copy the error
Jonathan> message from the STDERR screen, and was treated to an address error.

Well, if it can't handle that, I bet I could easily confuse it with
some ugly syntax things that are impossible to parse without Perl's
knowledge of the world out there.  Go back to Dejanews and see my
similar argument against the *possibility* of a syntax coloring editor
when a claim was made for one that worked "perfectly".  (Look in
comp.lang.perl.misc for "merlyn" and "sin" and "time".)

Nothing can parse Perl like /usr/bin/perl.  It just can't be done.

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 131 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

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 2401
**************************************

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