[31941] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3204 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 10 16:09:46 2010

Date: Wed, 10 Nov 2010 13:09:07 -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           Wed, 10 Nov 2010     Volume: 11 Number: 3204

Today's topics:
        Bugs or Documentation Deficiencies <e9427749@stud4.tuwien.ac.at>
    Re: Bugs or Documentation Deficiencies (Randal L. Schwartz)
    Re: FAQ 4.71 How can I check if a key exists in a multi sln@netherlands.com
    Re: Foreach <rvtol+usenet@xs4all.nl>
    Re: Foreach <kst-u@mib.org>
    Re: Foreach <rvtol+usenet@xs4all.nl>
    Re: testing whether a number is an integer <jurgenex@hotmail.com>
    Re: testing whether a number is an integer <jurgenex@hotmail.com>
    Re: testing whether a number is an integer <uri@StemSystems.com>
    Re: testing whether a number is an integer <jurgenex@hotmail.com>
    Re: testing whether a number is an integer <cartercc@gmail.com>
    Re: testing whether a number is an integer <kst-u@mib.org>
    Re: testing whether a number is an integer sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 10 Nov 2010 15:55:06 +0100
From: Josef <e9427749@stud4.tuwien.ac.at>
Subject: Bugs or Documentation Deficiencies
Message-Id: <4cdab24d$0$11352$3b214f66@tunews.univie.ac.at>


Hi!

Maybe i have found bugs or/and some unclearness in the documentation
of some modules in the core.

[About me:
  English is not my mother tongue (obviously).
  And most of my english experience came from user manuals.
  Because no bug description are usually in such kind of literature,
  such doing is more difficult for me, as e.g. just kidding and joking
  (yes i have read perl books also ;-).

  So i will start with some perl code with doing nothing else,
  as demonstration the diverting from my expectations.
]

The first observation is related to List::Util,
i use first() in the example below but the same behavior
can be find also e.g. in reduce().
(But not reproduceable in grep & map.)

<code>
   use strict; use warnings; use feature ':5.12';
   use List::Util   qw'reduce first';

   say '1.) localizing $_ in selecting subroutine for first:';
   $_="orginal";
   sub f0 { say "frm f0: $_";0 }
   my $dummy=first { say "frm c: $_"; local $_=10; f0() } 1..3;
   say "after first: $_";
</code>

This produce this error messages (count of listitems-1 times):
Attempt to free unreferenced scalar: SV 0x......, Perl interpreter: 0x35f54.
Beside that, the output is what i expected, but ...

<code mode='continue'>
   say '2.) localizing $a in selecting subroutine for first:';
   # or something else
   $a="orginal";
   sub f0a { say "frm f0a: $a";0 }
   $dummy=first { say "frm c: $a"; local $a=10; f0a() } 1..3;
   say "after first: $a\n";
</code>

This time no errors, but the result is strange because the
local'isation leaks into the next invocation, which is not
what i expected.

<output>
   frm c: orginal
   frm f0a: 10
   frm c: 10
   frm f0a: 10
   frm c: 10
   frm f0a: 10
   after first: orginal
</output>

Both problems have a easy workaround. Simply double the '{','}'.

<code mode='continue'>
   say '3.) localizing $a in first with workaround:';
   $dummy=first {{ say "frm c: $a"; local $a=10; f0a() }} 1..3;
   say "after first: $a\n";
</code>

<output comment='expected'>
   frm c: orginal
   frm f0a: 10
   frm c: orginal
   frm f0a: 10
   frm c: orginal
   frm f0a: 10
   after first: orginal
</output>

Another problem with go away with doubling the curly brackets
is by constructing closures inside.

reduce { my $f=$a; sub { ... $f->(...) ... } sub { 'bla' },...

produces always deep recursion where the reduce {{ ... }} variant
do not.


So imho this indicates a problem with scope.
If this is a feature or a optimization with ignores seldom usage then
i think it should at least be documented.

~~~~

The next have to do with unclear description
of calling subs in the form &fun;. So i can not say if it does what
it should do or not. The following example demonstrate that it is
not the same as for fun(@_) [no prototypes involved].

<code>
   sub f2 { shift }
   sub okidoki { f2(@_); join ',',@_ }
   say okidoki 'blabla',1..10;
   sub notwasiexpected { &f2; join ',',@_ }
   say notwasiexpected 'missing blabla',1..10;
</code>

<output>
   blabla,1,2,3,4,5,6,7,8,9,10
   1,2,3,4,5,6,7,8,9,10
</output>

~~~~

The core module 'Safe' does work differently in perl 5.12.1 as in 
version before like 5.12.0. In 5.13.x changed back to the old so far
i can guess with the help of cpantesters.org information.

I can not tell you more about this beside this is the reason that
the tests for module 'Petal' (on cpan) failed.

~~~~

The next problem is that the return context info is sometimes wrong.
But this sometimes happen by bigger applications and i can not
present a simple example to demostrate the issue.

The last time i see such behavior was in strawberry perl 5.12.1:
In that case a call to Data::Dump::dump at file scope (main::)
directly in the script file (.pl).

<code mode='missing necessary enviroment'>
   print "........\n";
   dump $var;
   print "........\n";
</code>

Where the output from dump is missed.
Changing print "....\n"; to say "....."; have changed this.
That is at least for me a little bit odd.

In older perls i have seen problems with wantarray.
But again not in a reproduceable way.



with best regards,
   Josef


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

Date: Wed, 10 Nov 2010 07:08:05 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Bugs or Documentation Deficiencies
Message-Id: <86mxphb4iy.fsf@red.stonehenge.com>

>>>>> "Josef" == Josef  <e9427749@stud4.tuwien.ac.at> writes:

Josef> Maybe i have found bugs or/and some unclearness in the documentation
Josef> of some modules in the core.

If you're filing a bug report, you *must* include the output of "perl -V",
so that people can figure out if it's an old version of Perl, or maybe a
bug that might show up only on a particular platform or set of options.

And it looks like you're trying to file a bug report against Perl, and
in the wrong place.  Use "perlbug".

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

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


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

Date: Tue, 09 Nov 2010 18:31:47 -0800
From: sln@netherlands.com
Subject: Re: FAQ 4.71 How can I check if a key exists in a multilevel hash?
Message-Id: <dq0kd6lcjc92c7t76mkj7leotsc45310kv@4ax.com>

On Tue, 09 Nov 2010 06:18:09 -0800, sln@netherlands.com wrote:

>On Tue, 9 Nov 2010 04:24:59 -0800 (PST), Marc Girod <marc.girod@gmail.com> wrote:
>
>>On Nov 9, 11:00 am, PerlFAQ Server <br...@theperlreview.com> wrote:
>>
>>>                foreach my $key ( @keys ) {
>>>                        return unless eval { exists $hash->{$key} };
>>>                        $hash = $hash->{$key};
>>>                        }
>>
>>Naive question: what is the role of the eval block above?
>>
>>Thanks,
>>Marc
>
>Presumeably its to block printing a message like this:
>
>Can't use string ("I am not a hash") as a HASH ref while "strict refs" in use at
> ...
>

It could also be done like this:

return unless ref( $hash ) eq "HASH" and exists( $hash->{$key} );



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

Date: Wed, 10 Nov 2010 04:11:09 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Foreach
Message-Id: <4cda0d4d$0$34849$e4fe514c@news.xs4all.nl>

On 2010-11-10 00:33, Martijn Lievaart wrote:
> On Tue, 09 Nov 2010 13:24:09 -0500, Uri Guttman wrote:

>> 	print "$_\n" foreach @ARGV ;
>
> Or (arguably worse)
>
> { $,="\n"; print @ARGV; }

ITYM:

{local $,=$\=$/; print @ARGV}

-- 
Ruud


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

Date: Wed, 10 Nov 2010 08:29:12 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Foreach
Message-Id: <lnsjz96t2f.fsf@nuthaus.mib.org>

"Dr.Ruud" <rvtol+usenet@xs4all.nl> writes:
> On 2010-11-10 00:33, Martijn Lievaart wrote:
>> On Tue, 09 Nov 2010 13:24:09 -0500, Uri Guttman wrote:
>
>>> 	print "$_\n" foreach @ARGV ;
>>
>> Or (arguably worse)
>>
>> { $,="\n"; print @ARGV; }
>
> ITYM:
>
> {local $,=$\=$/; print @ARGV}

That localizes $, but not $\, so the modified value of $\ is
retained after the block.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Wed, 10 Nov 2010 21:48:58 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Foreach
Message-Id: <4cdb053b$0$41110$e4fe514c@news.xs4all.nl>

On 2010-11-10 17:29, Keith Thompson wrote:
> "Dr.Ruud"<rvtol+usenet@xs4all.nl>  writes:
>> On 2010-11-10 00:33, Martijn Lievaart wrote:
>>> On Tue, 09 Nov 2010 13:24:09 -0500, Uri Guttman wrote:

>>>> 	print "$_\n" foreach @ARGV ;
>>>
>>> Or (arguably worse)
>>>
>>> { $,="\n"; print @ARGV; }
>>
>> ITYM:
>>
>> {local $,=$\=$/; print @ARGV}
>
> That localizes $, but not $\, so the modified value of $\ is
> retained after the block.

Ah, thanks for the review.

   {local($\,$,)=($/)x2;print @ARGV}

also known as

   print join( $/, @ARGV ), $/;

(though not fully equivalent)

-- 
Ruud


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

Date: Tue, 09 Nov 2010 18:18:35 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: testing whether a number is an integer
Message-Id: <2tvjd6petrncqd5420e0k7un7q81cla4d8@4ax.com>

"Uri Guttman" <uri@StemSystems.com> wrote:
>>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:
>
>  c> This is probably a Dumb Question, but I'll ask it anyway.
>  c> I have a number, which I use to validate an array, like this:
>  c> $number = scalar($@array) / 3;
>  c> If $number is an integer,
>  c> the array is perfect and can be processed.
>  c> If it isn't, it's malformed and must be written to an error log.

Is this a complicated way of testing if the number of elements in @array
is a multiple of 3?

Then why don't you use the modulo operator?
	print "Not a multiple of 3 elements" if @array % 3;

jue


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

Date: Tue, 09 Nov 2010 18:22:49 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: testing whether a number is an integer
Message-Id: <p80kd61uea1qnuu7ghmt9t7em9d4hsf0il@4ax.com>

"Uri Guttman" <uri@StemSystems.com> wrote:
>simple math will do it for you. the int func will truncate something to
>an integer. so it $foo/3 is an int, its int() value will be the same.
>
>my $int = @array/3 # no need for scalar() as / provides scalar context
>
>if ( $int == int( $int ) ) { ...

While probably not a problem in this case I would be _VERY_ wary of
using this code in general. Any rounding error caused by binary
arithmetic will bite you and therefore I would not ever use it, not even
if the division should yield an integer in the case of success.

Why not simply use modulo? It is meant for exactly this purpose and it
does not suffer from rounding errors.

jue


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

Date: Tue, 09 Nov 2010 21:30:17 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: testing whether a number is an integer
Message-Id: <87aalhdi6e.fsf@quad.sysarch.com>

>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:

  JE> "Uri Guttman" <uri@StemSystems.com> wrote:
  >> simple math will do it for you. the int func will truncate something to
  >> an integer. so it $foo/3 is an int, its int() value will be the same.
  >> 
  >> my $int = @array/3 # no need for scalar() as / provides scalar context
  >> 
  >> if ( $int == int( $int ) ) { ...

  JE> While probably not a problem in this case I would be _VERY_ wary of
  JE> using this code in general. Any rounding error caused by binary
  JE> arithmetic will bite you and therefore I would not ever use it, not even
  JE> if the division should yield an integer in the case of success.

  JE> Why not simply use modulo? It is meant for exactly this purpose and it
  JE> does not suffer from rounding errors.

i agree. it was a brain fart as i think. i was just coding in his
style. % is the right op and i should have known it.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 09 Nov 2010 19:18:33 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: testing whether a number is an integer
Message-Id: <9a3kd6149nfbll333k0t78a561qu9j0lud@4ax.com>

Jürgen Exner <jurgenex@hotmail.com> wrote:
>"Uri Guttman" <uri@StemSystems.com> wrote:
>>>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:
>>
>>  c> This is probably a Dumb Question, but I'll ask it anyway.
>>  c> I have a number, which I use to validate an array, like this:
>>  c> $number = scalar($@array) / 3;
>>  c> If $number is an integer,
>>  c> the array is perfect and can be processed.
>>  c> If it isn't, it's malformed and must be written to an error log.
>
>Is this a complicated way of testing if the number of elements in @array
>is a multiple of 3?
>
>Then why don't you use the modulo operator?
>	print "Not a multiple of 3 elements" if @array % 3;

Coming to think of it I have a very strong feeling that this is another
X-Y problem, caused by choosing a poor data structure.

If the number of elements in that array must be a multiple of 3 then
(unless there are some extraordinary circumstances) this implies that
the data is not a plain list of single elements but it is a list of
triplets. Had the OP used a proper data structure to represent this
fact, e.g. an array of triplets(*), then the integrity of his data would
be ensured by the data structure and we would not have this discussion
in the first place.
 
*: each triplet could be a hash or an array with 3 elements, depending
on the kind of data in each triplet.

jue 


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

Date: Wed, 10 Nov 2010 06:25:45 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: testing whether a number is an integer
Message-Id: <cbf5ff3c-976b-4e9f-9abc-96e535d4e5ed@fv1g2000vbb.googlegroups.com>

On Nov 9, 10:18=A0pm, J rgen Exner <jurge...@hotmail.com> wrote:
> J rgen Exner <jurge...@hotmail.com> wrote:
> >"Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>>>> "c" =3D=3D ccc31807 =A0<carte...@gmail.com> writes:
>
> >> =A0c> This is probably a Dumb Question, but I'll ask it anyway.
> >> =A0c> I have a number, which I use to validate an array, like this:
> >> =A0c> $number =3D scalar($@array) / 3;
> >> =A0c> If $number is an integer,
> >> =A0c> the array is perfect and can be processed.
> >> =A0c> If it isn't, it's malformed and must be written to an error log.
>
> >Is this a complicated way of testing if the number of elements in @array
> >is a multiple of 3?
>
> >Then why don't you use the modulo operator?
> > =A0 =A0print "Not a multiple of 3 elements" if @array % 3;
>
> Coming to think of it I have a very strong feeling that this is another
> X-Y problem, caused by choosing a poor data structure.
>
> If the number of elements in that array must be a multiple of 3 then
> (unless there are some extraordinary circumstances) this implies that
> the data is not a plain list of single elements but it is a list of
> triplets. Had the OP used a proper data structure to represent this
> fact, e.g. an array of triplets(*), then the integrity of his data would
> be ensured by the data structure and we would not have this discussion
> in the first place.
>
> *: each triplet could be a hash or an array with 3 elements, depending
> on the kind of data in each triplet.
>
> jue

This is a source file from a database of student courses. The source
file contains records like this:
ID,LAST,FIRST,MIDDLE,MAJOR, ... [COURSES]
where [COURSES] depends on the student enrollment in the term, which
can be from zero up to possible 7 or 8, and would be as follows:
ENG-101,BIO-202,ART-303,ABCD,BCDE,CDEF,N,N,X
These values are triplets, with all the courses first, then all the
sections, then all the statuses.

When I parse the line, I collect the individual data items into loop
variables like this:
my ($id, $last, $first, $middle, $major ... , @courses) =3D
parse_line();
The @courses array then contains the enrollment info. I divide it by
3, which gives me the number of courses. I then munge the @courses
data (actually by turning it into a series of strings like this:
"ENG-101-ABC-N")
The other data goes into a hash keyed on the ID, so I can print the
reports like this:

foreach my $k (keys %students)
{
  print OUT qq($k,$students{$k}{last}, ... \n);
}

The vast majority of the time the @courses array is perfect, but
rarely it is malformed in some way, thus requiring me to check the
format of the array.

I have been doing it by moding by 3 and checking to see if the result
is not zero, but it strikes me that, if I could check to see if the
result of the division by 3 is an integer, I wouldn't have to resort
to the extra step.

CC.


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

Date: Wed, 10 Nov 2010 08:24:01 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: testing whether a number is an integer
Message-Id: <lnwrol6tb2.fsf@nuthaus.mib.org>

ccc31807 <cartercc@gmail.com> writes:
[snip]
> The vast majority of the time the @courses array is perfect, but
> rarely it is malformed in some way, thus requiring me to check the
> format of the array.
>
> I have been doing it by moding by 3 and checking to see if the result
> is not zero, but it strikes me that, if I could check to see if the
> result of the division by 3 is an integer, I wouldn't have to resort
> to the extra step.

There's an extra step either way: either you need to check whether
the number of fields is a multiple of 3, or you need to check
whether the result of dividing that number by 3 is an integer.

There is, of course, More Than One Way To Do It.  I suggest that
checking whether the number of fields is a multiple of 3 expresses
the intent more clearly.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Wed, 10 Nov 2010 09:46:17 -0800
From: sln@netherlands.com
Subject: Re: testing whether a number is an integer
Message-Id: <l3mld61vtpjjcld1n7ffoesg2veoffo0qn@4ax.com>

On Wed, 10 Nov 2010 06:25:45 -0800 (PST), ccc31807 <cartercc@gmail.com> wrote:

>This is a source file from a database of student courses. The source
>file contains records like this:
>ID,LAST,FIRST,MIDDLE,MAJOR, ... [COURSES]
>where [COURSES] depends on the student enrollment in the term, which
>can be from zero up to possible 7 or 8, and would be as follows:
>ENG-101,BIO-202,ART-303,ABCD,BCDE,CDEF,N,N,X
>These values are triplets, with all the courses first, then all the
>sections, then all the statuses.
>
>When I parse the line, I collect the individual data items into loop
>variables like this:
>my ($id, $last, $first, $middle, $major ... , @courses) =
>parse_line();
>The @courses array then contains the enrollment info. I divide it by
>3, which gives me the number of courses. I then munge the @courses
>data (actually by turning it into a series of strings like this:
>"ENG-101-ABC-N")
>The other data goes into a hash keyed on the ID, so I can print the
>reports like this:
>
>foreach my $k (keys %students)
>{
>  print OUT qq($k,$students{$k}{last}, ... \n);
>}
>
>The vast majority of the time the @courses array is perfect, but
>rarely it is malformed in some way, thus requiring me to check the
>format of the array.
>
>I have been doing it by moding by 3 and checking to see if the result
>is not zero, but it strikes me that, if I could check to see if the
>result of the division by 3 is an integer, I wouldn't have to resort
>to the extra step.
>

Whatever you are doing to get the courses array populated should
be valid before population.

A clear sign of bad validation or parsing technique is that you
actually have to do a modulo on the finished array.
The finished array should be pristeen. 
If you have a remainder, the entire array is flawed.
The place to find flaws is before the array is populated, not after.
Craft a better parsing strategy.

-sln


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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 3204
***************************************


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