[21973] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4195 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 28 14:10:58 2002

Date: Thu, 28 Nov 2002 11:10:19 -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           Thu, 28 Nov 2002     Volume: 10 Number: 4195

Today's topics:
    Re: numbering forms (flinky)
    Re: Perl Validation <nobody@dev.null>
        Problem counting the number of lines in a file... <rubberducky703@hotmail.com>
    Re: Problem counting the number of lines in a file... <nobull@mail.com>
        Processing command line. <fred.nospam@unique.com>
    Re: Processing command line. <spp@monaco377.com>
    Re: Processing command line. <thepoet@nexgo.de>
    Re: Regular expression question <bart.lateur@pandora.be>
        Resolving Physical Path jsmoriss@mvlan.net
    Re: Resolving Physical Path <jurgenex@hotmail.com>
    Re: Resolving Physical Path (Ben Morrow)
    Re: Resolving Physical Path <nobull@mail.com>
    Re: rounding numbers (Helgi Briem)
    Re: rounding numbers <flavell@mail.cern.ch>
    Re: rounding numbers <BROWNHIK@Syntegra.Bt.Co.Uk>
    Re: rounding numbers <skilchen@swissonline.ch>
        segfault when using 5.8.0 but not with 5.6.1 (Ari Jolma)
    Re: segfault when using 5.8.0 but not with 5.6.1 <goltermann@attbi.com>
    Re: segfault when using 5.8.0 but not with 5.6.1 <joe+usenet@sunstarsys.com>
    Re: split binary data <zentara@highstream.net>
        substitue problem <carlos@pez.dk>
    Re: substitue problem (Helgi Briem)
    Re: substitue problem (Ben Morrow)
    Re: substitue problem <carlos@pez.dk>
        text extraction on one line? <andel@ifi.unizh.ch>
    Re: text extraction on one line? (Tad McClellan)
        Use package only if installed (Mike Bobbitt)
    Re: Use package only if installed <nospam@nospam.org>
    Re: Use package only if installed <nobull@mail.com>
    Re: What is $a,$b <nospam1102@joesbox.cjb.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 28 Nov 2002 08:41:50 -0800
From: alanluc@rogers.com (flinky)
Subject: Re: numbering forms
Message-Id: <9b921b8d.0211280841.841d5e1@posting.google.com>

Sorry about it, but i could not find the post so I thought something
went wrong along the way and did not get posted.  Thanks for the link
I'll take a look at it.

roberson@ibd.nrc.ca (Walter Roberson) wrote in message news:<as3fit$nl9$1@canopus.cc.umanitoba.ca>...
> In article <9b921b8d.0211271132.1c7732f5@posting.google.com>,
> flinky <alanluc@rogers.com> wrote:
> :I would like to have forms submitted from the web site
> :that have sequential tracking numbers. I can easily create the forms that
> :will post the data that they want, but how can I increment and post a number
> :with each submitted form?
> 
> I don't mean to sound rude, but this is essentially the same question
> you asked yesterday, and you don't appear to have taken into account
> the several replies that were posted, some of which gave explicit
> answers. Please review the previous replies and either use the
> solutions contained there, or answer the requests for clarifications
> already posted. 
> 
> If your site has lost the replies, you can review them at
> http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&threadm=9b921b8d.0211261238.2f3bd33c%40posting.google.com


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

Date: Thu, 28 Nov 2002 14:06:15 GMT
From: Andras Malatinszky <nobody@dev.null>
Subject: Re: Perl Validation
Message-Id: <3DE6227C.4040504@dev.null>



Gareth Glaccum wrote:

> Are there any standards or validations that perl has passed?
> For example, has the code been tested to a def-stan?
> I have seen comments about it being used for the UK Air traffic
> control, did this happen, and how was it validated safe for use?
> Gareth



Typing perldoc -q certified brings this up:

Found in perlfaq2.pod
   Is there an ISO or ANSI certified version of Perl?

  Certainly not. Larry expects that he'll be certified before perl is.







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

Date: Thu, 28 Nov 2002 17:01:15 -0000
From: "Rubber Duck" <rubberducky703@hotmail.com>
Subject: Problem counting the number of lines in a file...
Message-Id: <as5i1l$nf9b6$1@ID-116287.news.dfncis.de>

OK i tried it myself with this code i wrote:

#!/usr/bin/perl -w
use strict;
open(fileIN, "<$ARGV[0]") or die ("\nCan't read $ARGV[0]: $!\n");
my $data = my @data = <fileIN>;
close(fileIN);
print "File ".$ARGV[0]." contains ".$data." rows\n";
I got "File data.txt contains 14415 rows"

I thought this was a little low so i trawled the web and found this in a
perl FAQ (5.3):

        $lines = 0;
        open(FILE, $filename) or die "Can't open `$filename': $!";
        while (sysread FILE, $buffer, 4096) {
            $lines += ($buffer =~ tr/\n//);
        }
        close FILE;
I ran it, and this also gives me $lines set to 14415.

So you might think that the file data.txt contained 14415 rows.  Would i be
posting here if it was??
I copied the data into word and did a word count, which also gives me a
paragraph count... etc etc and importantly a line count.
According to word the number of lines is 20766 which is alot more feasible,
and more what i would expect it to be.

I created data.txt myself and it contains a \n seperated records.  No funny
games nothing just a standard flat file.

How can it be that that the number of records actually counted is so much
less than there actually are??

Thanks
RD





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

Date: 28 Nov 2002 17:54:02 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Problem counting the number of lines in a file...
Message-Id: <u9bs49zikl.fsf@wcl-l.bham.ac.uk>

"Rubber Duck" <rubberducky703@hotmail.com> writes:

> OK i tried it myself with this code i wrote:
> 
> #!/usr/bin/perl -w
> use strict;
> open(fileIN, "<$ARGV[0]") or die ("\nCan't read $ARGV[0]: $!\n");
> my $data = my @data = <fileIN>;
> close(fileIN);

Why would you want to read the whole file into memory just to count
the lines?  It is sufficient to hold only one line at a time in
memory.

my $data; # Damn silly name for the number of lines.
$data++ while <fileIN>;

> I got "File data.txt contains 14415 rows"
> 
> So you might think that the file data.txt contained 14415 rows.

Yes, this is what I think.

> Would i be  posting here if it was??

Evidently you are.

> I copied the data into word and did a word count, which also gives me a
> paragraph count... etc etc and importantly a line count.
> According to word the number of lines is 20766 which is alot more feasible,
> and more what i would expect it to be.

> How can it be that that the number of records actually counted is so much
> less than there actually are??

A better question is: Why do you think that the number of lines that a
word processor estimates would be needed to print out a word processed
document would be the same as the number of lines in a flat text file
that had been imported into the word processor?  (Hint: word
processors usually do some sort of line wrapping, flat text files can
contain lines of arbitrary lenght).

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


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

Date: Thu, 28 Nov 2002 16:33:21 +0000
From: Fred Clausen <fred.nospam@unique.com>
Subject: Processing command line.
Message-Id: <XzrF9.129$6Q3.1568@news.uk.colt.net>

Hi All,

I wish to have my program accept options like so :

myprog --start something

or

myprog --stop something

Now I am having problems extracting either --start and --stop or 
"something". I am using Getopt::Long to do the processing. Here is an 
example of code where I can get the "something" but not "--start" or 
"--stop" :

use Getopt::Long;

$success = GetOptions (
        "start=s" => \$start,
        "stop=s" => \$stop
        );

This will assign the value of "something" to $start or $stop. But now I 
dont know whether to start or stop the "something" because I only have 
"something" on $start or $stop. 

Here is an example of code where I can find out whether to start or stop 
but I cannot get the value "something" assigned :

use Getopt::Long;

$success = GetOptions (
       "start=s" => sub { $operation = "start" },
       "stop=s" => sub { $operation = "stop" }
);

Now I know that I must where I must start or stop "something" but I have no 
way of assigning "something" to a variable. Is there a way of both setting 
a flag ($operation in this case) and getting the value of what just 
followed that flag. I guess it boils down to : how to I extract bare 
options not preceeded by anything?

I could just do this :

myprog --start  --prog something 

But I would really like the behaviour mentioned in the first example.

Any help would be appreciated.

Kind regards,
Fred
--
Remove nospam to get real address.


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

Date: Thu, 28 Nov 2002 17:41:44 +0100
From: =?ISO-8859-15?Q?S=E9bastien?= Cottalorda <spp@monaco377.com>
Subject: Re: Processing command line.
Message-Id: <3de6477b$0$2150$626a54ce@news.free.fr>

Fred Clausen wrote:

> Hi All,
> 

Hi,

> [sniped]

> Here is an example of code where I can find out whether to start or stop
> but I cannot get the value "something" assigned :
> 
> use Getopt::Long;
> 
> $success = GetOptions (
>        "start=s" => sub { $operation = "start" },
>        "stop=s" => sub { $operation = "stop" }
> );

print "$operation @ARGV\n";

Doing that, you keep start or stop and you obtain the "something" value(s) 
in @ARGV.

Hope this helps.

Sebastien



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

Date: Thu, 28 Nov 2002 18:14:34 +0100
From: "Christian Winter" <thepoet@nexgo.de>
Subject: Re: Processing command line.
Message-Id: <as5its$slq$1@newsread1.arcor-online.net>

"Fred Clausen" <fred.nospam@unique.com> wrote:
> Hi All,
>
> I wish to have my program accept options like so :
>
> myprog --start something
>
> or
>
> myprog --stop something
>
> Now I am having problems extracting either --start and --stop or
> "something". I am using Getopt::Long to do the processing. Here is an
> example of code where I can get the "something" but not "--start" or
> "--stop" :
>
> use Getopt::Long;
>
> $success = GetOptions (
>         "start=s" => \$start,
>         "stop=s" => \$stop
>         );
>
> This will assign the value of "something" to $start or $stop. But now I
> dont know whether to start or stop the "something" because I only have
> "something" on $start or $stop.

Hi,

near the end of the Getopt::Long documentation, there is
an example of storing the GetOptions-results into a hash,
which might do what you want.

I tried this snippet:
------------------------------------------------------
#!/usr/bin/perl -w

use Getopt::Long;
my %hash;

my $success = GetOptions ( \%hash, 'start=s', 'stop=s' );
print "$_ -> $hash{$_}\n" foreach( keys %hash );
------------------------------------------------------
and called it like
$ myprog --start application
which gave me
start -> application

You can test with exists() on the hash for which
action was intended. Still keeping in the eye that this makes
it possible to enter something like
$ myprog --start one --stop other

HTH
-Christian



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

Date: Thu, 28 Nov 2002 17:42:53 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Regular expression question
Message-Id: <jukcuu83nja0878ht1pcn8gpqv72q5bq8c@4ax.com>

Steven May wrote:

>my $string = '1234';
>
>$string =~ s/(\d\d)(\d\d)/\1:\2/;

You shouldn't be using backquotes on the substitution part -- which is a
doublequotish string in Perl. Use dollar signs instead, just like in
normal such strings.

	$string =~ s/(\d\d)(\d\d)/$1:$2/;

"\1" etc. as you use it is reserved for backreferences inside the regex,
i.e. the part on the left:

	$_ = 'He said "go ahead"';
	/(['"])(.*?)\1/ and print "Extracted quoted string: $2\n";

In a string, "\2" means chr(2). As in:

	$string =~ s/(\d\d)(\d\d)/"$1:$2"/e;
vs.
	$string =~ s/(\d\d)(\d\d)/"\1:\2"/e;


-- 
	Bart.


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

Date: Thu, 28 Nov 2002 15:45:31 GMT
From: jsmoriss@mvlan.net
Subject: Resolving Physical Path
Message-Id: <vSqF9.1055$6X.14387@wagner.videotron.net>

Hi everyone,

I've done quite a bit of searching and thinking on this one, but can't
find a solution. I need to resolve the real/physical path of a file.
i.e. I have a path which includes several directories and a filename -
some of these directories, or even the filename, might be symbolic
links. I need to resolve this path back to the actual physical path.
Anyone know of a quick/efficient way of doing this?

Thanks,
js.
-- 
Jean-Sebastien Morisset, Sr. UNIX Administrator <jsmoriss@mvlan.net>
Personal Homepage <http://jsmoriss.mvlan.net:8080/>


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

Date: Thu, 28 Nov 2002 16:13:09 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Resolving Physical Path
Message-Id: <pgrF9.27$361.20@nwrddc04.gnilink.net>

jsmoriss@mvlan.net wrote:
> I've done quite a bit of searching and thinking on this one, but can't
> find a solution. I need to resolve the real/physical path of a file.
> i.e. I have a path which includes several directories and a filename -
> some of these directories, or even the filename, might be symbolic
> links. I need to resolve this path back to the actual physical path.
> Anyone know of a quick/efficient way of doing this?

I think we had this question as an excercise in our Unix class some 20 years
back.
The best way is to approach it backward. Read the last directory and follow
the parent dir entries upward. Eventually you will find a directory, where
'..' points to itself.
At that moment you found the root of a file system (and your quest is over
because this file system can be mounted someplace else tomorrow).
If you still need more path then you will have to look up the mount table.

None of this has anything to do with Perl of course.
But I have a vague recollection that there is some Module function which
will do this already. Sorry, no details.

jue




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

Date: Thu, 28 Nov 2002 17:11:18 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: Resolving Physical Path
Message-Id: <as5inm$o0n$1@wisteria.csv.warwick.ac.uk>

jsmoriss@mvlan.net wrote:
>Hi everyone,
>
>I've done quite a bit of searching and thinking on this one, but can't
>find a solution. I need to resolve the real/physical path of a file.
>i.e. I have a path which includes several directories and a filename -
>some of these directories, or even the filename, might be symbolic
>links. I need to resolve this path back to the actual physical path.
>Anyone know of a quick/efficient way of doing this?

perldoc Cwd

Ben


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

Date: 28 Nov 2002 17:32:30 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Resolving Physical Path
Message-Id: <u9k7ixzjkh.fsf@wcl-l.bham.ac.uk>

jsmoriss@mvlan.net writes:

> I've done quite a bit of searching and thinking on this one, but can't
> find a solution. 

perldoc Cwd

> I need to resolve the real/physical path of a file.

95% of the time when people believe they need to do this they are
wrong.  Indeed I consider this to be the litmus test for knowing if
someone is a native Unix thinker or not.  If your instinctive reaction
is always to think "I need to resolve the real/physical path of a
file" then you are not a native Unix thinker.

It's possible that your case is in the 5% but I advise you strongly to
consider you reasons for wanting to do this.  If they are intuative
reasons as opposed to hard "it will fail if I don't" reasons then they
you are probably being mis-lead by your intuition.  Be warned the
intuative reaction is often so stong that even after they've be warned
they still fool themselves.

As general rule it is best to always access a file by whatever means
you were told to access it rather than to try to double-guess the
situation.

This, of course, has nothing to do with Perl.


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

Date: Thu, 28 Nov 2002 14:18:48 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: rounding numbers
Message-Id: <3de624b9.3755204869@news.cis.dfn.de>

On Thu, 28 Nov 2002 13:05:29 -0000, "Kevin Brownhill"
<BROWNHIK@Syntegra.Bt.Co.Uk> wrote:

>As you have quoted IEEE, here is part of what the IEEE says:
>"Therefore round(0.5) is 0 and round(-1.5) is -2."

Yep.

>This is one way of rounding, and is the way sprintf() does it. 
>However it is inconsistent - 

It is consistent given the rules of proper rounding.

>sometimes it rounds away from zero and other times toward zero.

Yep.

>If you want to specify how rounding should be done, the arithmetic
>way is the best way.

Nope.

>If you want perl to do it its way, then sprintf() is better.

You misunderstand my point.  Your way is simply
*wrong*.  Yes, it may be what you learned in elementary
school, I certainly did, but it has been found to be a
biased and fundamentally incorrect way to round numbers.

The way Perl does it using sprintf or whatever is simply
the *correct* way. The same  applies to all other rounding 
methods in all other programming languages, spreadsheet 
software, maths packages, financial applications etc etc.etc
-- 
Regards, Helgi Briem
helgi AT decode DOT is

                           A: Top posting
                           Q: What is the most irritating thing on Usenet?
                                           - "Gordon" on apihna


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

Date: Thu, 28 Nov 2002 15:31:39 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: rounding numbers
Message-Id: <Pine.LNX.4.40.0211281527060.30994-100000@lxplus074.cern.ch>

On Nov 28, Kevin Brownhill inscribed on the eternal scroll:

> As you have quoted IEEE, here is part of what the IEEE says:
> "Therefore round(0.5) is 0 and round(-1.5) is -2."
>
> This is one way of rounding, and is the way sprintf() does it. However it is
> inconsistent -

No it isn't.

> somtimes it rounds away from zero and other times toward zero.

Just what I said: consistently!

If you want to make it _in_consistent, you would want to round towards
zero, thus making double the range of numbers round to zero that round
to any other value.



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

Date: Thu, 28 Nov 2002 15:38:28 -0000
From: "Kevin Brownhill" <BROWNHIK@Syntegra.Bt.Co.Uk>
Subject: Re: rounding numbers
Message-Id: <as5dln$4dv$1@pheidippides.axion.bt.co.uk>


"Helgi Briem" <helgi@decode.is> wrote in message
news:3de624b9.3755204869@news.cis.dfn.de...
> On Thu, 28 Nov 2002 13:05:29 -0000, "Kevin Brownhill"
> <BROWNHIK@Syntegra.Bt.Co.Uk> wrote:
>
> >As you have quoted IEEE, here is part of what the IEEE says:
> >"Therefore round(0.5) is 0 and round(-1.5) is -2."
>
> Yep.
>
> >This is one way of rounding, and is the way sprintf() does it.
> >However it is inconsistent -
>
> It is consistent given the rules of proper rounding.
>
> >sometimes it rounds away from zero and other times toward zero.
>
> Yep.
>
> >If you want to specify how rounding should be done, the arithmetic
> >way is the best way.
>
> Nope.
>
> >If you want perl to do it its way, then sprintf() is better.
>
> You misunderstand my point.  Your way is simply
> *wrong*.  Yes, it may be what you learned in elementary
> school, I certainly did, but it has been found to be a
> biased and fundamentally incorrect way to round numbers.
>
> The way Perl does it using sprintf or whatever is simply
> the *correct* way. The same  applies to all other rounding
> methods in all other programming languages, spreadsheet
> software, maths packages, financial applications etc etc.etc

But some people may want to round in the way they learned in elementary
school, college or university - it may be in the spec.

They may not care if it is statistically balanced, or whether someone has
recently discovered that it is fundamentally incorrect.

My way may be *wrong* according to your definition, but it is what may
people believe it to be, including the writers of Excel, and even if the
customer is sometimes *wrong* they only pay if they get what they want.

As I said, the arithmetic way is best if you want to specify how the
rounding is to work. The formula I originally supplied can be modified
transparently to give whatever type of rounding is required.





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

Date: Thu, 28 Nov 2002 19:10:14 +0100
From: "Samuel Kilchenmann" <skilchen@swissonline.ch>
Subject: Re: rounding numbers
Message-Id: <as5m8o$odqii$1@ID-13368.news.dfncis.de>

"Helgi Briem" <helgi@decode.is> wrote in
news:3de4a382.3656581255@news.cis.dfn.de...
>
> See the" ANSI/IEEE Std 754-1985, IEEE
> Standard for Binary Floating-Point Arithmetic, IEEE, New
> York, 1985." for example:
> http://www.psc.edu/general/software/packages/ieee/ieee.html
>
This standard has almost nothing to do with the way how your (s)printf
rounds. The rounding in (s)printf depends on the preferences of the
authors of the C runtime library used to compile Perl. E.g. the 
(s)printf in the MS C library doesn't implement the rounding rule
"round to nearest even" but the rule "round away from zero". The
following case has nothing to do with these two rounding rules:

On a computer running Windows XP
printf("%.2f", 0.155)
prints
0.16 if you use perl, v5.6.1 built for MSWin32-x86-multi-thread
0.15 if you use perl, v5.6.1 built for cygwin-multi

Try to explain that difference, given the fact that both Perls use the
same binary floating-point approximation of 0.155 as
5584463537939415/36028797018963968


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

Date: Thu, 28 Nov 2002 16:04:39 +0000 (UTC)
From: ajolma@water.hut.fi (Ari Jolma)
Subject: segfault when using 5.8.0 but not with 5.6.1
Message-Id: <as5eqn$kv4$1@water.hut.fi>

The following code segfaults on my machine for some reason, does
somebody have an idea what is going on?

The code does not segfault in an another machine with perl 5.6.1
(and slightly older Readline).

code:

#!/usr/bin/perl -w

use FileHandle;
use Math::FFT;
use Term::ReadLine;

my $term = new Term::ReadLine 'shell';
$_ = $term->readline('>');
print "\n";

I'm using fresh new perl 5.8.0 flawlessly compiled on a redhat
7.3. FileHandle is provided by perl distro, Math::FFT is 0.25,
Term::ReadLine is 1.13 (gnu). Math::FFT is quite straightforward
module which uses FFT C code.

gdb says:

Program received signal SIGSEGV, Segmentation fault.
0x400c310a in fclose@@GLIBC_2.1 () from /lib/libc.so.6
(gdb) bt
#0  0x400c310a in fclose@@GLIBC_2.1 () from /lib/libc.so.6
#1  0x080e518d in Perl_PerlIO_close ()
#2  0x080e438f in PerlIO_cleantable ()
#3  0x080e607b in PerlIO_cleanup ()
#4  0x0805d5ff in perl_destruct ()
#5  0x0805c8f4 in main ()
#6  0x400741c4 in __libc_start_main () from /lib/libc.so.6

Probably the thing giving trouble could be extracted still more
but anyway this uses only latest things from CPAN.

Ari


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

Date: Thu, 28 Nov 2002 16:45:18 GMT
From: Wilbur Goltermann <goltermann@attbi.com>
Subject: Re: segfault when using 5.8.0 but not with 5.6.1
Message-Id: <yKrF9.147167$WL3.64353@rwcrnsc54>

Ari Jolma wrote:

> The following code segfaults on my machine for some reason, does
> somebody have an idea what is going on?
> 
> The code does not segfault in an another machine with perl 5.6.1
> (and slightly older Readline).
> 
> code:
> 
> #!/usr/bin/perl -w
> 
> use FileHandle;
> use Math::FFT;
> use Term::ReadLine;
> 
> my $term = new Term::ReadLine 'shell';
> $_ = $term->readline('>');
> print "\n";
> 
> I'm using fresh new perl 5.8.0 flawlessly compiled on a redhat
> 7.3. FileHandle is provided by perl distro, Math::FFT is 0.25,
> Term::ReadLine is 1.13 (gnu). Math::FFT is quite straightforward
> module which uses FFT C code.
> 
> gdb says:
> 
> Program received signal SIGSEGV, Segmentation fault.
> 0x400c310a in fclose@@GLIBC_2.1 () from /lib/libc.so.6
> (gdb) bt
> #0  0x400c310a in fclose@@GLIBC_2.1 () from /lib/libc.so.6
> #1  0x080e518d in Perl_PerlIO_close ()
> #2  0x080e438f in PerlIO_cleantable ()
> #3  0x080e607b in PerlIO_cleanup ()
> #4  0x0805d5ff in perl_destruct ()
> #5  0x0805c8f4 in main ()
> #6  0x400741c4 in __libc_start_main () from /lib/libc.so.6
> 
> Probably the thing giving trouble could be extracted still more
> but anyway this uses only latest things from CPAN.
> 
> Ari

I have had similar problems with perl 5.8.  The following 
snippet of code regularly blows up with a seg fault:

***faulty snippet***

#!/usr/bin/perl
open TEXT, "TestSong.txt" || die "Unable to open lyrics text file";
#Add the rest of the lines of the text file
print "We seem to be opening the file\n";

LYRIC:while ( $line = <TEXT> ) {
    #show the line before segfault occurs
    print "1: $line";

    #capitalize the first letter of each line
    #this line causes a segfault
        $line =~ s/^(\s*)(\w*)/$1\u$2/;

    #and show again, should we successfully 
    #execute the offending line
    print "2: $line";
}


***end of faulty snippet****


Just to be sure we end up testing the same thing, here is the 
contents of "TestSong.txt" which I used in my test:

***TestSong.txt***
Unknown

abeline, abeline prettiest town that i've ever seen
women there don't treat you mean in abeline my abeline

i sit alone most every night
watch those trains pull out of sight
don't i wish they were carrying me back to abeline my abeline

crowded city there ain't nothing free
nothing in this town for me
wish to the lord that i could be in abeline my abeline

***end of TestSong.txt***


On an equally curious note, It appears that the thing blows up
when processing blank lines.  If I preceed the faulty line
with a statement which does not process a line unless it is
non-blank, the thing works yust-fine-yah-shoor-you-betcha!
Here's a version of the snippet which does not blow up.

***working snippet***
#!/usr/bin/perl
open TEXT, "TestSong.txt" || die "Unable to open lyrics text file";
#Add the rest of the lines of the text file
print "We seem to be opening the file\n";

LYRIC:while ( $line = <TEXT> ) {
    #show the line before segfault occurs
    print "1: $line";

    #capitalize the first letter of each line
    #process the line only if non-blank
    if ( $line =! m/^$/ ) {
        $line =~ s/^(\s*)(\w*)/$1\u$2/;
    }

    #and show again, should be successfully execute the offending line
    print "2: $line";
}
***end of working snippet***

The whole thing works perfectly under 5.6.1, and even does the
intended job.  It appears that something is wrong with Perl
5.8;  it doesn't matter what you feed an interpreter - if it
is bug-free, it shouldn't seg fault under any circumstances.

Wilbur


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

Date: 28 Nov 2002 12:05:48 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: segfault when using 5.8.0 but not with 5.6.1
Message-Id: <m3vg2haakz.fsf@mumonkan.sunstarsys.com>


[ I've cc'd this post to the author of Term::ReadLine::Gnu ]

ajolma@water.hut.fi (Ari Jolma) writes:

> The following code segfaults on my machine for some reason, does
> somebody have an idea what is going on?
> 
> The code does not segfault in an another machine with perl 5.6.1
> (and slightly older Readline).
> 
> code:
> 
> #!/usr/bin/perl -w
> 
> use FileHandle;
> use Math::FFT;
> use Term::ReadLine;
> 
> my $term = new Term::ReadLine 'shell';
> $_ = $term->readline('>');
> print "\n";
> 
> I'm using fresh new perl 5.8.0 flawlessly compiled on a redhat
> 7.3. FileHandle is provided by perl distro, Math::FFT is 0.25,
> Term::ReadLine is 1.13 (gnu). 
                    ^^^^

I suspect the segfault is coming from Term::ReadLine (the stack
trace looks similar to a bug in Apache::Request wrt 5.8 PerlIO 
filehandle dup's).  However, I cannot duplicate it.

The Term::ReadLine that ships with 5.8.0 has $VERSION = "1.00".
Where did you get 1.13 from?

Ah ha- I see there's a Term::ReadLine::Gnu on CPAN with version 
1.13.  It looks to me like it has the same kind of bug as Apache::Request
(the behavior of PerlIO_importFILE has apparently changed from 5.6 to 
5.8; in  Apache::Request's Request.xs we simply stopped using that macro).

HTH
-- 
Joe Schaefer    "I've come loaded with statistics, for I've noticed that a man
                          can't prove anything without statistics."
                                               --Mark Twain



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

Date: Thu, 28 Nov 2002 09:34:30 -0500
From: zentara <zentara@highstream.net>
Subject: Re: split binary data
Message-Id: <31acuu8nk21if5vdl94dvh7ch1ctpk10k6@4ax.com>

On 28 Nov 2002 09:44:40 +0900, Takahide Nojima <nojima@taito.co.jp>
wrote:

>Bob Walton <bwalton@rochester.rr.com> writes:
>> Here is one way:
>> for(1..600*1024){$a.=chr(rand(96)+32)} #generate 600K bytes
>> @a=($a=~m/(.{10240})/g);push @a,$' if length($')>0; #10K pieces
>> print "size of a=".@a."\n"; #how many pieces
>> print join ', ',map {length} @a; #size of each piece

> If $a have utf-8 string in that code with perl 5.6 ,
> I think the size of $a[0] is not equal 10240 bytes.
> I think the solution is like this,
>
> 1. In Perl 5.6 above:
>   Someone tell me to add 'use bytes' within the code like this,
>      { use bytes;
>        for(1..600*1024){$a.=chr(rand(96)+32)} #generate 600K bytes
>        @a=($a=~m/(.{10240})/g);push @a,$' if length($')>0; #10K pieces
>        print "size of a=".@a."\n"; #how many pieces
>        print join ', ',map {length} @a; #size of each piece
>      }    
>
> 2. In less than 5.6:
>     Perl doesn't treat multi-byte charactors in any string manipulation,so
>   any binary data always treat in the unit of bytes.

I played around with Mr. Walton's script. I've found that you need to
add the s option to the regex, to make it work on text files, etc.

@a=($a=~m/(.{10240})/gs);
                                   ^^






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

Date: 28 Nov 2002 15:00:14 GMT
From: "Michael L. Hostbaek" <carlos@pez.dk>
Subject: substitue problem
Message-Id: <slrnaucbru.19m0.carlos@freebsdcluster.dk>

Group,

I have written a uploadwizard kinda thing in perl - and it basically
takes a comma/tab delimited file and processes each field, and finally
inserts it to my DB. However, one of the fields are a price field - and
some of the files being uplaoded have currency symbols in front of there
price. Fx. $1400 or $ 1400 - I have made a function that cleans my price
field for spaces and dots etc etc... I can also remove $ signs.. however
some of my clients are using sterling pound sign and euro sign - and
myvim on my unix box will not let me type a sterling pound nor euro
sign.. Could I find the hex value, and substitute on that ??

My function looks like this:
sub no_space_price {
        my $t = shift;
        $t =~ s/\s+//g;
        $t eq '' and $t = ' ';
        $t =~ s/[^\d,.$#]+//g;
        $t;
}

Any help or pointers would be much appreciated... Thanks..


-- 
Regards,
Michael L. Hostbaek
-= So long, and thanks for all the fish.. =-



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

Date: Thu, 28 Nov 2002 16:23:56 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: substitue problem
Message-Id: <3de642e4.3762928685@news.cis.dfn.de>

On 28 Nov 2002 15:00:14 GMT, "Michael L. Hostbaek"
<carlos@pez.dk> wrote:

>Ihave written a uploadwizard kinda thing in perl - and it basically
>takes a comma/tab delimited file and processes each field, and finally
>inserts it to my DB. However, one of the fields are a price field - and
>some of the files being uplaoded have currency symbols in front of there
>price. Fx. $1400 or $ 1400 - I have made a function that cleans my price
>field for spaces and dots etc etc... I can also remove $ signs.. however
>some of my clients are using sterling pound sign and euro sign - and
>myvim on my unix box will not let me type a sterling pound nor euro
>sign.. Could I find the hex value, and substitute on that ??

You could, but why not remove everything that's not a 
digit or a dot?

s/[^\d.]//g;

-- 
Regards, Helgi Briem
helgi AT decode DOT is

                           A: Top posting
                           Q: What is the most irritating thing on Usenet?
                                           - "Gordon" on apihna


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

Date: Thu, 28 Nov 2002 17:09:05 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: substitue problem
Message-Id: <as5ijh$nqh$1@wisteria.csv.warwick.ac.uk>

helgi@decode.is wrote:
>On 28 Nov 2002 15:00:14 GMT, "Michael L. Hostbaek"
><carlos@pez.dk> wrote:
>
>>Ihave written a uploadwizard kinda thing in perl - and it basically
>>takes a comma/tab delimited file and processes each field, and finally
>>inserts it to my DB. However, one of the fields are a price field - and
>>some of the files being uplaoded have currency symbols in front of there
>>price. Fx. $1400 or $ 1400 - I have made a function that cleans my price
>>field for spaces and dots etc etc... I can also remove $ signs.. however
>>some of my clients are using sterling pound sign and euro sign - and
>>myvim on my unix box will not let me type a sterling pound nor euro
>>sign.. Could I find the hex value, and substitute on that ??
>
>You could, but why not remove everything that's not a 
>digit or a dot?
>
>s/[^\d.]//g;

But do you not want to remember which currency was specified?

If you use v5.8.0, \p{Sc} will match any currency symbol.

Ben


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

Date: 28 Nov 2002 17:12:46 GMT
From: "Michael L. Hostbaek" <carlos@pez.dk>
Subject: Re: substitue problem
Message-Id: <slrnaucjkd.19m0.carlos@freebsdcluster.dk>

Ben Morrow tried to tell us something, and all I got was:
>  
>  But do you not want to remember which currency was specified?
>  
>  If you use v5.8.0, \p{Sc} will match any currency symbol.

I am using 5.6.1 - and I am getting currency elsewhere - so I just want
to clean up the field.

-- 
Regards,
Michael L. Hostbaek
-= So long, and thanks for all the fish.. =-



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

Date: Thu, 28 Nov 2002 15:36:39 +0100
From: David Andel <andel@ifi.unizh.ch>
Subject: text extraction on one line?
Message-Id: <as59ad$jkh$1@sifnos.ifi.unizh.ch>

Hi

I like the onliners enabling me to replace text in files, like perl -i -p 
-e 's/search/replace/g' $(find . -type f)

But I can't figure out how to extract text from files (either by writing it 
to another file or by deleting the text before and after the sequence). Say 
I have a bunch of files containing some text between "begin" and "end". Is 
there a simple solution to extract that text (or delete everything before 
"begin" and after "end") in one line or do I have to write a script 
therefore?

Thanks, David


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

Date: Thu, 28 Nov 2002 08:56:24 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: text extraction on one line?
Message-Id: <slrnaucbko.28d.tadmc@magna.augustmail.com>

David Andel <andel@ifi.unizh.ch> wrote:
> 
> I like the onliners enabling me to replace text in files, like perl -i -p 
> -e 's/search/replace/g' $(find . -type f)
> 
> But I can't figure out how to extract text from files (either by writing it 
> to another file or by deleting the text before and after the sequence). Say 
> I have a bunch of files containing some text between "begin" and "end". Is 
> there a simple solution to extract that text (or delete everything before 
> "begin" and after "end") in one line or do I have to write a script 
> therefore?


perl -n0777 -e 'print "$1\n---\n" while /\bbegin\b(.*?)\bend\b/sg' files...


Slurps the entire file into memory though.


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


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

Date: 28 Nov 2002 09:42:37 -0800
From: Bobbitt@CdnArmy.ca (Mike Bobbitt)
Subject: Use package only if installed
Message-Id: <8805ec26.0211280942.2d11cfba@posting.google.com>

Hi all,

I'm trying to write some code that'll use the Digest::MD5 package if
present, and skip it if not:

use Digest::MD5  qw(md5_hex);
$cryptopass=md5_hex($storedpass);

The problem is, for systems that don't have MD5, it throws an error,
but I want it to just "skip" that section and move on. Does anyone
have advice on how to do that? I've been given the following code
snippet, but can't get it working for my needs:

eval { 
  require NetUtils; 
  import Module LIST; 
}; 

if ($@) { 
  print "Could not import NetUtils. Continuing anyway...\n"; 
} 

I've been using PERL for quite some time, but the whole
use/require/import thing isn't making sense to me... :(

Thanks in advance!


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

Date: Thu, 28 Nov 2002 12:52:41 -0500
From: "Christian Caron" <nospam@nospam.org>
Subject: Re: Use package only if installed
Message-Id: <as5l5a$90v8@nrn2.NRCan.gc.ca>


"Mike Bobbitt" <Bobbitt@CdnArmy.ca> wrote in message
news:8805ec26.0211280942.2d11cfba@posting.google.com...
> Hi all,
>
> I'm trying to write some code that'll use the Digest::MD5 package if
> present, and skip it if not:
>
> use Digest::MD5  qw(md5_hex);
> $cryptopass=md5_hex($storedpass);
>

eval { require Digest::MD5 };
if ($@) {            # if nothing has been returned from the eval, which
means it is installed
        $digest = "TRUE";
}

in your program, use "if ($digest)" to determine if it is installed or
not...

Christian





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

Date: 28 Nov 2002 18:00:08 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Use package only if installed
Message-Id: <u965uhziaf.fsf@wcl-l.bham.ac.uk>

Bobbitt@CdnArmy.ca (Mike Bobbitt) writes:

> eval { 
>   require NetUtils; 
>   import Module LIST; 
           ^^^^^^^^^^^
    
These are metasyntactic variables - you should replace them with the
actual module name (ie NetUtils) and the actual argument list for
NetUtils->import().

> }; 

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


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

Date: 28 Nov 2002 18:46:02 GMT
From: Josef Drexler <nospam1102@joesbox.cjb.net>
Subject: Re: What is $a,$b
Message-Id: <as5o9a$91d$1@panther.uwo.ca>

Tad McClellan quoted from the v5.8.0 perlvar.pod:
> Special package variables when using sort(), see L<perlfunc/sort>.
> Because of this specialness $a and $b don't need to be declared
> (using local(), use vars, or our()) even when using the strict
> vars pragma.  Don't lexicalize them with C<my $a> or C<my $b>
> if you want to be able to use them in the sort() comparison block
> or function.

This is incorrect, you *can* still access them if you declare them with
"our" inside the comparison block:

use strict;
use warnings;
my $a="Hello";
my $b="World";

print join " ", sort {our ($a,$b); $a<=>$b} 32,53,6,23,1;

 ...prints:
1 6 23 32 53
just as you would expect.

I suggest the last sentence be replaced with:

Note that if you lexicalize them with C<my $a> or C<my $b>, you won't be
able to use them in the sort() comparison block or function unless you
redeclare them as package variables using C<our ($a,$b)> for the scope of
that block.

Maybe this should be mentioned in the sort() documentation too:
---------
@@ -4663,9 +4663,16 @@
     use sort '_mergesort';  # note discouraging _
     @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
 
-If you're using strict, you I<must not> declare $a
-and $b as lexicals.  They are package globals.  That means
-if you're in the C<main> package and type
+If you've declared $a and $b as lexicals, you I<must> 
+redeclare them as package variables in the scope of the
+comparison block or function:
+
+    my ($a,$b) = qw(foo bar);
+    @articles = sort {our ($a,$b); $b <=> $a} @files;
+
+The reason for this is that sort needs $a and $b to be
+package globals.  That means if you're in the C<main> package
+and type
 
     @articles = sort {$b <=> $a} @files;
 
--------

Note that this doesn't actually have anything to do with "use strict" or
"use warnings".  It's a fatal error to lexicalize $a and $b and then use
them in the sort block, regardless of whether strictures or warnings are
enabled.

Personally, I don't ever use $a or $b, but I think it would be a good idea
to encourage lexicalizing them in general.  Then, if you need them in a
sort call, just use our.

-- 
   Josef Drexler                 |    http://publish.uwo.ca/~jdrexler/
---------------------------------+----------------------------------------
 Please help Conserve Gravity    |  Email address is *valid*.
 Give up downhill skiing.        |  Don't remove the "nospam" part.


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

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


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