[24453] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6636 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 31 21:05:45 2004

Date: Mon, 31 May 2004 18: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           Mon, 31 May 2004     Volume: 10 Number: 6636

Today's topics:
    Re: (?{ ... }) puzzlement <matthew.garrish@sympatico.ca>
    Re: (?{ ... }) puzzlement <matthew.garrish@sympatico.ca>
    Re: Am I a programmer or a scripter? <tadmc@augustmail.com>
    Re: Am I a programmer or a scripter? <postmaster@castleamber.com>
    Re: Am I a programmer or a scripter? <postmaster@castleamber.com>
    Re: Am I a programmer or a scripter? <postmaster@castleamber.com>
    Re: Am I a programmer or a scripter? <flavell@ph.gla.ac.uk>
    Re: Am I a programmer or a scripter? <usenet@morrow.me.uk>
    Re: Am I a programmer or a scripter? <uri@stemsystems.com>
    Re: Beginner needs help with script :) <matthew.garrish@sympatico.ca>
    Re: Cookies <tadmc@augustmail.com>
    Re: Cookies <uri@stemsystems.com>
    Re: decode_entities possible bug? <usenet@morrow.me.uk>
    Re: decode_entities possible bug? <gnari@simnet.is>
        Perl Installation on Darwin (Jim Keenan)
    Re: Perl Installation on Darwin <usenet@morrow.me.uk>
    Re: Perl Installation on Darwin <kalinaubears@iinet.net.au>
    Re: reading variable and fixed length records <uri@stemsystems.com>
        Reference to loop index in a closure <wksmith@optonline.net>
    Re: Reference to loop index in a closure <1usa@llenroc.ude>
    Re: Reference to loop index in a closure <usenet@morrow.me.uk>
    Re: Reference to loop index in a closure <1usa@llenroc.ude>
    Re: Reference to loop index in a closure <gnari@simnet.is>
        Spawning external programs and running system commands  (Sol Linderstein)
    Re: Spawning external programs and running system comma <1usa@llenroc.ude>
    Re: Spawning external programs and running system comma <kevin@vaildc.net>
    Re: Spawning external programs and running system comma <gnari@simnet.is>
    Re: splitting cvs file and insert in mysql via DBI <tadmc@augustmail.com>
    Re: splitting cvs file and insert in mysql via DBI <javier@t-online.de>
    Re: splitting cvs file and insert in mysql via DBI <usenet@morrow.me.uk>
    Re: splitting cvs file and insert in mysql via DBI <gnari@simnet.is>
    Re: splitting cvs file and insert in mysql via DBI <1usa@llenroc.ude>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 31 May 2004 19:00:34 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: (?{ ... }) puzzlement
Message-Id: <9OOuc.84348$tb4.3318772@news20.bellglobal.com>


"J Krugman" <jkrugman345@yahbitoo.com> wrote in message
news:c9f8on$no6$1@reader2.panix.com...
>
>
>
> In an attempt to find a single regexp that would succeed if three
> different sub-regexps matched in any order (see why in the thread
> called '"Commutative" regexps'), I started playing with (?{...})-type
> regexps.  As warm-up, I tried this:
>
>      1  use strict;
>      2  use re 'eval';
>      3
>      4  my @re0  = qw(abc pqr xyz);
>      5  my @seen = (undef) x @re0;
>      6  my @re   = map sprintf('%s(?{ $seen[%d] ||= "@-" })',

I think you want ?? not ?

my @re   = map sprintf('%s(??{ $seen[%d] ||= "@-" })',


>      7                         $re0[$_], $_),
>      8                 0..$#re0;
>      9  my $re   = eval "qr/@{[join('|', @re)]}/";
>     10
>     11  #0         1         2
>     12  #01234567890123456789012345
>     13  '__pqr____xyz__pqr___abc___' =~ /(($re).*?)*/;
>     14
>     15  print "\$seen[$_] = $seen[$_]\n" for (0..$#seen);
>     16
>     17  __END__
>

The output then becomes

$seen[0] = 20
 $seen[1] = 2
 $seen[2] = 9

Matt




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

Date: Mon, 31 May 2004 19:05:48 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: (?{ ... }) puzzlement
Message-Id: <3TOuc.84423$tb4.3320781@news20.bellglobal.com>


"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:9OOuc.84348$tb4.3318772@news20.bellglobal.com...
>
> "J Krugman" <jkrugman345@yahbitoo.com> wrote in message
> news:c9f8on$no6$1@reader2.panix.com...
> >
> >
> >
> > In an attempt to find a single regexp that would succeed if three
> > different sub-regexps matched in any order (see why in the thread
> > called '"Commutative" regexps'), I started playing with (?{...})-type
> > regexps.  As warm-up, I tried this:
> >
> >      1  use strict;
> >      2  use re 'eval';
> >      3
> >      4  my @re0  = qw(abc pqr xyz);
> >      5  my @seen = (undef) x @re0;
> >      6  my @re   = map sprintf('%s(?{ $seen[%d] ||= "@-" })',
>
> I think you want ?? not ?
>
> my @re   = map sprintf('%s(??{ $seen[%d] ||= "@-" })',
>
>
> >      7                         $re0[$_], $_),
> >      8                 0..$#re0;
> >      9  my $re   = eval "qr/@{[join('|', @re)]}/";
> >     10
> >     11  #0         1         2
> >     12  #01234567890123456789012345
> >     13  '__pqr____xyz__pqr___abc___' =~ /(($re).*?)*/;
> >     14
> >     15  print "\$seen[$_] = $seen[$_]\n" for (0..$#seen);
> >     16
> >     17  __END__
> >
>
> The output then becomes
>
> $seen[0] = 20
>  $seen[1] = 2
>  $seen[2] = 9
>

Forgot to mention that I also changed line 13 to:

'__pqr____xyz__pqr___abc___' =~ /(($re).*?)*(?!)/;

as per your original post.

Matt




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

Date: Mon, 31 May 2004 16:56:27 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <slrncbnagb.hv7.tadmc@magna.augustmail.com>

John Bokma <postmaster@castleamber.com> wrote:
> Anno Siegel wrote:
>> Tad McClellan  <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
>>>krakle <krakle@visto.com> wrote:
>>>
>>>
>>>>Well a lot of folks consider an interputed language scripting and a
                                    ^^^^^^^^^^

It's Archie Bunkerisms earn a smirk everytime it does it.


>>>>language that needs to be compiled programming.
>>>
>>>
>>>Perl is compiled.
>> 
>> ...and interputed!
> 
> So we are script programmers :-D. We should be paid extra since we do 
> two things at the same time ;-)


You mean _you_ aren't being paid extra already?

We all think that that is a shame.

:-)


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Mon, 31 May 2004 17:52:02 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <40bbb712$0$202$58c7af7e@news.kabelfoon.nl>

Tad McClellan wrote:

> John Bokma <postmaster@castleamber.com> wrote:

[snip]

> You mean _you_ aren't being paid extra already?

No, *snif*. Actually I have a hard time finding a nice Perl project :-(

> We all think that that is a shame.

Thanks!

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/


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

Date: Mon, 31 May 2004 17:53:52 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <40bbb780$0$202$58c7af7e@news.kabelfoon.nl>

Walter Roberson wrote:

> In article <237aaff8.0405311044.449ed953@posting.google.com>,
> krakle <krakle@visto.com> wrote:
> :Well a lot of folks consider an interputed language scripting and a
> :language that needs to be compiled programming.
> 
> Yet you never (or rarely) hear of "LISP scripting" or even of
> "FORTH scripting".

LOL. How does one script a Forth Processor ;-). IIRC there is even LISP 
in silicon :-D.

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/


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

Date: Mon, 31 May 2004 17:55:51 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <40bbb7f8$0$202$58c7af7e@news.kabelfoon.nl>

Tad McClellan wrote:

> krakle <krakle@visto.com> wrote:
> 
> 
>>Well a lot of folks consider an interputed language scripting and a
>>language that needs to be compiled programming.
> 
> Perl is compiled.

Java is interpreted on my computer. So am I a Java scripter? Oh, wait, 
JavaScript is a different language. Funny how the same work I do can 
change from scripting to programming by moving my program to a different 
platform.

A programmer is one who is able to write a script ;-)

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/


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

Date: Tue, 1 Jun 2004 00:04:35 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <Pine.LNX.4.53.0405312359590.31275@ppepc56.ph.gla.ac.uk>

On Mon, 31 May 2004, John Bokma wrote:

> A programmer is one who is able to write a script ;-)

Many a true word is spoken in jest.

On my "first computer" (which was already obsolete when I met it in
1958), the real experts couldn't be bothered to print out the answer -
they just walked over and read it off the dekatrons.


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

Date: Mon, 31 May 2004 23:31:01 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <c9gf7l$5ir$3@wisteria.csv.warwick.ac.uk>


Quoth John Bokma <postmaster@castleamber.com>:
> Tad McClellan wrote:
> 
> > krakle <krakle@visto.com> wrote:
> > 
> >>Well a lot of folks consider an interputed language scripting and a
> >>language that needs to be compiled programming.
> > 
> > Perl is compiled.
> 
> Java is interpreted on my computer.

All programs are ultimately interpreted, even if that is machine code
interpreted in hardware (or, in the case of Pentiums, JIT compiled to a
lower level of machine code... :). Some languages are compiled (into
another language) before interpretation, some are not.

FWIW, I'd say the difference between a program and a script is that a
script is simply a (relatively) trivial automation of something one
could do by hand whereas a program is more complex than that. Language is
irrelevant.

Ben

-- 
And if you wanna make sense / Whatcha looking at me for?          (Fiona Apple)
                            * ben@morrow.me.uk *


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

Date: Tue, 01 Jun 2004 00:36:53 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Am I a programmer or a scripter?
Message-Id: <x7brk46rmj.fsf@mail.sysarch.com>

>>>>> "JB" == John Bokma <postmaster@castleamber.com> writes:

  JB> Anno Siegel wrote:
  >> Tad McClellan  <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
  >> 
  >>> krakle <krakle@visto.com> wrote:
  >>> 
  >>> 
  >>>> Well a lot of folks consider an interputed language scripting and a
  >>>> language that needs to be compiled programming.
  >>> 
  >>> 
  >>> Perl is compiled.
  >> ...and interputed!

  JB> So we are script programmers :-D. We should be paid extra since we do
  JB> two things at the same time ;-)

i are a software engineer. i don't program. :) i can analyze, design,
archititect, document, refactor, disparage, insult, consult, praise,
shred, code and debug.

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: Mon, 31 May 2004 18:26:27 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Beginner needs help with script :)
Message-Id: <aiOuc.83513$tb4.3303979@news20.bellglobal.com>


"Player" <notachance@inhell.com> wrote in message
news:c9fiv2$3pb$1@news8.svr.pol.co.uk...
>
> Obviously not, do not speak down to me like I am a child sir, because I am
> far from that, and if nothing else I will not have you trying to play
school
> headmaster with me.
>

Oh please, spare the BS. Your writing is terrible and you're pouting like a
child. High school may seem grown up to you, but you're not fooling anyone.
Now do the sensible thing and stop whining about your wounded "pride" before
you're killfiled by everyone in the newsgroup. You acted like a twit and got
treated like one. Live with it!

Matt

Grammar tip of the day: "to know" is a verb; "to no" is not.




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

Date: Mon, 31 May 2004 17:13:43 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Cookies
Message-Id: <slrncbnbgn.hv7.tadmc@magna.augustmail.com>

dave h <daveharney@wi.rr.com> wrote:

> Thanks Gunnar and sorry if I offended anyone with my question.  


I doubt that you have offended anyone (with your question).

(krakle/Pinocchio does not count, as he is a troll and apparently 
 not a candidate for Mensa membership either. You can safely 
 discount whatever it says.
)


> I appreciate the
> time you have taken to help me.


You can pay Gunnar back by learning to properly format followups.

Have you seen the Posting Guidelines that are posted here frequently?



[ snip TOFU ]

-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Tue, 01 Jun 2004 00:39:00 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Cookies
Message-Id: <x78yf86riz.fsf@mail.sysarch.com>

>>>>> "TM" == Tad McClellan <tadmc@augustmail.com> writes:

  TM> dave h <daveharney@wi.rr.com> wrote:
  >> Thanks Gunnar and sorry if I offended anyone with my question.  

  TM> I doubt that you have offended anyone (with your question).

  TM> (krakle/Pinocchio does not count, as he is a troll and apparently
  TM> not a candidate for Mensa membership either. You can safely
  TM> discount whatever it says.  )

he could join the mensa chapter headed by bush! they have very low
standards for membership.

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: Mon, 31 May 2004 23:22:09 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: decode_entities possible bug?
Message-Id: <c9gen1$5ir$1@wisteria.csv.warwick.ac.uk>


Quoth "gnari" <gnari@simnet.is>:
> [removed non-existing comp.lang.perl from Newsgroups: line]
> 
> "Richard Bell" <rbell01824@earthlink.net> wrote in message
> news:40BB9A36.1030200@earthlink.net...
> >     I assume something along the lines of s/&#([0..9]){1,4}/ / will
> > turn them all into ' ' but had hoped for something a bit better as there
> > is some useful semantics amongst the rubble.
> 
> you want the terminating ';' too
>   s/&#[0-9]+;/ /
> and if i remember correctly, they can be in hex too (&#x12ac;)
>   s/&#x?[0-9a-f]+;/ /i

If you just want to get rid of them then

s/&.*?;/ /

will do fine.

Ben


-- 
  The cosmos, at best, is like a rubbish heap scattered at random.
                                                         - Heraclitus
  ben@morrow.me.uk


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

Date: Mon, 31 May 2004 23:30:57 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: decode_entities possible bug?
Message-Id: <c9gf4a$rod$1@news.simnet.is>

"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:c9gen1$5ir$1@wisteria.csv.warwick.ac.uk...
>
> Quoth "gnari" <gnari@simnet.is>:
> > [removed non-existing comp.lang.perl from Newsgroups: line]
> >
> > "Richard Bell" <rbell01824@earthlink.net> wrote in message
> > news:40BB9A36.1030200@earthlink.net...
> > >     I assume something along the lines of s/&#([0..9]){1,4}/ / will
> > > turn them all into ' ' but had hoped for something a bit better as
there
> > > is some useful semantics amongst the rubble.
> >
> > you want the terminating ';' too
> >   s/&#[0-9]+;/ /
> > and if i remember correctly, they can be in hex too (&#x12ac;)
> >   s/&#x?[0-9a-f]+;/ /i
>
> If you just want to get rid of them then
>
> s/&.*?;/ /
>
> will do fine.

maybe he needs &amp; &lt; and friends. lets compromise

  s/&#.*?;/ /

:-)

gnari






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

Date: 31 May 2004 16:24:35 -0700
From: jkeen_via_google@yahoo.com (Jim Keenan)
Subject: Perl Installation on Darwin
Message-Id: <196cb7af.0405311524.3055020a@posting.google.com>

I'm just beginning to explore Mac OS X.  I note that my iBook came
with Perl 5.8.1 installed, but I've heard that it's not a complete
installation.  I've tried to install CPAN modules only to get messages
suggesting I do a clean build.  One thing is evident:  the Perl
directory hierarchy is considerably different from what I've
encountered on Windows (ActivePerl) and Linux (Redhat 7.2 came with
5.6.0).

Since I've never built Perl from source before, I'm wondering:  Has
anyone written a primer on installation of Perl on Darwin, i.e., Perl
on Darwin for Dummies (or, at least, for Beginners)?

Jim Keenan


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

Date: Mon, 31 May 2004 23:42:56 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Perl Installation on Darwin
Message-Id: <c9gfu0$5ir$5@wisteria.csv.warwick.ac.uk>


Quoth jkeen_via_google@yahoo.com (Jim Keenan):
> I'm just beginning to explore Mac OS X.  I note that my iBook came
> with Perl 5.8.1 installed, but I've heard that it's not a complete
> installation.  I've tried to install CPAN modules only to get messages
> suggesting I do a clean build.  One thing is evident:  the Perl
> directory hierarchy is considerably different from what I've
> encountered on Windows (ActivePerl) and Linux (Redhat 7.2 came with
> 5.6.0).
> 
> Since I've never built Perl from source before, I'm wondering:  Has
> anyone written a primer on installation of Perl on Darwin, i.e., Perl
> on Darwin for Dummies (or, at least, for Beginners)?

I'm not really an OSX user, but I have perfectly successfully built perl
in the usual manner (install gcc etc. from the Apple CDs, download
perl-5.8.x.tar.gz from CPAN, untar, ./Configure; make; make install) on
an OSX box.

Ben

-- 
And if you wanna make sense / Whatcha looking at me for?          (Fiona Apple)
                            * ben@morrow.me.uk *


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

Date: Tue, 01 Jun 2004 10:53:33 +1000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: Perl Installation on Darwin
Message-Id: <40bbd4b7$0$8099$5a62ac22@freenews.iinet.net.au>

Jim Keenan wrote:
> I'm just beginning to explore Mac OS X.  I note that my iBook came
> with Perl 5.8.1 installed, but I've heard that it's not a complete
> installation.  I've tried to install CPAN modules only to get messages
> suggesting I do a clean build.  One thing is evident:  the Perl
> directory hierarchy is considerably different from what I've
> encountered on Windows (ActivePerl) and Linux (Redhat 7.2 came with
> 5.6.0).
> 
> Since I've never built Perl from source before, I'm wondering:  Has
> anyone written a primer on installation of Perl on Darwin, i.e., Perl
> on Darwin for Dummies (or, at least, for Beginners)?
> 

Just to point out (in case you're unaware of it) that the perl source 
contains a file called 'README.macosx' which would be worth reading 
before you build.

Cheers,
Rob

To reply by email u have to take out the u in kalinaubears.



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

Date: Tue, 01 Jun 2004 00:34:30 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: reading variable and fixed length records
Message-Id: <x7ekp06rqh.fsf@mail.sysarch.com>

>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:

  AS> Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
  >> >>>>> "b" == bof <nothingread@hotmail.com> writes:
  >> 
  >> >> Achieve what? Your description is not very clear.
  >> 
  b> Apologies, it's not what you read it to be . . .
  >>
  b> In terms of byte values the file might contain:
  >>
  b> Hex: 41,42,43,00,FF,00,80,5A,59,58,57,56,55,54,00,00,80,FF val:
  b> |---vv---|mm|---ff---|---------VV---------|MM|---FF---| rec:
  b> |-------record 1-----|-----------RECORD 2-------------|
  >>
  b> where:
  >>
  b> Hex: is the hex data
  >>
  b> val: "vv/VV" variable length data, "mm/MM" end of variable length
  b> marker, "ff/FF" fixed length data; lower/upper case differentiating
  b> two adjacent records.
  >>  when using wacko formats like this (or any file format, really)
  >> you must provide a proper spec like this. your first post was sorta
  >> clear to me but this is much better.
  >> 
  >> now munging with $/ on the fly is not something i would like to
  >> see. that idea would work but just makes me feel nasty. i have done
  >> similar stuff and it is better to just read in blocks of data and
  >> scan for records yourself. here is very untested code:

  AS> [code snipped]

  AS> The price is that you have to do your own input-buffering.

so what? make the buffer size whatever you want. or use read for
buffered i/o and use a different user level buffer.

  AS> Alternatively set $/ = "\0" for good.  Then each chunk read from
  AS> the file contains the fixed part of the last record and the
  AS> variable part of the next one, except for the first chunk.  Taking
  AS> this into account in the loop isn't too hard:

  AS>     $/ = "\0";

localize that at least.

  AS>     my ( $var_part, $fixed_part);
  AS>     while ( <$f> ) {
  AS>         chomp;
  AS>         unless ( $. == 1 ) {
  AS>             $fixed_part = substr( $_, 0, 2, ''); # extract and delete

that was for previous record

  AS>             print "fix: $fixed_part, var: $var_part\n";
  AS>         }
  AS>         $var_part = $_; # ...of next record

of this record that you just read in. you haven't read in the fixed part yet.

  AS>     }

i don't like the overlap of the record handling. since i have no problem
with doing my own buffering (old hat to me) i like my way better. :)

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: Mon, 31 May 2004 22:15:01 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Reference to loop index in a closure
Message-Id: <F7Ouc.34403$DC1.6921175@news4.srv.hcvlny.cv.net>

My "Case 1"  creates an array of references to anonymous subroutines.
Each subroutine is intended to print the value of the loop index ($i)
when it (the subroutine) was created.  One  of the subroutines  is
executed to demonstrate that the wrong value is printed. (The index is
never 3)

"Case 2"  is an experimental version which solves the problem by using
another variable  ($j) as the loop index
and making $i a lexical variable within the scope of the loop.

use strict;
use warnings;

my @ref1;

for (my $i = 0; $i < 3; $i++){
 $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
}
&{$ref1[1]};


for (my $j = 0; $j < 3; $j++){
 my $i = $j;
 $ref1[$i] = sub{print "Case 2 = $i OK\n"};
}
&{$ref1[1]};
__END__

What is wrong with "Case 1"?  I first encountered the problem in a Tk
application and wrote this case as a aide to solving it.  The successful
"Case 2" was found by trial and error.  Both appear to conform to
section of "Programming Perl" on closures.

Thanks,
Bill




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

Date: 31 May 2004 23:18:30 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: Reference to loop index in a closure
Message-Id: <Xns94FAC46ACBCDasu1cornelledu@132.236.56.8>

"Bill Smith" <wksmith@optonline.net> wrote in news:F7Ouc.34403$DC1.6921175
@news4.srv.hcvlny.cv.net:

> My "Case 1"  creates an array of references to anonymous subroutines.
> Each subroutine is intended to print the value of the loop index ($i)
> when it (the subroutine) was created.  One  of the subroutines  is
> executed to demonstrate that the wrong value is printed. (The index is
> never 3)

I am going to rearrange your post a little bit. Here is your case 1:

> use strict;
> use warnings;
> 
> my @ref1;
> 
> for (my $i = 0; $i < 3; $i++){
>    $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> }
> &{$ref1[1]};

I do not understand why you would ever expect the index to be 3. OTOH, when 
I ran your script unmodified on my system, it produced the following 
output:

C:\Home\asu1> perl -v

This is perl, v5.8.3 built for MSWin32-x86-multi-thread
(with 8 registered patches, see perl -V for more detail)

 ...

C:\Home\asu1> tttt
Case 1 = 3 WRONG

This puzzled me as well. Lacking sufficient knowledge, I am unsure how to 
explain this, but I would have done the following:

use strict;
use warnings;

my @ref1;

$ref1[$_] = index_gen($_) for (0 .. 2);

$_->() for @ref1;

sub index_gen {
    my $index = shift;
    return sub { print "$index\n"; }
}

__END__

Which generates the output (in line with my expectations):

C:\Home\asu1> tttt
0
1
2




-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


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

Date: Mon, 31 May 2004 23:40:26 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Reference to loop index in a closure
Message-Id: <c9gfpa$5ir$4@wisteria.csv.warwick.ac.uk>


Quoth "A. Sinan Unur" <1usa@llenroc.ude>:
> "Bill Smith" <wksmith@optonline.net> wrote in news:F7Ouc.34403$DC1.6921175
> @news4.srv.hcvlny.cv.net:
> 
> > My "Case 1"  creates an array of references to anonymous subroutines.
> > Each subroutine is intended to print the value of the loop index ($i)
> > when it (the subroutine) was created.  One  of the subroutines  is
> > executed to demonstrate that the wrong value is printed. (The index is
> > never 3)
> 
> I am going to rearrange your post a little bit. Here is your case 1:
> 
> > use strict;
> > use warnings;
> > 
> > my @ref1;
> > 
> > for (my $i = 0; $i < 3; $i++){
> >    $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> > }
> > &{$ref1[1]};
> 
> I do not understand why you would ever expect the index to be 3. OTOH, when 
> I ran your script unmodified on my system, it produced the following 
> output:
> 
> C:\Home\asu1> perl -v
> 
> This is perl, v5.8.3 built for MSWin32-x86-multi-thread
> (with 8 registered patches, see perl -V for more detail)
> 
> ...
> 
> C:\Home\asu1> tttt
> Case 1 = 3 WRONG
> 
> This puzzled me as well.

The variable $i is only declared once (the first section of a C-style
loop is only executed once); hence there is only one variable. All the
subs refer to the same variable $i, so whatever you do they will always
print the same thing.

The loop test is '$i < 3'; the loop will continue until that is false.
At that point, $i == 3, and remains so for the rest of the program. That
is why the subs will all print 3.

The following may help:

my @subs;
for (my $i = 0; $i < 3; $i++) {
    push @subs, sub {
        print "BEFORE: \$i is now $i\n";
        $i++;
        print "AFTER:  \$i is now $i\n";
    };
}

$_->() for @subs;

> use strict;
> use warnings;
> 
> my @ref1;
> 
> $ref1[$_] = index_gen($_) for (0 .. 2);
> 
> $_->() for @ref1;
> 
> sub index_gen {
>     my $index = shift;

This line (and the 'my $i = $j' in the OP's solution) is crucial: it
creates a new variable each time through the loop, so each sub gets a
different variable in its lexical pad. This is (usually) what you want.

Note that a Perl-style loop like

for my $i (0..2) {

will also create a new copy of $i each time through, unlike the C-style
which only has one copy. This is yet another reason to prefer Perl-style
for loops :).

Ben

-- 
        I must not fear. Fear is the mind-killer. I will face my fear and
        I will let it pass through me. When the fear is gone there will be 
        nothing. Only I will remain.
ben@morrow.me.uk                                          Frank Herbert, 'Dune'


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

Date: 31 May 2004 23:48:25 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: Reference to loop index in a closure
Message-Id: <Xns94FAC97D4FDBEasu1cornelledu@132.236.56.8>

Ben Morrow <usenet@morrow.me.uk> wrote in
news:c9gfpa$5ir$4@wisteria.csv.warwick.ac.uk: 

> Quoth "A. Sinan Unur" <1usa@llenroc.ude>:
>> "Bill Smith" <wksmith@optonline.net> wrote in
>> news:F7Ouc.34403$DC1.6921175 @news4.srv.hcvlny.cv.net:

 ...

>> > use strict;
>> > use warnings;
>> > 
>> > my @ref1;
>> > 
>> > for (my $i = 0; $i < 3; $i++){
>> >    $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
>> > }
>> > &{$ref1[1]};
 ...

>> C:\Home\asu1> tttt
>> Case 1 = 3 WRONG
>> 
>> This puzzled me as well.
> 
> The variable $i is only declared once (the first section of a C-style
> loop is only executed once); hence there is only one variable. All the
> subs refer to the same variable $i, so whatever you do they will
> always print the same thing.

A-ha! Thank you very much for the explanation.

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


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

Date: Mon, 31 May 2004 23:49:33 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Reference to loop index in a closure
Message-Id: <c9gg76$rqm$1@news.simnet.is>

"Bill Smith" <wksmith@optonline.net> wrote in message
news:F7Ouc.34403$DC1.6921175@news4.srv.hcvlny.cv.net...

> for (my $i = 0; $i < 3; $i++){
>  $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> }
> &{$ref1[1]};

i am no internal expert, but my perl sense tells me
that when the index is made lexical here, it is equivalent to



     my $i;
     for ($i = 0; $i < 3; $i++){
         $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
     }
   }

thus only one $i variable for the whole loop


>
> for (my $j = 0; $j < 3; $j++){
>  my $i = $j;
>  $ref1[$i] = sub{print "Case 2 = $i OK\n"};
> }

here a separate $i is instantiated for each round

gnari






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

Date: 31 May 2004 17:09:20 -0700
From: slinderstein@yahoo.com (Sol Linderstein)
Subject: Spawning external programs and running system commands from Windows
Message-Id: <e435b329.0405311609.5fc612e6@posting.google.com>

Hi,

I'm trying to do something trivial with ActivePerl on an XP machine
with no success.

C:\PERL\TEST>perl
system("dir");
^D

I get no output. In Unix this command works fine, substituting 'ls'
for 'dir'. My perl install is working because other programs, that
don't spawn external programs produce the expected output to the
screen.

I'm basically trying to call an external program from perl in a
Windows environment and the system/dir example is the easiest one I
can think of and it doesn't produce any output. I've tried various
other things, namely backticking and piping which work in Unix.

What's the trick to spawning external programs from Perl in Windows?

Thanks,
SL


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

Date: 1 Jun 2004 00:33:45 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: Spawning external programs and running system commands from Windows
Message-Id: <Xns94FAD12C75876asu1cornelledu@132.236.56.8>

slinderstein@yahoo.com (Sol Linderstein) wrote in 
news:e435b329.0405311609.5fc612e6@posting.google.com:

> Hi,
> 
> I'm trying to do something trivial with ActivePerl on an XP machine
> with no success.
> 
> C:\PERL\TEST>perl
> system("dir");
> ^D

dir is a built-in command. The EOF character on for cmd and command 
shells is ^Z (although ^D also seems to work on my machine with perl in 
this case).

What you need is:

C:\Home\asu1> perl
system 'cmd /c dir *.txt';
^Z
 Volume in drive C is HD
 Volume Serial Number is F882-4E55

 Directory of C:\Home\asu1

2004/05/12  09:12 PM             1,211 a.txt
2004/03/12  05:36 AM             4,278 ahsn.txt
2004/03/11  03:41 PM               103 asu1.sig.txt
2004/05/12  09:12 PM             1,131 b.txt
2003/08/20  02:04 PM            15,324 basicauth.txt

 ...

> I'm basically trying to call an external program from perl in a
> Windows environment and the system/dir example is the easiest one I
> can think of and it doesn't produce any output. I've tried various
> other things, namely backticking and piping which work in Unix.
> 
> What's the trick to spawning external programs from Perl in Windows?

There is no real trick but just to be sure, you might want to post a 
short example showing your problem.

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


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

Date: Tue, 01 Jun 2004 00:42:36 GMT
From: Kevin Michael Vail <kevin@vaildc.net>
Subject: Re: Spawning external programs and running system commands from Windows
Message-Id: <kevin-870CFC.20423331052004@news.verizon.net>

In article <e435b329.0405311609.5fc612e6@posting.google.com>,
 slinderstein@yahoo.com (Sol Linderstein) wrote:

> What's the trick to spawning external programs from Perl in Windows?

Win32::Process
-- 
Kevin Michael Vail | a billion stars go spinning through the night,
kevin@vaildc.net   | blazing high above your head.
 . . . . . . . . . | But _in_ you is the presence that
  . . . . . . . .  | will be, when all the stars are dead. 
 . . . . . . . . . |     (Rainer Maria Rilke)


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

Date: Tue, 1 Jun 2004 00:26:34 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Spawning external programs and running system commands from Windows
Message-Id: <c9gicj$s7q$1@news.simnet.is>

"Sol Linderstein" <slinderstein@yahoo.com> wrote in message
news:e435b329.0405311609.5fc612e6@posting.google.com...
> Hi,
>
> I'm trying to do something trivial with ActivePerl on an XP machine
> with no success.
>
> C:\PERL\TEST>perl
> system("dir");
> ^D

should work.

as should:

C:\PERL\TEST>perl
print qx(dir);
^D

gnari






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

Date: Mon, 31 May 2004 17:04:50 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: splitting cvs file and insert in mysql via DBI
Message-Id: <slrncbnb02.hv7.tadmc@magna.augustmail.com>

Ben Morrow <usenet@morrow.me.uk> wrote:
> Quoth Xaver Biton <javier@t-online.de>:


>>    while(<>){
>> 	  chomp;
>> 	  my ($erste,$zweite,$dritte,$cinque,$sei,$sette) = split /;/;
> 
> You really want to use an array here:
> 
> my @values = split /;/;


I wouldn't want that.

I don't want to have to remember "0 is erste, 1 is zweite ... 5 is sette"
every time I want to get a particular one from @values.

But if their scope was more than 8 or 10 lines, then I'd use a hash 
instead of an array (or individual scalars) so I have to remember 
less (I don't have a lot of brain to spare, so I have to conserve).


   my %fields;
   @fields{ qw/ erste zweite dritte cinque sei sette / } = split /;/;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Tue, 01 Jun 2004 00:31:35 +0200
From: Xaver Biton <javier@t-online.de>
Subject: Re: splitting cvs file and insert in mysql via DBI
Message-Id: <c9gbo6$9i0$00$1@news.t-online.com>

HI everyone,

Now works ok. I really thanks everyone.

The examples how to handle DBI error were very clear, thks again.

Xaver


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

Date: Mon, 31 May 2004 23:25:44 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: splitting cvs file and insert in mysql via DBI
Message-Id: <c9geto$5ir$2@wisteria.csv.warwick.ac.uk>


Quoth tadmc@augustmail.com:
> Ben Morrow <usenet@morrow.me.uk> wrote:
> > Quoth Xaver Biton <javier@t-online.de>:
> 
> 
> >>    while(<>){
> >> 	  chomp;
> >> 	  my ($erste,$zweite,$dritte,$cinque,$sei,$sette) = split /;/;
> > 
> > You really want to use an array here:
> > 
> > my @values = split /;/;
> 
> 
> I wouldn't want that.
> 
> I don't want to have to remember "0 is erste, 1 is zweite ... 5 is sette"
> every time I want to get a particular one from @values.
> 
> But if their scope was more than 8 or 10 lines, then I'd use a hash 
> instead of an array (or individual scalars) so I have to remember 
> less (I don't have a lot of brain to spare, so I have to conserve).

Given that 'erste', 'zweite', &c are presumably 'first', 'second', &c I
wouldn't imagine that would be too much of a problem for the OP :).

(I have to say I'm having trouble understanding where 4 has gone, or
what 'cinq' is doing in with all that German, but hey...)

Ben

-- 
   If you put all the prophets,   |   You'd have so much more reason
   Mystics and saints             |   Than ever was born
   In one room together,          |   Out of all of the conflicts of time.
ben@morrow.me.uk                                    The Levellers, 'Believers'


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

Date: Mon, 31 May 2004 23:38:41 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: splitting cvs file and insert in mysql via DBI
Message-Id: <c9gfir$rp6$1@news.simnet.is>

"A. Sinan Unur" <1usa@llenroc.ude> wrote in message
news:Xns94FAB43CBC08Easu1cornelledu@132.236.56.8...
> Xaver Biton <javier@t-online.de> wrote in
> news:c9g7hp$77h$04$1@news.t-online.com:
>
> >        my $sth->execute($erste,$zweite,$dritte,$cinque,$sei,$sette);
>
> If you had been checking for errors, you would have been told why the
> prepare call above did not succeed (and hence why $sth is undefined). You
> choose not to. How can we know what went wrong?

actually, the main reason for the undefinedness here is the 'my'.
but checking the result of the prepare() is good, specially when
*debugging*

gnari






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

Date: 1 Jun 2004 00:25:41 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: splitting cvs file and insert in mysql via DBI
Message-Id: <Xns94FACFCEFC84Basu1cornelledu@132.236.56.8>

"gnari" <gnari@simnet.is> wrote in news:c9gfir$rp6$1@news.simnet.is:

> "A. Sinan Unur" <1usa@llenroc.ude> wrote in message
> news:Xns94FAB43CBC08Easu1cornelledu@132.236.56.8...
>> Xaver Biton <javier@t-online.de> wrote in
>> news:c9g7hp$77h$04$1@news.t-online.com:
>>
>> >        my
>> >        $sth->execute($erste,$zweite,$dritte,$cinque,$sei,$sette); 
>>
>> If you had been checking for errors, you would have been told why the
>> prepare call above did not succeed (and hence why $sth is undefined).
>> You choose not to. How can we know what went wrong?
> 
> actually, the main reason for the undefinedness here is the 'my'.
> but checking the result of the prepare() is good, specially when
> *debugging*

I was so hung up on the OP blatantly disabling all sorts of error checking 
that I missed the obvious.

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


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

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


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