[25924] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8146 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 3 14:05:24 2005

Date: Fri, 3 Jun 2005 11:05:05 -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, 3 Jun 2005     Volume: 10 Number: 8146

Today's topics:
    Re: ActivePerl and -P option <tadmc@augustmail.com>
    Re: ActivePerl and -P option <sisyphus1@nomail.afraid.org>
    Re: ActivePerl and -P option <apeiron+usenet@coitusmentis.info>
    Re: counting word occurances  <segraves_f13@mindspring.com>
    Re: counting word occurances  <segraves_f13@mindspring.com>
    Re: counting word occurances <tadmc@augustmail.com>
    Re: counting word occurances <jurgenex@hotmail.com>
    Re: counting word occurances <noreply@gunnar.cc>
    Re: counting word occurances <1usa@llenroc.ude.invalid>
    Re: counting word occurances <vticau@excite.com>
    Re: counting word occurances <noreply@gunnar.cc>
    Re: counting word occurances <1usa@llenroc.ude.invalid>
        Shared perl reposity <hemulaattori@invalid.yahoo.com>
    Re: Shared perl reposity <nobull@mail.com>
    Re: Shared perl reposity <hemulaattori@invalid.yahoo.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 3 Jun 2005 08:42:04 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: ActivePerl and -P option
Message-Id: <slrnda0nhc.5ei.tadmc@magna.augustmail.com>

Viviana Vc <vcotirlea@hotmail.com> wrote:

> I am new to these lists, 


This is not a "list" (as in "email list").

This is a "newsgroup", which is a very different thing.

Anyway, there are Posting Guidelines posted here twice a week that
contains lots of tips and tricks to increase your chances of
getting answers.


> I have to write a perl script that uses some defines from an .h file. 

> those defines (i.e. #define ALFA "alfa") I


Surely you must have mean "e.g" instead of "i.e".

Don't you want to handle defines with names and values that
are different from the one you've shown?  :-)


> have to run the script through the preprocessor first,


You don't "have" to, there are other ways.

Like converting your C defines into Perl constants. See

   perldoc constant

Then you can just convert them into Perl with something like:

   s/^#define\s+(\w+)\s+(.+)/use constant $1 => $2;/;


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


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

Date: Sat, 4 Jun 2005 00:22:52 +1000
From: "Sisyphus" <sisyphus1@nomail.afraid.org>
Subject: Re: ActivePerl and -P option
Message-Id: <42a067b3$0$23801$afc38c87@news.optusnet.com.au>


"Viviana Vc" <vcotirlea@hotmail.com> wrote in message
news:3gavgeFbku9rU1@individual.net...
> On Fri, 03 Jun 2005 12:59:30 +0200, Josef Moellers

>
> But, I get the "The system cannot find the path specified" error by just
> typing in cmd prompt "perl -P", without having any perl script or
> include files. So it's not related to those but with other files that
> perl needs. If I am giving the wrong or right files I get the exact same
> error.

Yes, I get the same with perl 5.8.4 (ActiveState build 810), though MSVC++
6.0 is locatable.
With the same version of perl, but built with MSVC++ 7.0 (.NET), I get a
slightly more explanatory message:

'F:\perlvc7\bin' is not recognized as an internal or external command,
operable program or batch file.

'F:\perlvc7\bin' is the perl\bin folder (containing perl.exe, etc.) - I
don't know why such a command is being run.

With my MinGW-built perl 5.8.6 (a slightly newer version of perl), I find
there is no problem. The '-P' switch works as intended, at least in simple
cases.

---- try.h ----
#define ALSA 7

---- try.pl ----
use warnings;
#include "try.h"
print ALSA, "\n";
__END__

D:\pscrpt>perl -P try.pl
7

So - it's not a Win32 issue, but looks to be some issue with the Microsoft
compiler/preprocessor. I have verified that the 'F:\perlvc7\bin' command is
not related to the contents of the 'path' environment variable, and that
it's not being read from Config.pm, but that's about as far as I've got ....
any ideas on how to proceed ?

Cheers,
Rob




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

Date: 03 Jun 2005 17:07:24 GMT
From: Christopher Nehren <apeiron+usenet@coitusmentis.info>
Subject: Re: ActivePerl and -P option
Message-Id: <slrnda13ic.3ml.apeiron+usenet@prophecy.dyndns.org>

On 2005-06-03, Viviana Vc scribbled these
curious markings:
> I have to run the script through the preprocessor first, and the help
                                                               ^^^^^^^^

You mean the POD, yes? "help" to me implies a proprietary, compressed(?)
format accessible via a Microsoft program or software that emulates
such.

> says this is supported: "-P   run program through C preprocessor
> before compilation".

It also says this, at least on my FreeBSD system:

<begin quote>
-P   NOTE: Use of -P is strongly discouraged because of its inherent
           problems, including poor portability.

[...]

If you're considering using "-P", you might also want to look at
the Filter::cpp module from CPAN.

[...]

The problems of -P include, but are not limited to:

*         The "#!" line is stripped, so any switches there don't
          apply.

*         A "-P" on a "#!" line doesn't work.

<end quote>

> I tried this in 2 ways, and failed both ways:
>
> 1) I tried at the beginning of the perl script to put:
> #!/usr/bin/perl -P
> but by running the script I get:
> "Can't emulate -P on #! line at a.pl line 1."

This is expressly documented in the POD as not doing what you expect.

> 2) By running the perl with -P option I always get "The system cannot
> find the path specified.". Any clue why do I get this error? Any clue
> how could I fix this?

Maybe try Filter::cpp, which I presume is more portable? Before cringing
at the fact that it's in the Filter namespace, remember that cpp itself
is one of the oldest and most venerable of filters in existence. :)

Best Regards,
Christopher Nehren
-- 
I abhor a system designed for the "user", if that word is a coded
pejorative meaning "stupid and unsophisticated". -- Ken Thompson
If you ask the wrong people questions, you get "Joel on Software".
Unix is user friendly. However, it isn't idiot friendly.


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

Date: Fri, 03 Jun 2005 15:56:05 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: counting word occurances 
Message-Id: <p4%ne.14893$w21.2205@newsread3.news.atl.earthlink.net>

"Rodrick Brown" <rodrick.brown@gmail.com> wrote in message
news:c7Qne.4832$jU5.1473111@twister.nyc.rr.com...
> Hello,
>
> Just learning Perl so bare with me.
>
> I have the following output file:
>
> pear
>  apple
> apple
>    orange
> mango
> mango
>         pear
>    cherry
> apple
>
> ill would like the count the ammount of occurances for each fruit.
>
> I spent a few hours trying to do this and just gave up if someone can help
> me out with an example or a better way to do this than the method i'm
trying
> to use

See Chaper 17, Exercise 2, in Learning Perl, 2ed.
--
Bill Segraves




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

Date: Fri, 03 Jun 2005 16:07:38 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: counting word occurances 
Message-Id: <ef%ne.14896$w21.4181@newsread3.news.atl.earthlink.net>

"Rodrick Brown" <rodrick.brown@gmail.com> wrote in message
news:c7Qne.4832$jU5.1473111@twister.nyc.rr.com...
> Hello,
>
> Just learning Perl so bare with me.

See Chapter 17, Exercise 2, p. 176, in Learning Perl, 2nd ed., as well as
"one way to do it" in Appendix A, p. 233 .
--
Bill Segraves





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

Date: Fri, 3 Jun 2005 07:47:01 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: counting word occurances
Message-Id: <slrnda0ka5.5ei.tadmc@magna.augustmail.com>

John Bokma <john@castleamber.com> wrote:
> Jürgen Exner wrote:
> 
>> delete $cnt{''}; #delete empty key in case we picked up an empty line
> 
> Must remember that one, more readable then next if $line eq '';


More readable than what I use too:

   next unless length $line;


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


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

Date: Fri, 03 Jun 2005 14:11:15 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: counting word occurances
Message-Id: <7yZne.3$nr3.0@trnddc02>

Tad McClellan wrote:
> John Bokma <john@castleamber.com> wrote:
>> Jürgen Exner wrote:
>>
>>> delete $cnt{''}; #delete empty key in case we picked up an empty
>>> line
>>
>> Must remember that one, more readable then next if $line eq '';
>
> More readable than what I use too:
>   next unless length $line;

I think my approach should be faster, too, because it eliminates the "if" 
test for every single line.

jue 




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

Date: Fri, 03 Jun 2005 17:14:05 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: counting word occurances
Message-Id: <3gbaaiFbl35mU1@individual.net>

Jürgen Exner wrote:
> A. Sinan Unur wrote:
>> "Jürgen Exner" <jurgenex@hotmail.com> wrote in
>>>
>>>     s/^\s*//; #remove leading white space
>>>     s/\s*$//; #remove trailing white space
>>
>> Or:
>>   next unless /^\s*(\w+)\s*$/;
> 
> See
>     perldoc -q "strip blank"

That FAQ entry comments on the s/// operator. Is that applicable to 
capturing a value via the m// operator too?

> Another difference between our solutions would be the handling of lines that 
> contain more than one single word, e.g. "green grapes" or "mini-tomatos".

     while (<LOG>) { /(\S(?:.*\S))/ and $cnt{$1}++ or next }

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Fri, 03 Jun 2005 15:13:52 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: counting word occurances
Message-Id: <Xns966A7207CACCasu1cornelledu@127.0.0.1>

"Jürgen Exner" <jurgenex@hotmail.com> wrote in
news:V3Yne.1300$mb2.1255@trnddc07: 

> A. Sinan Unur wrote:
>> "Jürgen Exner" <jurgenex@hotmail.com> wrote in
>>>     s/^\s*//; #remove leading white space
>>>     s/\s*$//; #remove trailing white space
>>
>> Or:
>>   next unless /^\s*(\w+)\s*$/;
> 
> See
>     perldoc -q "strip blank"

Hasty post on my part. However, note a couple of differences between the 
comparison in the FAQ and my suggestion:

    Although the simplest approach would seem to be

        $string =~ s/^\s*(.*?)\s*$/$1/;

    not only is this unnecessarily slow and destructive, it also fails
    with embedded newlines.

Well, my suggestion does not involve s///, so the bit about 
'destructive' is not applicable. Embedded newlines also are not an issue 
because we are reading line-by-line from a file. As for speed:

#! /usr/bin/perl

use strict;
use warnings;

use Benchmark ':all';

my $INPUT = [
'pear        ',
' apple              ',
'apple',
'   orange        ',
'       mango                  ',
'mango',
'        pear',
'   cherry               ',
'apple',
'',
];

sub capture {
    my @input = @{ $INPUT };
    my %counts;

    for (@input) {
        if( /^\s*(\w+)\s*$/ ) {
            $counts{$1}++;
        }
    }
}

sub strip {
    my @input = @{ $INPUT };
    my %counts;

    for (@input) {
        s/^\s*//;
        s/\s*$//;
        $counts{$_}++;
    }
    delete $counts{''};
}

cmpthese 0, {
    capture => \&capture,
    strip   => \&strip,
};

__END__

D:\Home> perl -v
This is perl, v5.8.6 built for MSWin32-x86-multi-thread

D:\Home> st
           Rate capture   strip
capture 29936/s      --     -2%
strip   30640/s      2%      --

OK, you have a point there (and I knew it even before I ran the 
benchmark.

> Another difference between our solutions would be the handling of
> lines that contain more than one single word, e.g. "green grapes" or
> "mini-tomatos". Which behaviour the OP wants is everybody's guess.

On the other hand, *this* is the crux of the matter, isn't it? Being as 
expressive as one can be (in Perl) about what part of the input string 
one wants to use enables others to be able to figure out what the code 
was meant to do. So, in that sense, me using (\w+) is not such a good 
idea. After all, words really do not contain digits.

So, I might even use:

        if( /^\s*([[:alpha:]]+)\s*$/ ) {
            $counts{$1}++;
        }

or even

my %accept = map { $_ => 1 } qw{pear apple mango cherry};

 ...

        if( /^\s*(.+?)\s*$/ and $accept{$1}) {
            $counts{$1}++;
        }

This is even slower, but it allows me to count only the input I want to 
count.

There is some value in that.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

Date: Fri, 03 Jun 2005 17:08:39 GMT
From: vali <vticau@excite.com>
Subject: Re: counting word occurances
Message-Id: <r80oe.6452$II3.3529@news.cpqcorp.net>

Jürgen Exner wrote:
> A. Sinan Unur wrote:
> 
>>"Jürgen Exner" <jurgenex@hotmail.com> wrote in
>>
>>>    s/^\s*//; #remove leading white space
>>>    s/\s*$//; #remove trailing white space
>>
>>Or:
>>  next unless /^\s*(\w+)\s*$/;
> 
> 
> See
>     perldoc -q "strip blank"
> 
> Another difference between our solutions would be the handling of lines that 
> contain more than one single word, e.g. "green grapes" or "mini-tomatos". 
> Which behaviour the OP wants is everybody's guess.
> 
> jue 
> 
> 

Wasn't aware about the above faq. I've been using for years:
s/(^\s+|\s+$)//g;
which seems to be the same (or not ?!) as:
s/^\s*//; s/\s*$//;

__Vali


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

Date: Fri, 03 Jun 2005 19:20:09 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: counting word occurances
Message-Id: <3gbhtmFblvbgU1@individual.net>

Gunnar Hjalmarsson wrote:
> Jürgen Exner wrote:
>> A. Sinan Unur wrote:
>>> "Jürgen Exner" <jurgenex@hotmail.com> wrote in
>>>>
>>>>     s/^\s*//; #remove leading white space
>>>>     s/\s*$//; #remove trailing white space
>>>
>>> Or:
>>>   next unless /^\s*(\w+)\s*$/;
>>
>> See
>>     perldoc -q "strip blank"
> 
> That FAQ entry comments on the s/// operator. Is that applicable to 
> capturing a value via the m// operator too?

Judging from Sinan's benchmark it's not.
http://groups-beta.google.com/group/comp.lang.perl.misc/msg/76160d73413fba1c

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Fri, 3 Jun 2005 18:02:46 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: counting word occurances
Message-Id: <Xns966A8FCEC112asu1cornelledu@132.236.56.8>

vali <vticau@excite.com> wrote in
news:r80oe.6452$II3.3529@news.cpqcorp.net: 

> Jürgen Exner wrote:

>> See
>>     perldoc -q "strip blank"
>> 
>> Another difference between our solutions would be the handling of
>> lines that contain more than one single word, e.g. "green grapes" or
>> "mini-tomatos". Which behaviour the OP wants is everybody's guess.

 ...

> Wasn't aware about the above faq. I've been using for years:
> s/(^\s+|\s+$)//g;
> which seems to be the same (or not ?!) as:
> s/^\s*//; s/\s*$//;


Not functionally the same. Your expression requires at least one \s either 
at the beginning or the end.

Second, it uses alternation in the regex which is generally more expensive.

Third, you are unnecessarily capturing.

use strict;
use warnings;
#! /usr/bin/perl

use strict;
use warnings;

use Benchmark ':all';

my $INPUT = [
'pear        ',
' apple              ',
'apple',
'   orange        ',
'       mango                  ',
'mango',
'        pear',
'   cherry               ',
'apple',
'',
];

sub faq {
    my @input = @{ $INPUT };
    for (@input) {
        s/^\s*//;
        s/\s*$//;
    }
}

sub vali {
    my @input = @{ $INPUT };
    for (@input) {
        s/(:?^\s*)|(:?\s*$)//g;
    }
}    

cmpthese 0, {
    faq  => \&faq,
    vali => \&vali,
};

__END__

D:\Home>perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread

D:\Home>perl t.pl
       Rate vali  faq
vali 2655/s   -- -63%
faq  7198/s 171%   --


Oooops!

Sinan


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

Date: Fri, 03 Jun 2005 19:46:41 +0200
From: Hessu <hemulaattori@invalid.yahoo.com>
Subject: Shared perl reposity
Message-Id: <d7q14l$oi8$1@phys-news1.kolumbus.fi>

Hello,

I have a shared common directory for users (TOOLDIR)to have tools I 
wrote available for them.

Everybody is mapping TOOLDIR and adding it to path to use my tools.
But they have to install required perl version and install required 
modules locally on their PCs to make everything working.
And sadly if tools got any modules updated, users have to update them 
too, locally.

1) Is it practically possible to install perl modules in TOOLDIR so they 
would be available to users without continuing local module updates?

2) Is it practically possible to install perl in TOOLDIR so that would 
be available so that users doesn't have to install perl locally or they 
can keep their own local perl version?

3) Would there be any other scripting or tool language with which this 
would be easy or even possible?

Thank you, Yours

Hessu


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

Date: Fri, 03 Jun 2005 17:56:04 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Shared perl reposity
Message-Id: <d7q234$i3f$1@redhat2.bham.ac.uk>

Hessu wrote:

> I have a shared common directory for users (TOOLDIR)to have tools I 
> wrote available for them.
> 
> Everybody is mapping TOOLDIR and adding it to path to use my tools.
> But they have to install required perl version and install required 
> modules locally on their PCs to make everything working.
> And sadly if tools got any modules updated, users have to update them 
> too, locally.
> 
> 1) Is it practically possible to install perl modules in TOOLDIR so they 
> would be available to users without continuing local module updates?

Yes, just include TOOLDIR in @INC.  (Using the PERL5LIB envronment 
variable).

> 2) Is it practically possible to install perl in TOOLDIR so that would 
> be available so that users doesn't have to install perl locally or they 
> can keep their own local perl version?

There should be no problem on most operating systems so long as the 
contents TOOLDIR is accessed using the same path on all clients (i.e. 
they map it so the same local drive or directory).



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

Date: Fri, 03 Jun 2005 20:22:06 +0200
From: Hemis <hemulaattori@invalid.yahoo.com>
Subject: Re: Shared perl reposity
Message-Id: <d7q372$sn7$1@phys-news1.kolumbus.fi>

Brian McCauley wrote:
> Hessu wrote:
> 
>> I have a shared common directory for users (TOOLDIR)to have tools I 
>> wrote available for them.
>>
>> Everybody is mapping TOOLDIR and adding it to path to use my tools.
>> But they have to install required perl version and install required 
>> modules locally on their PCs to make everything working.
>> And sadly if tools got any modules updated, users have to update them 
>> too, locally.
>>
>> 1) Is it practically possible to install perl modules in TOOLDIR so 
>> they would be available to users without continuing local module updates?
> 
> 
> Yes, just include TOOLDIR in @INC.  (Using the PERL5LIB envronment 
> variable).
> 
>> 2) Is it practically possible to install perl in TOOLDIR so that would 
>> be available so that users doesn't have to install perl locally or 
>> they can keep their own local perl version?
> 
> 
> There should be no problem on most operating systems so long as the 
> contents TOOLDIR is accessed using the same path on all clients (i.e. 
> they map it so the same local drive or directory).
> 

Thank you for your answers.

Actually, mapping the dir to agreed drive ("Everybody must use T: for 
tools-yeah it's windows!") is what we do.


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

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


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