[23698] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5905 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 7 03:06:02 2003

Date: Sun, 7 Dec 2003 00:05:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 7 Dec 2003     Volume: 10 Number: 5905

Today's topics:
    Re: Am I right on Mod_Perl? (Randal L. Schwartz)
    Re: appending to file appends twice!? <someone@somewhere.nb.ca>
    Re: Can you explain LOCAL, MY and OUR with examples? (Randal L. Schwartz)
    Re: How to open a file from the end and read the last 1 <nmihai_year_2000@yahoo.com>
    Re: How to open a file from the end and read the last 1 <uri@stemsystems.com>
    Re: How to write to drive A:\ from CGI Perl <asu1@c-o-r-n-e-l-l.edu>
    Re: How to write to drive A:\ from CGI Perl <jurgenex@hotmail.com>
    Re: How to write to drive A:\ from CGI Perl (Cle)
    Re: Idiom for array index that I'm foreach'ing over? <bmb@ginger.libs.uga.edu>
    Re: Overloading <no_spam_for_jkeen@verizon.net>
        Processing array elements without iterative loop (John Markos O'Neill)
    Re: Processing array elements without iterative loop <jurgenex@hotmail.com>
    Re: Processing array elements without iterative loop <asu1@c-o-r-n-e-l-l.edu>
    Re: Processing array elements without iterative loop <uri@stemsystems.com>
    Re: question about installation of GD::Text on cygwin ( <mgjv@tradingpost.com.au>
    Re: read file with while and then scan lines into array <no_spam_for_jkeen@verizon.net>
    Re: What is anonymous sub? Why is it better? (Randal L. Schwartz)
    Re: What is anonymous sub? Why is it better? <uri@stemsystems.com>
    Re: What is anonymous sub? Why is it better? <tassilo.parseval@rwth-aachen.de>
    Re: What is anonymous sub? Why is it better? <uri@stemsystems.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 07 Dec 2003 04:11:50 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Am I right on Mod_Perl?
Message-Id: <db89508cee489a992876387944c720f7@news.teranews.com>

>>>>> "Picker" == Picker Leon <Temp@NoSuchDomain.Info> writes:

Picker> If I have take care of the globle varible by either make no
Picker> globle varibles, or by clean them each time before I use them
Picker> and after I use them, I can pretty much use the same code of
Picker> my perl in mod_perl enviroment.

Perhaps you are confusing all of mod_perl with the one thing of the
Apache::Registry module used with mod_perl.

Picker> Please correct me if I am wrong.

Asking for that is implied. :)  You're in CLPM, remember? :)

-- 
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: Sun, 07 Dec 2003 02:43:15 GMT
From: "Guy" <someone@somewhere.nb.ca>
Subject: Re: appending to file appends twice!?
Message-Id: <7twAb.6333$IF6.297736@ursa-nb00s0.nbnet.nb.ca>

Well I think I found the cause, but I still don't understand what's wrong...
(BTW, I will read up on perldoc -q quot, thanks for that :-)

I recently added a locking scheme to prevent 2 instances of my Perl script
from updating the text data files at the same time.

Basically it locks a dummy file for exclusive - once the file is locked it
does its stuff - then it closes that dummy file.

In any case, it turns out that if I don't call those locksys and unlocksys
routines of mine, this problem doesn't occur.
Am I doing anything wrong.
The only thing I can see different is that I have 2 files open at the same
time: LCKFILE and FILE
Do I need to so anything before opening a 2nd file or using a 2nd file
handle?

sub locksys
  {
  my $chk=3840;
  if(-d "$fd")
    {
    $chk=3584;
    if(open(LCKFILE,">$lockfil"))
      {
      $chk=flock(LCKFILE,2);
      }
    }
  return $chk;
  }

sub unlocksys
  {
  close LCKFILE;
  }

---------------------
Once that dummy file is locked, I open , read, and close other files using
FILE, for example:
--------------------

sub get_qa
  {
  my $chk=3840;
  if(-d "$fd")
    {
    $chk=256;
    if(open(FILE,"$qafil"))
      {
      @qafile=<FILE>;
      close(FILE);
      $chk=0;
      print"QAFILE = @qafile<BR><BR>\n";
      }
    }
  return $chk;
  }

sub f25
  {
  my $chk=0;
  my $number=0;
  my $qaline;
  my @qafield;

  &prt_head;

  $chk=get_qa();
  if($chk==0)
    {
    $chk=256;
    if(open(FILE,">$qafil"))
      {
      $chk=1;
      foreach $qaline (@qafile)
        {
        @qafield=();
        push(@qafield,split(/\t/,$qaline));
        if($qafield[0] == $fn)
          {
          $qaline="$fn\t$fq\t$fa\t\n";
          $chk=0;
          }
        print FILE $qaline;
        }
      if($chk==1)
        {
        $fn=$qafield[0]+1;
        $qaline="$fn\t$fq\t$fa\t\n";
        print FILE $qaline;
        }
      close(FILE);
      $chk=0;
      }
    }
  if($chk != 0){erreur($chk);}
  print"</FORM></BODY></HTML>\n";
  }





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

Date: Sun, 07 Dec 2003 04:11:49 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Can you explain LOCAL, MY and OUR with examples?
Message-Id: <d3b6b76e0af7f9ceda1837e49b95146b@news.teranews.com>

>>>>> "Picker" == Picker Leon <Temp@NoSuchDomain.Info> writes:

Picker> Anyone can give me an example script which will act
Picker> differently with MY, LOCAL and OUR?

Better than that, an entire article at

        <http://www.stonehenge.com/merlyn/UnixReview/col46.html>

And alongside that one, another 198 feature-length articles on Perl
to read.  And another 2.5 each month!

(Please post with a valid email address next time.  I would have preferred
emailing this to you instead.)

-- 
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: Sun, 07 Dec 2003 03:37:47 GMT
From: "Mihai N." <nmihai_year_2000@yahoo.com>
Subject: Re: How to open a file from the end and read the last 100 lines
Message-Id: <Xns9449C7C10FC6DMihaiN@204.127.199.17>

Uri Guttman <uri@stemsystems.com> wrote in
news:x7brqlsao7.fsf@mail.sysarch.com: 

>>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
> 
>   AS> Sara <genericax@hotmail.com> wrote in comp.lang.perl.misc:
>  >> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message
>  >> news:<bqs8md$hfo$1@mamenchi.zrz.TU-Berlin.DE>...
>  >> > > 
>  >> > >    die "I hate MONDAYS!\n" unless open F, 'log';
>  >> > >    my @l = <F>;
>  >> > >    close F;
>  >> > >    @l = splice @l, @l-100;
>  >> > > 
>  >> > > and botta bing you have your last 100 lines! 
>  >> > 
>  >> > ...except when the file has fewer than 100 lines, in which case a
>  >> > fatal run-time error results.  Uri's File::ReadBackwards deals
>  >> > correctly with that case.
>  >> > 
>  >> > This is a good demonstration why "rolling your own" is a bad idea,
>  >> > even if the problem looks trivial.
>  >> 
>  >> Oh yes a small mod for that trivial case:
>  >> 
>  >> splice @l, @l-100 if @l > 100;
> 
>   AS> Don't shrug off trivial bugs.  In a production program, this kind
>   of AS> bug can go unnoticed for a long time.  Then the program fails
>   for no AS> good reason, perhaps because it's called a few times a day
>   instead of AS> just once.  Not good.  CPAN modules don't *have*
>   trivial bugs like that. 
> 
> and my module is much faster than her code as well. slurping in a whole
> file to get last 100 lines is a waste of ram and cpu. and if the file is
> a large log, forget it. sara will just have to learn that rolling your
> own all the time is fruitless. the ultimate result is write your own in
> c because perl is just a large c based application. 
> 
> uri
> 

what about this:
while( <> ) {
    	push @lines, $_;
    	shift @lines if $#lines > 100;
}

I agree CPAN is great.
But you have to put hings in balance.

When the time + effort to search for what you need,
evaluate the 20 possible modules, select one or two, compare,
understand how they work, I would rather write my own two lines.

I would not do this for complex stuff, like parsing XML/HTML,
sending emails, DB interogations, etc.
Where is the limit for "complex" for each one, it is for each
one to decide. If I am not able to write a line of perl to
do my stuff, chances are I will not be able to use properly a
CPAN module.
CPAN is not going to think for you.

-- 
Mihai
-------------------------
Replace _year_ with _ to get the real email


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

Date: Sun, 07 Dec 2003 04:52:42 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: How to open a file from the end and read the last 100 lines
Message-Id: <x7ptf1qlgo.fsf@mail.sysarch.com>

>>>>> "MN" == Mihai N <nmihai_year_2000@yahoo.com> writes:

  MN> Uri Guttman <uri@stemsystems.com> wrote in
  MN> news:x7brqlsao7.fsf@mail.sysarch.com: 

  >> and my module is much faster than her code as well. slurping in a whole
  >> file to get last 100 lines is a waste of ram and cpu. and if the file is
  >> a large log, forget it. sara will just have to learn that rolling your
  >> own all the time is fruitless. the ultimate result is write your own in
  >> c because perl is just a large c based application. 

  MN> what about this:
  MN> while( <> ) {
  MN>     	push @lines, $_;
  MN>     	shift @lines if $#lines > 100;
  MN> }

  MN> I agree CPAN is great.
  MN> But you have to put hings in balance.

that still reads in the ENTIRE file. and it does push and shift for most
lines. just try it on a large log file. i will be waiting for you.

  MN> I would not do this for complex stuff, like parsing XML/HTML,
  MN> sending emails, DB interogations, etc.  Where is the limit for
  MN> "complex" for each one, it is for each one to decide. If I am not
  MN> able to write a line of perl to do my stuff, chances are I will
  MN> not be able to use properly a CPAN module.  CPAN is not going to
  MN> think for you.

complex, slow, simpler, faster are all in the coder's control. too many
people asked how to read from the end of a file so i wrote
File::Readbackwards to solve that problem. so now it is the standard
answer for that. it is simple (simpler than rolling your own anyway you
code it), faster than all other methods (if you can show me a faster
method, i will use it) and on cpan for all to use. so there is no reason
to do it your own way. it is not such an interesting problem that you
can learn much by doing it yourself. and most common and obvious methods
are much slower and more cumbersome. that is the essence of resuable
code - solve a common problem well and put it in to some reuseable
thing. then focus your energies where they are needed. rolling your own
each time is a waste of your own time. searching cpan or asking
somewhere about a module is almost always going to be more productive.

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: 7 Dec 2003 02:56:59 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: How to write to drive A:\ from CGI Perl
Message-Id: <Xns9449DF491B32Dasu1cornelledu@132.236.56.8>

canle@lecan.com (Cle) wrote in 
news:8a21e493.0312061529.be1aaed@posting.google.com:

> Hi!
> 
> Thank you all for your helps.

Don't take this the wrong way but I am afraid you are dabbling in things 
that are way above the level of your knowledge and current skills. You seem 
to be confused beyond my capacity to help you.
 
> My codes from html file to access my drive A which is working OK:
> file:///A|/

So, are you saying that you have a hyperlink

<a href="file:///A|/">A: Drive</a>

on a web page you are serving? I do not know what you are trying to 
accomplish, but you should go back to basics for a while to figure out why 
that is nonsensical 99.9% of the time.

> My sample in Perl script which is also working OK:
> http://www.lecan.com/cgi-bin/cgiwrap/canle/hw3mcgi.pl#EST

I did look at that page. What I saw was scary:

<title>http://lecan.com</title><meta http-equiv=Content-Type 
content=text/html;
charset=UTF-8><font size=4 face=Verdana, helvetica><h5><body
text="blue"><br>Book List Database Sample <body text="blue"><br>

If that does not look weird to you, then please go back to basics.

> This is output stored in Telnet server at my website directory after I
> run above script, but I wanted to write below output to drive A of my
> computer:

A CGI script running on a separate computer cannot access the drives on the 
computers of visitors to that site. If that does not make sense to you, 
stop what you are doing and go back to basics. The script can generate 
output which the visitor can decided where to save (or whether to save).

> This is what was appeared in my telnet home directory when I wrote to
> C:
> C:Documents and SettingsMyName.COMPNameDesktopOutputFile.txt

If you need to refer to the path

C:\Documents and Settings\MyName.COMPName\Desktop\OutputFile.txt

you need to use

C:\\Documents and Settings\\MyName.COMPName\\Desktop\\OutputFile.txt

or

C:/Documents and Settings/MyName.COMPName/Desktop/OutputFile.txt

> I am trying to test your codes.

Don't. Instead, go back to basics. Learn HTML first. Learn about servers a 
little. Read the CGI spec etc. Then start learning Perl by getting a good 
book and going at a slow and steady pace.

Sinan.

-- 
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov


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

Date: Sun, 07 Dec 2003 05:38:52 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How to write to drive A:\ from CGI Perl
Message-Id: <M1zAb.1309$nz.935@nwrddc01.gnilink.net>

Cle wrote:

I am guessing, but because you seem to be unable to provide a clear scenario
it is the only option.
Please feel free to jump in at any time and correct my assumptions. Maybe we
can get a clearer picture that way.

Assumption #1: you are talking about a CGI script written in Perl running on
a web server

> My codes from html file to access my drive A which is working OK:
> file:///A|/

Assumption #2: here you are talking about some HMTL document that is
referencing some file on the local A drive using href="file://......". It is
irrelevant if the HTML document is a local file or if it has been downloaded
from a web server.

> My sample in Perl script which is also working OK:
> http://www.lecan.com/cgi-bin/cgiwrap/canle/hw3mcgi.pl#EST
>
> This is output stored in Telnet server at my website directory after I
> run above script,

I am guessing that now you are not talking about a client side (in a
server-client context) file any more, but about a server side Perl CGI
script, that writes some file to the filesystem on the server.

> but I wanted to write below output to drive A of my
> computer:

I am guessing again, that with "my computer" you are referring to the client
computer that is connected to a web server, which in turn runs the Perl CGI
script.

Assumption #3: I am guessing that somehow you got the idea that a
server-side Perl CGI script would be able to write a file on the client
side. If this is indeed what you are looking for: forget it.

First of all it has nothing to do with Perl. In this scenario Perl is merely
the tool, that generates the HTML document, that is sent to the client by
the web server. Or are you also blaming the hammer if your pictures aren't
hanging straight?

And second: think about what you are asking. A web server being able to
write a file to your A drive or even worse C drive? Any file? ANY? Do you
really want any website overwriting your files without your consent? Just
think about what you are asking for.

Of course I may very well be wrong. You didn't give us much information to
go on, so my assumptions and guesses may very well be way off. In that case
please try to explain in clear terms what you mean. We are not mind readers
(although some people are darn good it) and cannot know what you are trying
to do without you telling us.

jue




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

Date: 6 Dec 2003 23:09:59 -0800
From: canle@lecan.com (Cle)
Subject: Re: How to write to drive A:\ from CGI Perl
Message-Id: <8a21e493.0312062309.65e7f907@posting.google.com>

Hi Sinan and William.

Thank you for your times.

What browser did you look at my homepage to see only my HTML codes ? I
could view its output from my windows 98 and XP by old Netscape 4.5 to
7.1 or IE 6.0. This srcipt run OK on Linux 7.1 server.

I am having a better version of this type of Perl, and will invite you
to look at it again in the near fututre, if you have time.

I could read a file in drive A of my computer from cgi-bin Telnet by
having this line in my CGI Perl script:

print "<iframe src =\"file:///A:\FileNmae.txt"></iframe>";

The reason that I like to write to drive A: or other removable drive,
from CGI as a select option: I like to keep my file private from
SVCHOST.EXE which has RPC calls to do Remote things from windows, they
did remotely turned off my microphone at audio chatroom by RPC and
network problem, among other commands from Paltak. So any hacker can
read my file from another windows OS, even if they know my password
from camera and other methods or not, someday, they might delete or
copy my file from another PC.

I am hoping that you will have time to view my other output in near
future. I am learning about server as you suggested.

Thank you

Cle


"A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu> wrote in message > I did look at that page. What I saw was scary:

 <title>http://lecan.com</title><meta http-equiv=Content-Type 
 content=text/html;
 charset=UTF-8><font size=4 face=Verdana, helvetica><h5><body
 text="blue"><br>Book List Database Sample <body text="blue"><br>
 
 If that does not look weird to you, then please go back to basics.
 
 Sinan.


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

Date: Sat, 6 Dec 2003 21:32:05 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Idiom for array index that I'm foreach'ing over?
Message-Id: <Pine.A41.4.58.0312062113080.17406@ginger.libs.uga.edu>

On Fri, 5 Dec 2003, Anno Siegel wrote:
> Ben Morrow  <usenet@morrow.me.uk> wrote in comp.lang.perl.misc:
> >     rewind(@_), return if grep { $i{$set} >= @$_ } @_;
> >     # I'm not sure what we want here... 'if @_ == grep ...' may be better.
>
> So far, I have taken the first array to be the measure of all.  Oh, so the
> prototype should be (\@;\@...), a bug.  Yours take the maximum or the
> minimum length of all arrays offered, which is more thorough.  There may
> be other useful strategies.

Regarding other strategies, I was thinking of an option to "go out of
bounds", i.e., to take the maximum length in the set of arrays.
Assigning to the "goob" routine both chooses this option and sets the
value to return when you're out of bounds., e.g.

goob( @aa, @bb ) = '';  # or perhaps 0 or even undef

Do you see a better way to do that?


my %i;
my %goob;  # parallel hashes, no less

sub goob   (;\@\@\@\@\@) : lvalue { $goob{ join "", @_ } }

sub start  (;\@\@\@\@\@) : lvalue { $i{ join "", @_} }

sub saw    (;\@\@\@\@\@) { ($i{ join "", @_}||0) - 1 }

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub them   (;\@\@\@\@\@) {
    my $set = join "", @_;
    my $i = $i{ $set } ++; # increment for next time, use current
    if( exists $goob{ $set } ) {
        &rewind, return unless grep { $i < @$_ } @_;
        return ( $i, map { $i < @$_ ? $_->[$i] : $goob{ $set } } @_);
    }
    &rewind, return if grep { $i >= @$_ } @_;
    ( $i, map $_->[$i], @_);
}


Regards,

Brad


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

Date: Sun, 07 Dec 2003 03:48:51 GMT
From: "Jim Keenan" <no_spam_for_jkeen@verizon.net>
Subject: Re: Overloading
Message-Id: <DqxAb.1459$Ji.253@nwrdny02.gnilink.net>


"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:bqsm47$149$1@wisteria.csv.warwick.ac.uk...
> I've been playing with overloading, and the following is puzzling
> me... the script
> [snip]
>
> prints
>
> stringify
> *
> numify
> Argument "*" isn't numeric in addition (+) at ./op line 15.
> numify
> Argument "*" isn't numeric in addition (+) at ./op line 15.
> 0
> sub(1)
> Argument "*" isn't numeric in subtraction (-) at ./op line 35.
> stringify
> 1
>
> about which I have two questions:
>
> 1. Why is 'numify' called twice for int($i)?
>
> 2. Why is 'numify' not called at all before calling '-'? OK, I guess
>    that makes sense... but why, then, will '-' not autogenerate in
>    terms of 'numify'? Is there any way to call it, without giving the
>    sub a name (i.e., is it possible to simply 'numify' a scalar, as
>    "$s" will stringify)?
>

In attempting to reproduce your problem, I rearranged the code so as to put
package I at the top of the file, then explicitly called package main.  I
also threw in some newlines for readability.  The result:  package main ran
without warnings.

package I;
use overload
    "0+" => sub {
        my $s = shift;
        my $v = $$s;
        print "numify";
        0+$v;
    },

    q{""} => sub {
        my $s = shift;
        my $v = $$s;
        print "stringify";
        "$v";
    },

    "=" => sub {
        my $s = shift;
        my $v = $$s;
        print "copy";
        my $t = $v;
        bless \$t;
    },

    "-" => sub {
        my $s = shift;
        my $v = $$s;
        my $a = shift;
        print "sub($a)";
        $$s = (shift) ? $v - $a : $a - $v;
    };

sub new {
    my $c = shift;
    my $s = shift;
    bless \$s, $c;
}

1;

package main;
use warnings;
use strict;

my $i = new I "*";
print $i, "\n";
print int($i), "\n";
print $i--, "\n";

__END__
# above code prints:

stringify*
numify0
sub(1)stringify1




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

Date: 6 Dec 2003 21:55:19 -0800
From: john@nhoj.com (John Markos O'Neill)
Subject: Processing array elements without iterative loop
Message-Id: <8179ccbd.0312062155.38ad4847@posting.google.com>

Yesterday someone suggested to me that it was possible to do something
to each element in an array without an iterative (say, a foreach)
loop.  Well, actually, they said it could be done in one line but I
assume they didn't just mean it would be done with an aesthetically
displeasing removal of white space.

In my experience, the least-lines-of-perl way to, say, print out all
of the even elements in an array would be something like

@array = (1, 2, 3, 4, 5, 6, 7, 8);
foreach $array_ele (@array) {
    ($array_ele % 2 == 0) &&
    ($even_array_ele = $array_ele) &&
    print("\$even_array_ele:  $even_array_ele\n");
}

For the sake of this discussion, let's call the three-line statement
in the foreach loop a single line.  Is it possible to do this with
fewer statements?  If so, how?

Thanks,

John Markos O'Neill


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

Date: Sun, 07 Dec 2003 06:15:40 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Processing array elements without iterative loop
Message-Id: <gAzAb.1387$nz.276@nwrddc01.gnilink.net>

John Markos O'Neill wrote:
> In my experience, the least-lines-of-perl way to, say, print out all
> of the even elements in an array would be something like

> @array = (1, 2, 3, 4, 5, 6, 7, 8);
> foreach $array_ele (@array) {
>     ($array_ele % 2 == 0) &&
>     ($even_array_ele = $array_ele) &&
>     print("\$even_array_ele:  $even_array_ele\n");
> }
>
> For the sake of this discussion, let's call the three-line statement
> in the foreach loop a single line.  Is it possible to do this with
> fewer statements?  If so, how?

What about a simple
    my @array = (1, 2, 3, 4, 5, 6, 7, 8, 13, 14);
    print grep(($_ +1) % 2, @array);

This will print
    246814
i.e. all the even elements in the array (just not separated by a blank, but
you could easily insert that).

jue




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

Date: 7 Dec 2003 06:21:09 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: Processing array elements without iterative loop
Message-Id: <Xns944ADC34928asu1cornelledu@132.236.56.8>

john@nhoj.com (John Markos O'Neill) wrote in 
news:8179ccbd.0312062155.38ad4847@posting.google.com:

> In my experience, the least-lines-of-perl way to, say, print out all
> of the even elements in an array would be something like
> 
> @array = (1, 2, 3, 4, 5, 6, 7, 8);
> foreach $array_ele (@array) {
>     ($array_ele % 2 == 0) &&
>     ($even_array_ele = $array_ele) &&
>     print("\$even_array_ele:  $even_array_ele\n");
> }
> 
> For the sake of this discussion, let's call the three-line statement
> in the foreach loop a single line.  Is it possible to do this with
> fewer statements?  If so, how?

map {print "\$even: $_\n"} grep {!($_ % 2)} (1, 2, 3, 4, 5, 6, 7, 8);

And the interative version can be written as:

for (@array) {
	print "$_ is even\n" unless ($_ % 2);
}



-- 
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov


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

Date: Sun, 07 Dec 2003 06:34:00 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Processing array elements without iterative loop
Message-Id: <x7he0dqgq0.fsf@mail.sysarch.com>

>>>>> "JMO" == John Markos O'Neill <john@nhoj.com> writes:

  JMO> Yesterday someone suggested to me that it was possible to do something
  JMO> to each element in an array without an iterative (say, a foreach)
  JMO> loop.  Well, actually, they said it could be done in one line but I
  JMO> assume they didn't just mean it would be done with an aesthetically
  JMO> displeasing removal of white space.

  JMO> In my experience, the least-lines-of-perl way to, say, print out all
  JMO> of the even elements in an array would be something like

  JMO> @array = (1, 2, 3, 4, 5, 6, 7, 8);
  JMO> foreach $array_ele (@array) {
  JMO>     ($array_ele % 2 == 0) &&
  JMO>     ($even_array_ele = $array_ele) &&
            ^^^^^^^^^^^^^^^
why do you need that temp var? and what if the element was 0 - that
would fail. 

  JMO>     print("\$even_array_ele:  $even_array_ele\n");
  JMO> }

others have shown you clean short ways. i just want to know why yours
was much longer than it needed to be.

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: Sun, 7 Dec 2003 13:59:21 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: question about installation of GD::Text on cygwin (could be a Makefile q)
Message-Id: <slrnbt55s7.8l1.mgjv@martien.heliotrope.home>

On 05 Dec 2003 04:18:59 GMT,
	Martien Verbruggen <mgjv@tradingpost.com.au> wrote:
> On 4 Dec 2003 08:21:32 -0800,
> 	Upstart <stimonyhall@netscape.net> wrote:
>> Martien Verbruggen <mgjv@tradingpost.com.au> wrote in message news:<slrnbsq4d3.5j7.mgjv@verbruggen.comdyn.com.au>...
>>  
>>> Just for the heck of it, could you do something like:
>>> 
>>> $ cd /tmp
>>> $ h2xs -ACXn Foo
>>> Writing Foo/Foo.pm
>>> Writing Foo/Makefile.PL
>>> Writing Foo/README
>>> Writing Foo/test.pl
>>> Writing Foo/MANIFEST
>>> $ cd Foo
>>> [edit Makefile.PL and put "use GD;" near the top]
>>> $ perl Makefile.PL
>> 
>> This breaks!
>> 
>> $ h2xs -ACXn Foo
> [snip]

I just created a fresh installation of Perl 5,8.2 on my platform:

$ /opt/perl-5.8.2/bin/perl -v

This is perl, v5.8.2 built for i686-linux
[snip]

and installed GD in there, linked against libgd 2.0.15

$ /opt/perl-5.8.2/bin/perl -MGD -le 'print $GD::VERSION'
2.11

$ ldd /opt/perl-5.8.2/lib/site_perl/5.8.2/i686-linux/auto/GD/GD.so 
        libgd.so.2 => /opt/gd-2.0.15/lib/libgd.so.2 (0x40014000)
[snip]

That GD is linked against libfreetype 2.1.3.

The MakeMaker is the standard 6.17 version that comes with 5.8.2.

$ /opt/perl-5.8.2/bin/h2xs -ACXn Foo
Defaulting to backwards compatibility with perl 5.8.2
If you intend this module to be compatible with earlier perl versions,
please
specify a minimum perl version with the -b option.

Writing Foo/lib/Foo.pm
Writing Foo/Makefile.PL
Writing Foo/README
Writing Foo/t/Foo.t
Writing Foo/MANIFEST
$ cd Foo 
$ vi Makefile.PL 
$ /opt/perl-5.8.2/bin/perl Makefile.PL                  
Checking if your kit is complete...
Looks good
Writing Makefile for Foo

So that works fine.

And in my current checkout of the GD::Text modules:

$ cd ~/src/GDTextUtils
$ make distclean
[snip]
$ /opt/perl-5.8.2/bin/perl Makefile.PL 
Checking if your kit is complete...
Looks good
Writing Makefile for GD::Text
$ make test
cp Text.pm blib/lib/GD/Text.pm
cp Text/Wrap.pm blib/lib/GD/Text/Wrap.pm
cp Text/Align.pm blib/lib/GD/Text/Align.pm
PERL_DL_NONLAZY=1 /opt/perl-5.8.2/bin/perl "-MExtUtils::Command::MM"
"-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/align....ok                                                                
        5/21 skipped: Some TTF tests disabled: Freetype inconsistent
t/text.....ok                                                                
        2/20 skipped: Some TTF tests disabled: Freetype inconsistent
t/wrap.....ok                                                                
        2/14 skipped: Some TTF tests disabled: Freetype inconsistent
All tests successful, 9 subtests skipped.
Files=3, Tests=55,  1 wallclock secs ( 0.59 cusr +  0.09 csys =  0.68 CPU)


Which is also all as expected.


So, my conclusion would be that the problem is not related to Perl, GD,
libgd or freetype, and their interaction.

I believe it is either something that is broken in the cygwin
installation in general (unlikely), or in your installation
specifically.


If someone else could run the above tests on Cygwin as well, that might
shed some light on things, or maybe you could try getting a clean
installation of cygwin to try things on?

>>> If that breaks, then the problem is somewhere in the interaction
>>> between GD and MakeMaker, and that's probably worth a bug report to
>>> either P5P of Lincoln Stein.
>> 
>> What would be the most effective way to proceed? It is a bummer the
>> way this thing is not throwing some kind of error message. Is there a
>> debug switch somewhere?
> 
> I'll have a look to see whether I can reproduce it in my environment.
> I haven't actually got 5.8.2 installed yet, but I should. If the same
> happens here (non-cygwin) I'll submit a bug report. If not, I'll get
> back here, and someone with cygwin shuld probably try to figure ut
> what the story is.

At the moment, I can't see that either GD or Perl are to blame, so
neither of the above should get a bug report. It could still be a Cygwin
problem, but it's looking more and more like it might be something local
in your installation.

>> Is there anybody out there who has my combination of a platform (win
>> 2000, perl 5.8.2, cygwin) that has got this to work recently?

That would be a good way to find out whether it's a more general Cygwin
issue, or something local to your machine.

Anyway, I can't help any further, I think, since I tend to not use
Windows platforms unless I have to. I don't have one easily available to
me on which I can quickly cobble together a fresh environment.

Martien
-- 
                        | 
Martien Verbruggen      | 
                        | In a world without fences, who needs Gates?
                        | 


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

Date: Sun, 07 Dec 2003 04:18:14 GMT
From: "Jim Keenan" <no_spam_for_jkeen@verizon.net>
Subject: Re: read file with while and then scan lines into array
Message-Id: <aSxAb.1503$Ji.1154@nwrdny02.gnilink.net>

"Martin Foster" <mdfoster44@netscape.net> wrote in message
news:6a20f90a.0312051108.2559ac0f@posting.google.com...
> I'm scanning text files into a database.
>
> My perl script looks like this:
>

You've written your post in such a confusing manner that it is difficult to
figure out what your problem is.

> # start loop of file to scan for data
>   while (defined ($_2 = <INFILE>)){
>     # Find  cell data
>     if ($_2 =~ m/_cell_length_a\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/){
>       $cell[0] = $1;

In the code presented, you don't assign to any element of @cell other than
$cell[0].  So why use an array at all?

>       print "Found cell parameter a= ", $cell[0], "  ";
>       print "For str_id number ", $au_id, "\n";

Where did $au_id come from?

>       # Insert data
>       $stmt1 = "UPDATE bgb_data SET latpar_a = ? WHERE str_id = ?";
>       $sth = $dbh->prepare($stmt1);
>       $sth->execute($cell[0], $au_id);
>     }
>
>    # get sequences
>     if ($_2 =~ m/_Sequence/){
>       # start loop to scan in sequences

This loop is incomplete.  Was what you really intended something like this?

     if  ($_2 =~ m/_cell_length_a\s+(-?([0-9]+(\.[0-9]*)?|\.[0-9]+))/){
        # process
    } elsif () {
        # process
    } ($_2 =~ m/_Sequence/)

>
> So now I've found a tag and the next few lines are number sequences
> which
> I want in an array.
>
> I want to scan in those lines into until a blank line appears and then
> continue scanning for further data, in the while loop.
>
Does that mean that when you are processing a file line-by-line and
encounter a blank line, you wish to start a new array to hold the sequence
numbers?

Can you provide some sample data we could test this with?

Jim Keenan





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

Date: Sun, 07 Dec 2003 04:11:50 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <fa52de1d59962c4e9c9406cff955eec3@news.teranews.com>

>>>>> "Uri" == Uri Guttman <uri@stemsystems.com> writes:

Uri> ever heard of closures?

But what does that have to do with anonymous subs?

Being anonymous is an orthogonal property to being a closure.
There are anonymous non-closures, and named closures.

print "Just another Perl hacker," # the original JAPH

-- 
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: Sun, 07 Dec 2003 04:59:21 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <x7n0a5ql5e.fsf@mail.sysarch.com>

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

>>>>> "Uri" == Uri Guttman <uri@stemsystems.com> writes:
  Uri> ever heard of closures?

  RLS> But what does that have to do with anonymous subs?

  RLS> Being anonymous is an orthogonal property to being a closure.
  RLS> There are anonymous non-closures, and named closures.

that is a debateable point. some prefer to only call closures only anon
subs with lexicals. some claim all subs which have such lexicals are
closures. i won't debate you on that but i prefer to only call the anon
subs such. named subs that have external lexicals just have external
lexicals. my view of closures is the ability to make new subs with new
values of those private lexicals and named subs can't do that.

in any case, the OP is delving into stuff way over his head and he
barely know any perl. and he still doesn't seem to be reading any
responses here as he doesn't post where he gets his crazy code.

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: 7 Dec 2003 07:24:49 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <bqukk1$5be$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Uri Guttman:

>>>>>> "RLS" == Randal L Schwartz <merlyn@stonehenge.com> writes:
> 
>>>>>> "Uri" == Uri Guttman <uri@stemsystems.com> writes:
>  Uri> ever heard of closures?
> 
>  RLS> But what does that have to do with anonymous subs?
> 
>  RLS> Being anonymous is an orthogonal property to being a closure.
>  RLS> There are anonymous non-closures, and named closures.
> 
> that is a debateable point. some prefer to only call closures only anon
> subs with lexicals. some claim all subs which have such lexicals are
> closures. i won't debate you on that but i prefer to only call the anon
> subs such. named subs that have external lexicals just have external
> lexicals. my view of closures is the ability to make new subs with new
> values of those private lexicals and named subs can't do that.

Here is what for instance wikipedia has to say on closures. I find
similar definitions elsewhere:

    Closure (computer science)
    
    In programming languages, a closure is an abstraction representing a
    function, plus the lexical environment in which the function was
    created.

    Closures are typically implemented with a special data structure
    that contains a pointer to the function code, plus a representation
    of the function's lexical environment (i.e., the set of available
    variables and their values) at the time when the closure was
    created.

So these definitions include the nature of a closure (first paragraph)
along with their common implementation (second paragraph). So you and
Randal are probably both right. :-)

> in any case, the OP is delving into stuff way over his head and he
> barely know any perl. and he still doesn't seem to be reading any
> responses here as he doesn't post where he gets his crazy code.

I can't quite agree with the first point. If a beginner was following
the advice of using modules for common tasks, he'd sooner or later
encounter closures (closures according to your definition). Some very
common modules make use of them, such as XML::Parser (when using
handlers) or File::Find. Other methods often proposed to newbies involve
them as well, for instance dispatch-tables.

I therefore do endorse it when people want to tackle them very early.
Once they understand how they work, they understand quite a bit of Perl
and its mindset. Thus they'll pick up other essential things en-passant,
such as scoping rules and references.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Sun, 07 Dec 2003 07:33:11 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: What is anonymous sub? Why is it better?
Message-Id: <x7ad65qdzc.fsf@mail.sysarch.com>

>>>>> "TvP" == Tassilo v Parseval <tassilo.parseval@rwth-aachen.de> writes:

  TvP> Also sprach Uri Guttman:

  >> in any case, the OP is delving into stuff way over his head and he
  >> barely know any perl. and he still doesn't seem to be reading any
  >> responses here as he doesn't post where he gets his crazy code.

  TvP> I can't quite agree with the first point. If a beginner was following
  TvP> the advice of using modules for common tasks, he'd sooner or later
  TvP> encounter closures (closures according to your definition). Some very
  TvP> common modules make use of them, such as XML::Parser (when using
  TvP> handlers) or File::Find. Other methods often proposed to newbies involve
  TvP> them as well, for instance dispatch-tables.

those modules use callbacks which can be closures. they can also call
any code refs you pass in. closures are useful there but not needed. i
have used callbacks with named subs in a class just fine.

  TvP> I therefore do endorse it when people want to tackle them very early.
  TvP> Once they understand how they work, they understand quite a bit of Perl
  TvP> and its mindset. Thus they'll pick up other essential things en-passant,
  TvP> such as scoping rules and references.

in general i would agree with you. but the OP is bouncing around all
over perl and conflating all sorts of stuff (eval vs my vs
local????). it just seems like the OP is trying to learn all of perl in
one fell swoop and showing poor signs of programming skills along the
way. in his case, i would recommend learning basic perl first and not
jump into using powerful modules with callback apis.

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: 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 5905
***************************************


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