[18803] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 971 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 23 18:10:42 2001

Date: Wed, 23 May 2001 15:10:16 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <990655816-v10-i971@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 23 May 2001     Volume: 10 Number: 971

Today's topics:
    Re: Multiple Line printing (jianxin)
    Re: Multiple Line printing nobull@mail.com
    Re: Multiple Line printing <godzilla@stomp.stomp.tokyo>
    Re: parsing perl again (Damian Conway)
    Re: parsing perl again (Damian Conway)
    Re: Permuting days of the week <Steve_Alpert@idx.com>
    Re: Permuting days of the week (Randal L. Schwartz)
    Re: problem with linux/cgi perl script (Abigail)
    Re: Problem with scalar variables nobull@mail.com
    Re: Split versus Regex nobull@mail.com
    Re: unlinking files under -T <webmaster@webdragon.unmunge.net>
    Re: url parsing <todd@designsouth.net>
    Re: url parsing (Gary E. Ansok)
        Writing file in Perl <shino_korah@yahoo.com>
    Re: Writing file in Perl (Peter L. Berghold)
    Re: Writing file in Perl <kstep@pepsdesign.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 23 May 2001 11:15:59 -0700
From: guojx@yahoo.com (jianxin)
Subject: Re: Multiple Line printing
Message-Id: <8b625622.0105231015.2892e817@posting.google.com>

grobitaille@mail.com (Gary) wrote in message news:<a2051d6.0105230401.5f5a46c7@posting.google.com>...
> I need to parse a text file and when a pattern is matched I need to
> print that line and the next 6 lines. Can someone provide me with a
> perl solution
> 
> 
> Thanks

If the number of the following line changes, how to deal with that? it
would be a more interesting question.


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

Date: 23 May 2001 20:21:18 +0100
From: nobull@mail.com
Subject: Re: Multiple Line printing
Message-Id: <u9wv776fdd.fsf@wcl-l.bham.ac.uk>

"Godzilla!" <godzilla@stomp.stomp.tokyo> writes:

> There is a very shadowy division line between efficiency
> of slurp looping and efficiency of while looping, as to
> which is more efficient. This division line is broad and
> hard to draw. Externalities such as actual data size,
> nature of the contents of the data and degree of needed
> processing, all effect an efficiency comparison.
>
> In general, small data reads are handled just as effectively
> by a slurp as are handled by a while. Inherently, both
> slurp and while have pros and cons. Neither can do all
> which the other can do.

True, and if you are prepared to stop and think which is better in a
particular case then you should use whichever is better.  

However it is not often worth the effort of stopping to think.  In
these cases go for "don't slurp" because the consequences of slurping
where you shouldn't are several orders of magnitude worse than the
consequences of not slurping where you should.  For this reason it
unkind to present the file-slurping idiom to newbies without a health
warning.

> Use of benchmark is a good tool to help make a decision on which is
> best for use.

However benchmark in this case is often misleading.  As the
externalities that Godzilla mentions, in particular the contentsion
for memery and size of data files, are often very different between
the development environment and the production environment.

Sure you can, with sufficient skill and effort, construct a meaningful
benchmark but if you are at that level of programming skill then you
are way beyond the point where Godzilla's advice of "benchmark is a
good tool" is going to be needed.

> Don't be fooled by those zealots whom spread Perl 5 Cargo Cult.

This has nothing to do with Perl.  The slurp/don't slrp argument is
the more or less the same in any language.  Except in some situations
it's further complicated by the mmap() alternative.

This is also a misuse of the term "Cargo Cult".  The metaphor "Cargo
Cult" alludes to people from primative cutures building things that
look like airports to try to attract "airplane gods".  (This story is
IITR apocryphal).  I can, however, quite resonably send air-cargo
without having understand all the avionics, excise reguations, money
transfers and so on that is needed to make all this work.

I think "rule of thumb" is a more appropriate term here.

> Test their dictates and discover your own truths.

The corollary to that is that there are some truths that you are not
really ready for until you have the skill to discover them yourself.

To avoid causing distress the illuminati tend not to talk about these
truths in the company of others.  I'm not just talking about
Perl/Usenet/Programming here - I'm talking about all aspects of life.

We are the Men in Black.  We are all Kosh.  We are shape-shifting
reptilian hybrids.  We are Frank.

(Oh dear, time to adjust my medication I think).

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Wed, 23 May 2001 13:22:33 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Multiple Line printing
Message-Id: <3B0C1C09.E5C40F45@stomp.stomp.tokyo>

nobull@mail.com wrote:

> Godzilla! wrote:

(unnoted snippage by bull)
 
(snipped)

> > In general, small data reads are handled just as effectively
> > by a slurp as are handled by a while. Inherently, both
> > slurp and while have pros and cons. Neither can do all
> > which the other can do.
 
> True, and if you are prepared to stop and think which is better in a
> particular case then you should use whichever is better.
 
> However it is not often worth the effort of stopping to think.

My experience is taking time to think is almost always beneficial.


> > Don't be fooled by those zealots whom spread Perl 5 Cargo Cult.
 
> This has nothing to do with Perl.  The slurp/don't slrp argument is
> the more or less the same in any language.  Except in some situations
> it's further complicated by the mmap() alternative.
 
> This is also a misuse of the term "Cargo Cult".  The metaphor "Cargo
> Cult" alludes to people from primative cutures building things that
> look like airports to try to attract "airplane gods".  (This story is
> IITR apocryphal).  I can, however, quite resonably send air-cargo
> without having understand all the avionics, excise reguations, money
> transfers and so on that is needed to make all this work.


This is a classic cargo cult misconception. This term Cargo Cult
originated during the nineteenth century, long before the Wright
boys decided to flap their arms and fly about. Earliest studies
of these cultural phenomenonas trace back to Melanesia and
New Guenia during the European time of colonialism / imperialism,
just prior to the Age of New Imperialism. Indigenous populations
did not fall to their knees before metallic birds falling from
their skies but rather marveled at wooden ships chasing after 
sheets filled with trade winds.

My minor is in Cultural Anthropology.

You boys are classic Cargo Cultists and, when Perl 6 is released,
you will develop and promulgate a new set of worshipped icons.

Praise be Perl God, all knowing all powerful Deity of Perl Perl Land.

 
> > Test their dictates and discover your own truths.
 
> The corollary to that is that there are some truths that you are not
> really ready for until you have the skill to discover them yourself.

There are no truths which should be hidden. Often little skill is
involved in truth discovery; truth is self-evident. Nonetheless,
discouraging people to learn of truth based on a premise of,
"You are not skilled enough to know truth," is certainly dubious.

"Truth is only dangerous when denied."
  - Kira

 
> We are the Men in Black.  We are all Kosh.  We are shape-shifting
> reptilian hybrids.  We are Frank.


You boys are Borg Geeks.


Godzilla!


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

Date: 23 May 2001 21:02:54 GMT
From: damian@cs.monash.edu.au (Damian Conway)
Subject: Re: parsing perl again
Message-Id: <9eh8hu$v2f$1@towncrier.cc.monash.edu.au>

jdimov@cis.clarion.edu (Jordan Dimov) writes:

   > I think I've heard rummors that there's a Perl 5 grammar for Damian's 
   > Parse::RecDescent?

Not yet. It's high on the ToDo list, but not yet at the top.

Meanwhile, you might want to take a peak inside Text::Balanced (particularly
the C<extract_codeblock> subroutine). You might find you can adapt the code
to your needs.

Damian


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

Date: 23 May 2001 21:04:22 GMT
From: damian@cs.monash.edu.au (Damian Conway)
Subject: Re: parsing perl again
Message-Id: <9eh8km$vpa$1@towncrier.cc.monash.edu.au>

Oops:

	s/peak/peek/

[Note to self: get more sleep]

Damian


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

Date: Wed, 23 May 2001 12:37:03 -0400
From: Steve Alpert <Steve_Alpert@idx.com>
Subject: Re: Permuting days of the week
Message-Id: <hspngtgpo5t7ocsuos8bedt7tti0cfhbf0@4ax.com>

chadbour@wwa.com (James Weisberg) wrote:

>
>   Here's a simple challenge.  
> 
>   Given the days of the week: Sun, Mon, Tue, Wed, Thu, Fri, Sat
>I would like to take various permuations and sequences of those strings 
>and fill the days in between. Examples: 
>
>Sun-Sat = "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
>Mon-Sun = "Mon,Tue,Wed,Thu,Fri,Sat,Sun"
>Wed-Tue = "Wed,Thu,Fri,Sat,Sun,Mon,Tue"
>etc, and sub-sequences:
>Sun-Tue = "Sun,Mon,Tue"
>Fri-Sun = "Fri,Sat,Sun"
>etc.

How 'bout

#!/user/bin/perl -w
use strict;

# every day follows every other day
my @dow = qw(Sun Mon Tue Wed Thu Fri Sat);
push @dow, @dow; # double it

my $s = shift;
my $e = shift;

my $f = 0;  # looking for start

print join ',', grep { !$f ? ( $_ eq $s ? ($f=1,$_) : "") :
              $f == 1 ? ( $_ ne $e ? $_ : ($f=2,$_) ) : "" } @dow;



        Steve Alpert (Steve_Alpert @ i d x . c o m)
        IDX Systems Corp.   Boston, Massachusetts



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

Date: 23 May 2001 13:15:23 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Permuting days of the week
Message-Id: <m1r8xfes9w.fsf@halfdome.holdit.com>

>>>>> "Randal" == Randal L Schwartz <merlyn@stonehenge.com> writes:

Randal> Hmm.  This sounds like a great homework problem.  I think I'll assign
Randal> it to my class (google for "OGI503").  Thanks!

Randal> If it's not a homework problem, you'll need to provide a little more
Randal> context.

In fact, it was such a good problem that I turned it into my next
SysAdmin Magazine Perl column!  Thanks for the inspiration.

Still smells of being a homework problem though.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: Wed, 23 May 2001 21:25:21 +0000 (UTC)
From: abigail@foad.org (Abigail)
Subject: Re: problem with linux/cgi perl script
Message-Id: <slrn9goam1.v99.abigail@tsathoggua.rlyeh.net>

Ja NE (janko@bellay.com) wrote on MMDCCCXXI September MCMXCIII in
<URL:news:1etu1a6.15qgs8afs0k4N%janko@bellay.com>:
$$  soory if this is'n appropriate place to ask (hope it is) but I'm in real
$$  trouble... and sorry on bad english.
$$  
$$  Problem is in understanding language - yes, I think that I'm under the
$$  level of 'novice'. I'm working on script called "soupermail", have lot
$$  of nice features, I just need to activate them... 
$$  here is my config file in which I have tryed to tell soupermail that
$$  attachment must have ".jpg" in name... but it isn't working... 


This is a group about Perl, not about help with some random application
that just happens to be written in Perl.

For help with your application, contact the vendor of your application.



Abigail
-- 
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"


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

Date: 23 May 2001 19:15:20 +0100
From: nobull@mail.com
Subject: Re: Problem with scalar variables
Message-Id: <u91ypg6ifb.fsf@wcl-l.bham.ac.uk>

Mark Patton <mark.patton@program-products.co.uk> writes:

> I'm struggling crossing over from shell scripting to perl. Stuff I know
> works in ksh is causing problems.

Try to forget work-rounds for the limitations of shell.

> What I want to do is
> 
> assign a variable...
> $variable=3
> use the value of the variable to make an array name unique.

Stop wanting that.

> then
> Increment the variable so the next array name is unique as I work
> through another array.

You only believe you want to do this because you are comming from an
enviroment where there's no better way to implement multidimentional arrays.

See "perldoc perllol".

> @array$variable does not evaluate to an arrayname of @array3.
> 
> Can anyone help me please?

Yes, I will help you by refusing to tell you the correct syntax to
achive what you ask :-)

What you are trying to do is use something called a symbolic
reference.  Perl references both symbolic and hard, are documented in
"perldoc perlref".  If you read that you'll find how to do what you
ask, but you'll also find why not to, and what to do instead.


> --------------47388CC9854526C10C251491
> Content-Type: text/x-vcard; charset=us-ascii;
>  name="mark.patton.vcf"

No, this is plaintext newsgroup.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 23 May 2001 19:04:52 +0100
From: nobull@mail.com
Subject: Re: Split versus Regex
Message-Id: <u94ruc6iwr.fsf@wcl-l.bham.ac.uk>

"M" <mike@gatrell.org> writes:

> I keep on having to pull variables from a csv and file do stuff with
> them.
> 
> I sometimes need to do stuff to all the variables in each line of the
> file, but more normally am interested in the first and last. Which is
> more efficient to do??

If you really want to know use Benchmark.

> while (<FOO>){
> 	@foo = split(/,/,$_);
> 	$first = $foo[0];
> 	$last = $foo[$#foo];
> 	}

I think there's probably a chmop missing there.  I doubt you really
want an EOL in $last.

Note $foo[$#foo] can be written as $foo[-1]

You can slice the return value from split directly without an
intermediate array.

my ($first,$last) = (split /,/)[0,-1];

Be aware that split used like this will ignore trailing empty items.

Be also aware that in current versions of Perl $first and $last will
be set to empty strings if $_ is empty.  This is a bug and in a future
version of Perl they may be undef.

> while (<FOO>){
> 	/(.*?),.*,(.*?)$/;
> 	$first = $1;
> 	$last = $2;
> 	}

In this case the lack of chomp is OK.

IMNSHO using $1 and so like this on makes code messy.

my ($first,$last) = /^(.*?),.*,(.*)$/;

The ^ and $ anchors are strictly redundant but I think they aid
readability.

The above won't work if there are fewer than 3 items, whereas
the split will work even if there's only 1.  If there's ever likley to
be fewer than 3 items:

chomp;
my ($first) = /^([^,]*)/;
my ($last) = /([^,]*)$/;

You _could_ do it with a single regex (using look-ahead) but it would
be messy.

At a guess the regex will win over split on speed although maybe not
if there are only a few items.  If you really care use Benchmark.

For an interesting discussion of the relative merits of the two
approaches see the thread "How to find what is between n'th and the
next tab?" from November last year.

> NB I'm still very new to this perl malarky and haven't tested the code
> above so it's quite likely flawed in some way.
> 
> any comments welcome.

Posting untested code is discourged.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 23 May 2001 20:29:50 GMT
From: "Scott R. Godin" <webmaster@webdragon.unmunge.net>
Subject: Re: unlinking files under -T
Message-Id: <9eh6ju$ogf$0@216.155.32.217>

In article <9ebdic$4lp$1@ichaos.ichaos-int>,
 Juha Laiho <Juha.Laiho@iki.fi> wrote:

 | "Scott R. Godin" <webmaster@webdragon.unmunge.net> said:
 | ># During-Usage Cleanup of old user-created files, but save our samples.
 | >opendir(DIR, $output_path) or die "Cannot Open Output Dir $output_path 
 | >-- $!";
 | >my @oldfiles = readdir(DIR);
 | >closedir(DIR) or die "closing read on $output_path directory failed: $!";
 | >
 | >foreach my $oldfile (@oldfiles) 
 | >{
 | ...
 | >        unlink "$output_path$oldfile"; #older than 24 hours
 | ...
 | >}
 | >
 | >I get an "Insecure dependency" warning about the unlink. Now, none of 
 | >this information is user-generated, so I don't quite understand why I 
 | >get a warning of this nature unless it's because ANY unlink operation is 
 | >considered unsecure (which seems to be the case, from my perusal of 
 | >perlsec.pod).
 | 
 | I'd claim that filenames read off from directory are the reason for that
 | insecure dependency. As your program doesn't actually know what generated
 | the filenames it's reading off the directory, perl throws the "insecure
 | dependency" at you.

So is this something I can then clean up with the simple sort of regex 
solution proposed in the depths of perlsec.pod ? 

these file names may contain the characters '[]{}' as well as /w, '-', 
'.', so I'm curious if it's safe to 'launder' them with a regex such as: 

    if ($data =~ /^([-\[\]\{\}\w\.]+)$/) {
        $data = $1;                     # $data now untainted
    } else {
        die "Bad data in $data";        # log this somewhere
    }

perhaps I should write that as 

    /^([\Q-[]{}.\E\w]+)$/

?

-- 
unmunge e-mail here:
#!perl -w
print map {chr(ord($_)-3)} split //, "zhepdvwhuCzhegudjrq1qhw"; 
# ( damn spammers. *shakes fist* take a hint. =:P )


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

Date: Wed, 23 May 2001 20:09:10 GMT
From: "Todd Smith" <todd@designsouth.net>
Subject: Re: url parsing
Message-Id: <GNUO6.62593$I5.13334185@news1.rdc1.tn.home.com>

> >
> >the first set of characters between /'s should be the domain, right? And
if
> >$1 doesn't match, $domain will stay undefined. Everybody wins.
>
> No, if the match doesn't succeed, $1 will be leftover from the last
> successful match.
>

Oh yeah. Got me.

 >$url =~ m|/(.+?)/|;

> Also, the regex should start with *two* slashes, otherwise the matched
> domain would be "/prefix.domain.suffix1.suffix2", which is probably not
> what you want.
>

no way! just add a slash to the end if there's not one, and the regex
matches the first instance, then stops. so it would match the first two
slashes with something between them, which would be the domain.




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

Date: 23 May 2001 21:15:08 GMT
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: url parsing
Message-Id: <9eh98s$k4p@gap.cco.caltech.edu>

In article <GNUO6.62593$I5.13334185@news1.rdc1.tn.home.com>,
Todd Smith <todd@designsouth.net> wrote:
> >$url =~ m|/(.+?)/|;
>
>> Also, the regex should start with *two* slashes, otherwise the matched
>> domain would be "/prefix.domain.suffix1.suffix2", which is probably not
>> what you want.
>
>no way! just add a slash to the end if there's not one, and the regex
>matches the first instance, then stops. so it would match the first two
>slashes with something between them, which would be the domain.

I don't think you understood what I was complaining about.  When I run 
this one-liner, using your regex, I get "/foo.com".  Wouldn't you want 
"foo.com"?  Using m|//(.+?)/| instead fixes that.

perl -e '$_ = q(http://foo.com/); print m|/(.+?)/|;'

(If you're on Windows, tr/'/"/ on the above.)

-- Gary Ansok


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

Date: Wed, 23 May 2001 11:33:36 -0700
From: "terminalsplash" <shino_korah@yahoo.com>
Subject: Writing file in Perl
Message-Id: <9egvq0$8rb@news.or.intel.com>

Hi

    I want to read and write to a file..
but open(FP,"+>$filename") doesn't seem to work
and more over when I tried to do
open(FP,">$filename")
nothing inside
close(FP);
my full data inside the file is gone.

What am I doing wrong?

Thanks




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

Date: Wed, 23 May 2001 19:03:16 GMT
From: peter@uboat.berghold.net (Peter L. Berghold)
Subject: Re: Writing file in Perl
Message-Id: <slrn9go2b6.ern.peter@uboat.berghold.net>

[*]What am I doing wrong?
[*]
I'm not sure because you haven't shown what it is you are doing to 
actually write to the file. 

I'll quote from perldoc -f open:

" You can put a '+' in front of the '>' or '<' to indicate that
you want both read and write access to the file; thus '+<'> is almost
always preferred for read/write updates--the '+>' mode would clobber the
file first.  You can't usually use either read-write mode for updating
textfiles, since they have variable length records.  See the -i
switch in perlrun for a better approach. "


-- 
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Peter L. Berghold                        Peter@Berghold.Net
Schooner Technology Consulting           http://www.berghold.net
Unix Professional Services    Perl Perl/CGI mod_perl 


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

Date: Wed, 23 May 2001 15:14:01 -0400
From: "Kurt Stephens" <kstep@pepsdesign.com>
Subject: Re: Writing file in Perl
Message-Id: <9eh25l$vfo$1@slb0.atl.mindspring.net>

"terminalsplash" <shino_korah@yahoo.com> wrote in message
news:9egvq0$8rb@news.or.intel.com...
> Hi
>
>     I want to read and write to a file..
> but open(FP,"+>$filename") doesn't seem to work
> and more over when I tried to do
> open(FP,">$filename")
> nothing inside
> close(FP);
> my full data inside the file is gone.

Oops!

>
> What am I doing wrong?

Read the following in perlfaq5: "How come when I open a file read-write it
wipes it out?"  This section goes on through many scenarios involving
reading from and writing to the same file.

In short, opening a file with ">" or "+>" clobbers the existing data,
replacing the current file with a new, empty file open for write (>) or
read-write (+>" access.  You probably want to use "+<", if read-write access
is really what you need.  If your file contains fixed length records or if
you only plan to write to the end of the file, this should work.  If the
file contains variable length text and you plan to write the data via random
access, you are probably better off using a temporary file.  As usual, the
FAQ goes into this in much greater detail.

HTH,

Kurt Stephens





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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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.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.

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 V10 Issue 971
**************************************


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