[24405] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6593 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 21 14:05:49 2004

Date: Fri, 21 May 2004 11:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 21 May 2004     Volume: 10 Number: 6593

Today's topics:
        * or x ? vjp2.at@at.BioStrategist.dot.dot.com
        2-1/2 regexp questions <jkrugman345@yahbitoo.com>
    Re: 2-1/2 regexp questions <ittyspam@yahoo.com>
    Re: 2-1/2 regexp questions <lallip@dishwasher.cs.rpi.edu>
    Re: 2-1/2 regexp questions <ittyspam@yahoo.com>
    Re: 2-1/2 regexp questions <nobull@mail.com>
        advice on backing up a DB from server? (dan baker)
    Re: advice on backing up a DB from server? <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: Called as CGI or from command line? ctcgag@hotmail.com
    Re: Called as CGI or from command line? <flavell@ph.gla.ac.uk>
    Re: How to find the target of a Unix symlink? <Juha.Laiho@iki.fi>
        How to pass input from perl to shell programming? (Swamy)
    Re: How to pass input from perl to shell programming? <spamtrap@dot-app.org>
    Re: How to pass input from perl to shell programming? <nobull@mail.com>
    Re: ISO two Perl idioms... <dwall@fastmail.fm>
    Re: ISO two Perl idioms... <uri@stemsystems.com>
    Re: ISO two Perl idioms... <dwall@fastmail.fm>
    Re: ISO two Perl idioms... <usenet@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 21 May 2004 17:56:33 +0000 (UTC)
From: vjp2.at@at.BioStrategist.dot.dot.com
Subject: * or x ?
Message-Id: <c8lfsh$g5f$1@reader2.panix.com>

* or X ??

perl -pe 'if ($.%4==2){$_.="-" x 37} elseif {$.%4==0) {$_.="=" x 37} $_.="\n"' < %1 >%1


I want to stick blank or (tribar) striped lines in between text





				- = -
    Vasos-Peter John Panagiotopoulos II, Columbia'81+, Bio$trategist
	      BachMozart ReaganQuayle EvrytanoKastorian
       http://ourworld.compuserve.com/homepages/vjp2/vasos.htm
  ---{Nothing herein constitutes advice.  Everything fully disclaimed.}---
   [Homeland Security means private firearms not lazy obstructive guards]
    [Health Reform means abolishing FDR's insurance tax exemption]
 [To stop SPAM, Charge net-postage] [Abolish 16th (Inc Tx) Amendment]


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

Date: Fri, 21 May 2004 16:36:55 +0000 (UTC)
From: J Krugman <jkrugman345@yahbitoo.com>
Subject: 2-1/2 regexp questions
Message-Id: <c8lb76$ea3$1@reader2.panix.com>




1. Supposed I wanted apply s/// at a particular (offset, length)
substring of a string (for example, applying s/(a[^a]+)/*\U$1*/ to the
(offset = 7, length = 3) substring in 'abracadabra', to get
'abracad*ABR*a').  I could use some labored code like this:

  $string = 'abracadabra';
  ($substring = substr($string, 7, 3)) =~ s/(a[^a]+)/;
  substr($string, 7, 3) = $substring;

This requires creating an auxiliary variable $substring and two calls
to substr, which seems like a waste.  Is there any way to target s///
to a particular substring in a string?

2. Given any finite string S and given any regexp R, there is a finite
set A of (o, w) pairs, such that the substring S[o, w] of S, beginning
at offset o and having length w matches R [1].  For example, if

  S = '1a21b4xy' and
  R = /(\d+)/,

then the pairs in the set A would be

  (0, 1)  --> '1'
  (2, 1)  --> '2'
  (2, 2)  --> '21'
  (3, 1)  --> '1'
  (5, 1)  --> '4'

Does Perl offer any simple and/or built-in way to generate the set
A?

2.5 There's a *different* problem, simpler for me to code than the one
described in (2): generate the set B of all pairs (length($PREMATCH),
length($MATCH)) generated during a "global" (i.e. /g-modified) match.
For example:

  while ($S =~ /(\d+)/g) {
    push @pairs, [ map length, ($`, $&) ];
  }

For the string S and the regexp R in the example given in question 2,
the set B would be { (0, 1), (2, 2), (5, 1) }.

Does Perl offer some built-in mechanism to get directly at the set B?

TIA,

jill


[1] If we changed R in (2) to the anchored regexp /^(\d+)/ we would
get the same set A as before, and not the set { (0, 1) }, because each
substring S[o, w] is tested against R in isolation.)
-- 
To  s&e^n]d  me  m~a}i]l  r%e*m?o\v[e  bit from my a|d)d:r{e:s]s.



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

Date: Fri, 21 May 2004 13:37:29 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: 2-1/2 regexp questions
Message-Id: <20040521133535.O382@dishwasher.cs.rpi.edu>

On Fri, 21 May 2004, J Krugman wrote:

> 1. Supposed I wanted apply s/// at a particular (offset, length)
> substring of a string (for example, applying s/(a[^a]+)/*\U$1*/ to the
> (offset = 7, length = 3) substring in 'abracadabra', to get
> 'abracad*ABR*a').  I could use some labored code like this:
>
>   $string = 'abracadabra';
>   ($substring = substr($string, 7, 3)) =~ s/(a[^a]+)/;
>   substr($string, 7, 3) = $substring;
>
> This requires creating an auxiliary variable $substring and two calls
> to substr, which seems like a waste.  Is there any way to target s///
> to a particular substring in a string?

This one's easy.

substr($string, 7, 3) =~ s/(a[^a]+)/;


The others... I'm gonna let someone else have a go.

Paul Lalli



>
> 2. Given any finite string S and given any regexp R, there is a finite
> set A of (o, w) pairs, such that the substring S[o, w] of S, beginning
> at offset o and having length w matches R [1].  For example, if
>
>   S = '1a21b4xy' and
>   R = /(\d+)/,
>
> then the pairs in the set A would be
>
>   (0, 1)  --> '1'
>   (2, 1)  --> '2'
>   (2, 2)  --> '21'
>   (3, 1)  --> '1'
>   (5, 1)  --> '4'
>
> Does Perl offer any simple and/or built-in way to generate the set
> A?
>
> 2.5 There's a *different* problem, simpler for me to code than the one
> described in (2): generate the set B of all pairs (length($PREMATCH),
> length($MATCH)) generated during a "global" (i.e. /g-modified) match.
> For example:
>
>   while ($S =~ /(\d+)/g) {
>     push @pairs, [ map length, ($`, $&) ];
>   }
>
> For the string S and the regexp R in the example given in question 2,
> the set B would be { (0, 1), (2, 2), (5, 1) }.
>
> Does Perl offer some built-in mechanism to get directly at the set B?
>
> TIA,
>
> jill
>
>
> [1] If we changed R in (2) to the anchored regexp /^(\d+)/ we would
> get the same set A as before, and not the set { (0, 1) }, because each
> substring S[o, w] is tested against R in isolation.)
> --
> To  s&e^n]d  me  m~a}i]l  r%e*m?o\v[e  bit from my a|d)d:r{e:s]s.
>
>


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

Date: Fri, 21 May 2004 13:42:52 -0400
From: Paul Lalli <lallip@dishwasher.cs.rpi.edu>
Subject: Re: 2-1/2 regexp questions
Message-Id: <20040521134056.X382@dishwasher.cs.rpi.edu>

On Fri, 21 May 2004, Paul Lalli wrote:

> On Fri, 21 May 2004, J Krugman wrote:
>
> > 1. Supposed I wanted apply s/// at a particular (offset, length)
> > substring of a string (for example, applying s/(a[^a]+)/*\U$1*/ to the
> > (offset = 7, length = 3) substring in 'abracadabra', to get
> > 'abracad*ABR*a').  I could use some labored code like this:
> >
> >   $string = 'abracadabra';
> >   ($substring = substr($string, 7, 3)) =~ s/(a[^a]+)/;
> >   substr($string, 7, 3) = $substring;
> >
> > This requires creating an auxiliary variable $substring and two calls
> > to substr, which seems like a waste.  Is there any way to target s///
> > to a particular substring in a string?
>
> This one's easy.
>
> substr($string, 7, 3) =~ s/(a[^a]+)/;

Bah.  I copy&pasted your typo...
substr($string, 7, 3) =~ s/(a[^a]+)/*\U$1*/;

Paul Lalli


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

Date: Fri, 21 May 2004 13:59:01 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: 2-1/2 regexp questions
Message-Id: <20040521135605.I382@dishwasher.cs.rpi.edu>

On Fri, 21 May 2004, J Krugman wrote:

> 2. Given any finite string S and given any regexp R, there is a finite
> set A of (o, w) pairs, such that the substring S[o, w] of S, beginning
> at offset o and having length w matches R [1].  For example, if
>
>   S = '1a21b4xy' and
>   R = /(\d+)/,
>
> then the pairs in the set A would be
>
>   (0, 1)  --> '1'
>   (2, 1)  --> '2'
>   (2, 2)  --> '21'
>   (3, 1)  --> '1'
>   (5, 1)  --> '4'
>
> Does Perl offer any simple and/or built-in way to generate the set
> A?
>
> 2.5 There's a *different* problem, simpler for me to code than the one
> described in (2): generate the set B of all pairs (length($PREMATCH),
> length($MATCH)) generated during a "global" (i.e. /g-modified) match.
> For example:
>
>   while ($S =~ /(\d+)/g) {
>     push @pairs, [ map length, ($`, $&) ];
>   }
>
> For the string S and the regexp R in the example given in question 2,
> the set B would be { (0, 1), (2, 2), (5, 1) }.
>
> Does Perl offer some built-in mechanism to get directly at the set B?

Still no idea how to go about 2, but you might be able to fiddle with my
solution to 2.5 to see what you can come up with....
$s = '1a21b4xy';
$r = qr/(\d+)/;
while ($s =~ /$r/g){
        $w = length($1);
        $o = pos ($s) - $w;
        print "($o, $w) --> $1\n";
}
__END__
(0, 1) --> 1
(2, 2) --> 21
(5, 1) --> 4

I don't know how you want to define built-in mechanism in this case, but
this seems quick enough to me.  (Your method is certainly short enough
too, but using the $`, $&, and $' variables is considered bad form for
mostly historical reasons...)

Paul Lalli


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

Date: 21 May 2004 18:57:53 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: 2-1/2 regexp questions
Message-Id: <u9k6z5slwe.fsf@wcl-l.bham.ac.uk>

J Krugman <jkrugman345@yahbitoo.com> writes:

> 1. Supposed I wanted apply s/// at a particular (offset, length)
> substring of a string (for example, applying s/(a[^a]+)/*\U$1*/ to the
> (offset = 7, length = 3) substring in 'abracadabra', to get
> 'abracad*ABR*a').  I could use some labored code like this:
> 
>   $string = 'abracadabra';
>   ($substring = substr($string, 7, 3)) =~ s/(a[^a]+)/;
>   substr($string, 7, 3) = $substring;
> 
> This requires creating an auxiliary variable $substring and two calls
> to substr, which seems like a waste.  Is there any way to target s///
> to a particular substring in a string?

Err.... just do it.

substr() is an lvalued funtion you can use it on the LHS of =~.

> 2. Given any finite string S and given any regexp R, there is a finite
> set A of (o, w) pairs, such that the substring S[o, w] of S, beginning
> at offset o and having length w matches R [1].

Please write in Perl.  (Have you seen the posting guidelines that are
posted frequently?)

I shall assume you means to say:

Given a string $S and a regexp $R there is a finite set of pairs $o,$w
such that substr($S,$o,$w)=~/\A$R\Z/ is true.


>  For example, if
> 
>   S = '1a21b4xy' and
>   R = /(\d+)/,

  my $S = '1a21b4xy';
  my $R = qr/\d+/;

> then the pairs in the set A would be
> 
>   (0, 1)  --> '1'
>   (2, 1)  --> '2'
>   (2, 2)  --> '21'
>   (3, 1)  --> '1'
>   (5, 1)  --> '4'
> 
> Does Perl offer any simple and/or built-in way to generate the set
> A?

If we ignore $w for a moment you can easily get the set of $o such
that substr($S,$o)=~/\A$R/ is true.

my @A;
while ( $S = /(?=$R)/g ) {
  push @A, [ pos($S), undef ];
}

To get the width it's a bit more involved

my @A;
{
    my @w;
    
    my $record_width = qr/(?{ push @w, length $1 })/;
    while ( $S =~ /(?=($R)$record_width)/g ) {
	push @A, map { [ pos($S), $_ ] } @w;
	@w=();
    }
}

For some reason I get an extra copy of [5,1] in @A.  I can't
immediately see why.

It also seems odd that whilst $1 seems to do the right thing inside
the (?{}) $+[1] does not.
 
> 2.5 There's a *different* problem, simpler for me to code than the one
> described in (2): generate the set B of all pairs (length($PREMATCH),
> length($MATCH)) generated during a "global" (i.e. /g-modified) match.
> For example:
> 
>   while ($S =~ /(\d+)/g) {
>     push @pairs, [ map length, ($`, $&) ];
>   }

You should not use length($PREMATCH), length($MATCH).  

Use $-[0] and $+[0]-$-[0] instead.

> Does Perl offer some built-in mechanism to get directly at the set B?

No.

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


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

Date: 21 May 2004 08:18:38 -0700
From: botfood@yahoo.com (dan baker)
Subject: advice on backing up a DB from server?
Message-Id: <13685ef8.0405210718.33c5e83b@posting.google.com>

I have a small database on a remote webserver that I would like to
back up daily. The host does NOT make backups, except by special
request and extra fees. I was thinking I could mirror either to my
localhost, or mail it to myself via email, or something... I would
appreciate some feedback as to what a good approach might be.

The host I use does allow the configuration of cron jobs, so I could
write a little perl script to fire up nightly on the remote host. I
just don't know what the easiet/best approach might be, or what
modules might make this a slam dunk.

I do have a localhost running at home that I use for testing, but I
dont know if it is possible to mirror file(s) from a domain on the web
to my localhost? I don't have a fixed IP, it is dynamically assigned
from my ISP.

I was thinking about the email approach, but wasn't sure what the
easiest way would be to attach the DB as a binary attachment and send
it to myself. Is this a decent way to go? The database is fairly small
(about 1MB), and doesnt get any activity at night.

d


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

Date: Fri, 21 May 2004 08:31:43 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: advice on backing up a DB from server?
Message-Id: <vc7l8c.elp.ln@goaway.wombat.san-francisco.ca.us>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

In article <13685ef8.0405210718.33c5e83b@posting.google.com>, dan baker wrote:
> I have a small database on a remote webserver that I would like to
> back up daily.
[snip]
> The host I use does allow the configuration of cron jobs, so I could
> write a little perl script to fire up nightly on the remote host. I
> just don't know what the easiet/best approach might be, or what
> modules might make this a slam dunk.

I have three pieces of advice:

1) Investigate the tools your database software has for backing up
your database.  They will certainly be helpful, even if you decide to
roll your own eventually.

2) Look into the DBI module and the DBD::* modules.

3) Don't post database questions to comp.lang.perl.misc.  If you end
up writing Perl code, and need help, then you might try this newsgroup
first.  But database questions are probably more appropriate somewhere
else.

- --keith

- -- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.2 (Darwin)

iD8DBQFAriDihVcNCxZ5ID8RAohUAJ95lt3/yfKypclnB9ohh4unj5n4zACePOYV
O0zwbORubrz1ap+/8ncRzqQ=
=rWkG
-----END PGP SIGNATURE-----


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

Date: 21 May 2004 14:33:37 GMT
From: ctcgag@hotmail.com
Subject: Re: Called as CGI or from command line?
Message-Id: <20040521103337.401$Se@newsreader.com>

"Alan J. Flavell" <flavell@ph.gla.ac.uk> wrote:
> On Fri, 21 May 2004, Anno Siegel wrote:
>
> > Environment-checking probably is the solution OP was looking for, but
> > it should be clear that nothing stops a command-line user from setting
> > those variables.
>
> Right
>
> > Their existence doesn't prove the program was called through
> > the CGI.
>
> That's a matter of terminology.  If it walks like a duck, and quacks
> like a duck...  If all the relevant requirements have been set up for
> the common gateway interface, then it -is- the common gateway
> interface.  What it really -doesn't- prove is that the program was
> invoked from a web server (HTTPD), which is presumably the real
> criterion that someone asking this question would have had in mind.
> But I know of no portable (i.e platform-independent) way of proving
> that conclusively.

Presumably, someone running Perl code from a CLI has access to the
code itself.  That being the case, they can do whatever they want
with the code, such as removing or reversing the CGI detection logic.
So if you are assuming a hostile environment, at this point you are
already screwed.  I think what the OP really wants to know is whether
the options should be gotten from CGI or from CL arguments, and whether
the output should be HTML or not.  If an authorized CLI user wishes to
go out of his way to trick the script into making life more difficult for
himself, I really couldn't care less, unless I'm paying for his time.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Fri, 21 May 2004 16:38:18 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: Called as CGI or from command line?
Message-Id: <Pine.LNX.4.53.0405211629070.29041@ppepc56.ph.gla.ac.uk>

On Fri, 21 May 2004 ctcgag@hotmail.com wrote:

> Presumably, someone running Perl code from a CLI has access to the
> code itself.  That being the case, they can do whatever they want
> with the code, such as removing or reversing the CGI detection logic.
> So if you are assuming a hostile environment, at this point you are
> already screwed.

Well, I was thinking more in terms of someone who is trying to design
a portable bi-modal script, which will behave in one way when invoked
from an HTTPD via the CGI (common gateway interface), and in a
different way when invoked from a user call.

I wasn't imputing "hostility" on the part of its user, but rather,
asking what they would want to happen if they, as a user, deliberately
synthesised a "common gateway interface" environment for the script
(for example, in order to debug the way in which it would behave when
run from an HTTPD via the CGI).

If you decided to base your test on the presence of a TTY, for
example, then you'd defeat the ability of the user to do that.  Which
is why I was asking to think about the real purpose of this test.

> I think what the OP really wants to know is whether
> the options should be gotten from CGI or from CL arguments,

agreed

> and whether the output should be HTML or not.

Not necessarily!  Some CGI scripts are designed to return JPEG, or
even plain text.  Maybe you meant to say "whether the output needs to
begin with CGI response header(s) or not" ?

> If an authorized CLI user wishes to go out of his way to trick the
> script into making life more difficult for himself, I really
> couldn't care less,

Sure.  But I don't think that was really the point that I was trying
to make.  Sorry for any confusion caused.


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

Date: Fri, 21 May 2004 17:32:02 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: How to find the target of a Unix symlink?
Message-Id: <c8lecn$e0l$4@ichaos.ichaos-int>

Ben Morrow <usenet@morrow.me.uk> said:
>sub readalllinks {
>    my $file = shift;
>    while (-l $file) {
>        $file = readlink $file;
>    }
>}

That code begs to create two symlinks pointing to each other (or just
a single symlink pointing to itself).
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)


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

Date: 21 May 2004 09:46:33 -0700
From: swamyb@hotmail.com (Swamy)
Subject: How to pass input from perl to shell programming?
Message-Id: <1aaaeaf0.0405210846.302f71dd@posting.google.com>

I have a perl program which executes a shell program using system
call. This shell program needs user input for several questions it
asks. How can I automate the user input so that user don't have to key
in the input?

thanks
Swamy


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

Date: Fri, 21 May 2004 13:23:45 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: How to pass input from perl to shell programming?
Message-Id: <JvmdncCd5_2_pjPdRVn-tA@adelphia.com>

Swamy wrote:

> I have a perl program which executes a shell program using system
> call. This shell program needs user input for several questions it
> asks. How can I automate the user input so that user don't have to key
> in the input?

Have a look at the Expect module on CPAN.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: 21 May 2004 18:58:28 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: How to pass input from perl to shell programming?
Message-Id: <u9fz9tslvf.fsf@wcl-l.bham.ac.uk>

swamyb@hotmail.com (Swamy) writes:

> I have a perl program which executes a shell program using system
> call. This shell program needs user input for several questions it
> asks. How can I automate the user input so that user don't have to key
> in the input?

I Expect there's something on CPAN.
  ^^^^^^

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


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

Date: Fri, 21 May 2004 14:12:01 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: ISO two Perl idioms...
Message-Id: <Xns94F067C32B7E8dkwwashere@216.168.3.30>

Uri Guttman <uri@stemsystems.com> wrote:

> i read the other posts and none seem to use subs which make for a
> great flow control mechanisim. and i don't like to use boolean
> flags for loop stuff if i can help it that it reeks of fortran and
> spaghetti code. 
> 
>      sub skip_until_condition {
>           my( $fh, $cond_code) = @_ ;
> 
>           while( <$fh> ) {
>                return if $cond_code->($_) ;
>          }
> 
>           die "lousy file i just read" ;
>     }
> 
> now in the main code you just do:
> 
>      skip_until_condition( \*IN, \&meets_condition ) ;
> 
>      while( <IN> ) {
> 
>           ...
>     }
> 
> clean and easy to understand. the lesson is that return is a flow
> control op too and that subs are your friend.

OK, here's my original solution. I thought maybe it was overkill, so 
I dropped the sub before posting.  :-)

    sub read_until {
        my ($filehandle, $condition) = @_;
        while (<$filehandle>) {
            last if $condition->($_);
        }
        return $_;
    }
    
    my $found = read_until( \*DATA, sub { ... } );
    die "Nothing found!" unless $found;

I like returning the line that meets the condition, because it defers 
the choice of what to do with it. 


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

Date: Fri, 21 May 2004 14:42:01 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: ISO two Perl idioms...
Message-Id: <x7hdu9suyu.fsf@mail.sysarch.com>

>>>>> "DKW" == David K Wall <dwall@fastmail.fm> writes:

  DKW> Uri Guttman <uri@stemsystems.com> wrote:
  >> 
  >> sub skip_until_condition {
  >> my( $fh, $cond_code) = @_ ;
  >> 
  >> while( <$fh> ) {
  >> return if $cond_code->($_) ;
  >> }
  >> 
  >> die "lousy file i just read" ;
  >> }

  DKW> OK, here's my original solution. I thought maybe it was overkill, so 
  DKW> I dropped the sub before posting.  :-)

  DKW>     sub read_until {
  DKW>         my ($filehandle, $condition) = @_;
  DKW>         while (<$filehandle>) {
  DKW>             last if $condition->($_);

		return $_ if $condition->($_);

  DKW>         }
  DKW>         return $_;

		return ;

  DKW>     }
    
  DKW>     my $found = read_until( \*DATA, sub { ... } );
  DKW>     die "Nothing found!" unless $found;
                                      ^defined

what if the last line was '0' without a newline? that is the classic
(but very rare) gotcha of while(<>) in old perls (fixed with the implied
defined wrapper).

  DKW> I like returning the line that meets the condition, because it defers 
  DKW> the choice of what to do with it. 

that is ok but the OP never spec'ed it that way. he made it sound like
that line is just a marker and has no significant data.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Fri, 21 May 2004 15:08:44 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: ISO two Perl idioms...
Message-Id: <Xns94F071613AC2Fdkwwashere@216.168.3.30>

Uri Guttman <uri@stemsystems.com> wrote:

>>>>>> "DKW" == David K Wall <dwall@fastmail.fm> writes:
> 
>   DKW> OK, here's my original solution. I thought maybe it was
>   overkill, so DKW> I dropped the sub before posting.  :-)
> 
>   DKW>     sub read_until {
>   DKW>         my ($filehandle, $condition) = @_;
>   DKW>         while (<$filehandle>) {
>   DKW>             last if $condition->($_);
> 
>           return $_ if $condition->($_);
> 
>   DKW>         }
>   DKW>         return $_;
> 
>           return ;
> 
>   DKW>     }
>     
>   DKW>     my $found = read_until( \*DATA, sub { ... } );
>   DKW>     die "Nothing found!" unless $found;
>                                       ^defined
> 
> what if the last line was '0' without a newline? 

Then there's a bug that might be hard to find. Oops. Bummer. I liked 
that feature. It's easy enough to return a boolean AND the line, but 
that's getting too crufty for something simple.

> that is the
> classic (but very rare) gotcha of while(<>) in old perls (fixed
> with the implied defined wrapper).
> 
>   DKW> I like returning the line that meets the condition, because
>   it defers DKW> the choice of what to do with it. 
> 
> that is ok but the OP never spec'ed it that way. he made it sound
> like that line is just a marker and has no significant data.

Sure. I was just putting my own preferences into it. 


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

Date: Fri, 21 May 2004 15:11:40 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: ISO two Perl idioms...
Message-Id: <c8l67c$61k$1@wisteria.csv.warwick.ac.uk>


Quoth "David K. Wall" <dwall@fastmail.fm>:
> Uri Guttman <uri@stemsystems.com> wrote:
> 
> OK, here's my original solution. I thought maybe it was overkill, so 
> I dropped the sub before posting.  :-)
> 
>     sub read_until {
>         my ($filehandle, $condition) = @_;
>         while (<$filehandle>) {
>             last if $condition->($_);
>         }
>         return $_;
>     }
>     
>     my $found = read_until( \*DATA, sub { ... } );
>     die "Nothing found!" unless $found;
> 
> I like returning the line that meets the condition, because it defers 
> the choice of what to do with it. 

Purely as a matter of style, I'd write it as

use Symbol;

sub read_until (&*) {
    my ($cond, $FH) = @_;
    local $_;
    $FH = Symbol::qualify_to_ref $FH, caller;
    $FH ||= \*ARGV;
    while (<$FH>) {
        last if $cond->($_);
    }
    return $_;
}

so you can call it as

my $found = read_until { ... } DATA;

; and I'd probably add a hash of params on the end so you could set $/
locally, croak with a given message if the condition wasn't met, etc.

Ben

-- 
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
  ben@morrow.me.uk                                                 (Kate Rusby)


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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