[33012] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4288 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 25 05:17:19 2014

Date: Thu, 25 Sep 2014 02:17:06 -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           Thu, 25 Sep 2014     Volume: 11 Number: 4288

Today's topics:
    Re: Modules, how to store private data <whynot@pozharski.name>
    Re: Modules, how to store private data <jimsgibson@gmail.com>
    Re: perl and math need help with printf or other : fina <lionslair@consolidated.net>
    Re: perl and math need help with printf or other <lionslair@consolidated.net>
    Re: perl and math need help with printf or other <rweikusat@mobileactivedefense.com>
    Re: perl and math need help with printf or other <hjp-usenet3@hjp.at>
    Re: perl and math need help with printf or other <rweikusat@mobileactivedefense.com>
    Re: perl and math need help with printf or other <gamo@telecable.es>
    Re: perl and math need help with printf or other <jblack@nospam.com>
    Re: perl and math need help with printf or other <hjp-usenet3@hjp.at>
        Tie::Alias -- Why does it bless() the original variable <jl_post@hotmail.com>
    Re: Tie::Alias -- Why does it bless() the original vari <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 23 Sep 2014 20:30:58 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Modules, how to store private data
Message-Id: <slrnm23bii.hj8.whynot@orphan.zombinet>

with <8738bkyt5z.fsf@vps1.hacking.dk> Peter Makholm wrote:

*SKIP*
> In this case I think weaken works consistently between the object
> models in question.

Indeed.  Now I feel much better.

> But I wouldn't be surprised if a wrongly used weaken could act
> differently with different ways to represent objects.

Well, "wrongly used weaken" is "wrongly used" in first place.
(Remember, now I feel much better.)

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Wed, 24 Sep 2014 10:42:38 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Modules, how to store private data
Message-Id: <240920141042385722%jimsgibson@gmail.com>

In article <60lteb-r4n.ln1@zem.masonsmusic.co.uk>, Justin C
<justin.1401@purestblue.com> wrote:

 
> My main concern is someone casually looking at the data, making 
> assumptions about certain parts of it, and thinking "It'd be easier
> if I just..." [time passes] ... and then I get an email.

Publish your API. When someone emails you saying your module doesn't
work, ask them for a short, sample program demonstrating the problem.
Inspect their code. If they are not using the published API, tell them
it is not your problem (but offer to modify the API to do whatever it
is they want to do).

-- 
Jim Gibson


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

Date: Tue, 23 Sep 2014 21:51:20 -0500
From: Martin Eastburn <lionslair@consolidated.net>
Subject: Re: perl and math need help with printf or other : final solution
Message-Id: <_kqUv.347254$oh4.339649@fx11.iad>

On 9/23/2014 2:48 PM, Peter J. Holzer wrote:
> On 2014-09-23 11:46, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
>> Why bother with all these calculating and hacking a string
>> apart piece-by-piece when it is sufficient to tell perl to do that?
>
> Well, you posted a very similar solution, so why don't you answer this
> question yourself?
>
> In my case the answer should be obvious. I wanted to show Martin how to
> improve his approach in little steps. Seemed more useful than a
> ready-made fancy solution to me (especially since you already posted two
> of them). Teaching a man to fish and all that ...
>
>          hp
Works nicely.
I'll post what Peter helped me get to.  The last time I ran Perl was in 
99.  Must have been 87 when I started.

I was a IC, PCB and System designer and used Perl to do some work. 
Wrote in Verilog a good deal. I'm 67 next month.  Thought I might keep 
my mind working as I do other designs for friends.

I used to use D and E size printers and my personal E size died upon 
delivery here at our retirement home site.  My TTY aka teletype (ASR 33) 
is in the shop on an OLD computer.  We had to issue two commands,
CR and LF.  Both after each other using the same time span to execute.
It was faster than my 5 level ASR 32 but it wasn't in use long at my 
house.  I have two nice A & B size printers for fun and engineering work.

#  A program that will take a long string of several thousand or more
#  alpha-numeric characters and make it handy to print and file in a
#  folder. The number '10' in the printing line allows hole punch.

  #!perl
     use warnings;
     use strict;

     my $line = <STDIN>;
     chomp($line);
     my $line_length = length($line);
     my $length = 70;

     for (my $offset = 0; $offset < $line_length; $offset += $length) {
         print " " x 10, substr($line, $offset, $length), "\n";
     };
     print "\n";
# the end;

Martin




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

Date: Mon, 22 Sep 2014 22:54:06 -0500
From: Martin Eastburn <lionslair@consolidated.net>
Subject: Re: perl and math need help with printf or other
Message-Id: <P96Uv.347113$8G3.153643@fx12.iad>

On 9/22/2014 2:48 PM, Peter J. Holzer wrote:
<snip>

Wow - what a training lesson!  Wonderful.

I'll check it out Tuesday.  Running late again today.

I was working on it today myself.  Started to make it
a bit fancy.  Might be easier with the cleaned up and
logical version from you.

Thank you again, report to be posted.

Martin



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

Date: Tue, 23 Sep 2014 12:46:38 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: perl and math need help with printf or other
Message-Id: <87iokeblup.fsf@sable.mobileactivedefense.com>

"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
> On 2014-09-22 04:06, Martin Eastburn <lionslair@consolidated.net> wrote:

[...]

>     #!perl
>     use warnings;
>     use strict;
>
>     my $line = <STDIN>;
>
>     if ($line eq "\n") {
>         print "That was just a blank line!\n";# and bail out
>         exit 1;
>     }
>
>     chomp($line);
>
>     my $line_length = length($line);
>     print "          The input line has : $line_length characters\n\n";
>
>     # $line contains 10000+2 characters.  starting with 3.14159.....;
>     # The three and decimal point comprise the added two.  10k decimal values.;
>
>     my $length = 70;
>
>     for (my $offset = 0; $offset < $line_length; $offset += $length) {
>
>         my $part = substr($line,$offset,$length);
>
>         print "          $part";
>         print "\n";
>     };
>     print "\n";
>
> I'd also reduce the for loop a bit:
>
>     for (my $offset = 0; $offset < $line_length; $offset += $length) {
>         print " " x 10, substr($line, $offset, $length), "\n";
>     };

Why bother with all these calculating and hacking a string
apart piece-by-piece when it is sufficient to tell perl to do that?

----------
my $input;

$input = <STDIN>;

chomp($input);
unless ($input) {
    print STDERR ("War wohl nix ...\n");
    exit(1);
}

for ($input =~ /.{1,70}/g) {
        print(' ' x 10, $_, "\n");
}
----------

Q: Why is AI ultimatively futile?
A: An AI would need more elaborate and more sensible instructions
   than

	for (i = 0; i < 55; i++) {
        	if (i == 0) {
                	puts("0");
		} else if (i == 1) {
                	puts("1");
                } else if (i == 3) {
                	puts("3");
                } else {
                	puts("none of the above");
		}
	}

   in order to accomplish more than an ordinary computer.

Have they possibly been laughing about us secretly for years?


                      


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

Date: Tue, 23 Sep 2014 21:48:19 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: perl and math need help with printf or other
Message-Id: <slrnm23jk3.v5v.hjp-usenet3@hrunkner.hjp.at>

On 2014-09-23 11:46, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
> Why bother with all these calculating and hacking a string
> apart piece-by-piece when it is sufficient to tell perl to do that?

Well, you posted a very similar solution, so why don't you answer this
question yourself?

In my case the answer should be obvious. I wanted to show Martin how to
improve his approach in little steps. Seemed more useful than a
ready-made fancy solution to me (especially since you already posted two
of them). Teaching a man to fish and all that ...

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Wed, 24 Sep 2014 14:00:46 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: perl and math need help with printf or other
Message-Id: <874mvx5g1t.fsf@sable.mobileactivedefense.com>

"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
> On 2014-09-23 11:46, Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
>> Why bother with all these calculating and hacking a string
>> apart piece-by-piece when it is sufficient to tell perl to do that?
>
> Well, you posted a very similar solution, so why don't you answer this
> question yourself?

I did this because I wanted to show a sensible implementation of the
originally proposed algortithm, ie one without

my $input_length = length($input);

my $offset = 0; # I want my uninitialized locals back!
loop:
if ($offset < $input_length) {
                              
	.
        .
        $offset += $per_line;
        goto loop;
}

all concatenated on a single line in order to maximize the density of
'funny characters' there. IMHO,

for (<init-expr>; <test expr>; <step expr>) {
	<block>
}

with the (pseudo-Perl) meaning of

<init-expr>;
{
	last unless <test-expr>;
	<block>;
        <step-expr>;

        redo;
}

is a misguided 'low-level' construct which is rarely capable of
expressing real-world loops (without ending up as single line with a
hundred or more characters on it) and ought to be avoided in favor of

while/ until (<condition>) {
}

or

do {
} while/ until (<condition>)

whichever is more appropriate. And these two should be avoided in favour
of iterating over a list aka 'a foreach loop' unless there are good
reasons for not using one, eg, the list becomes so large that this is a
tangible performance impact, because the latter enables writing 'natural
loops' (process all available items) without having to deal with 'the
mechanics of moving the boxes', ie, explict initializations of loop
variables, explicitly written conditions and explicit step expression
code.

Also IMHO, the main problem of the orignal code was the desire to be
involved with 'moving the boxes' at a much more detailed level than had
been necessary to solve the problem. One could call this "control-freak
programming" or "writing FORTRAN in any language". Assuming that the
list-based solution is considered infeasible, another alternative would
be:

----------
use constant PER_LINE => 70;

my ($input, $line, $ofs);

$input = <STDIN>;
chomp($input);

while ($line = substr($input, $ofs, PER_LINE)) {
    print(' ' x 10, $line, "\n");
    $ofs += PER_LINE;
}
----------

It is not really necessary to determine the input line length explicitly
and compare anything with that because substr already does this: It will
return an empty substring if there is nothing at offset $ofs. The
explicit length test sort-of 'optimizes' for the edge-case that the
input line was empty.

(I'm also not so much in love with beancounting that I consider explicit
initializations a feature --- I'm glad that Perl doesn't need them in
many cases because this means less boilerplate code has to be written).


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

Date: Wed, 24 Sep 2014 15:06:37 +0200
From: gamo <gamo@telecable.es>
Subject: Re: perl and math need help with printf or other
Message-Id: <lvufks$d5v$1@speranza.aioe.org>

El 21/09/14 a las 05:51, Martin Eastburn escribió:
> After 10 years away from Perl, I'm trying to learn the language again. I
> started with the Pink version manual - and downloaded from JPL.
>
> Anyway - today I generated pi to 10,000 places and thought that was
> good.  I then did a factorial to 10,000 numbers - that is 10000!  for most.
>

If you want to print and store the results as a form of sign of
intelligent life, I recommend you that ask for 10002 digits of
pi, and after that, chop the last 2 digits just to be sure there
are not rounding problems due to the algorithm. It could happen.

-- 
http://www.telecable.es/personales/gamo/


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

Date: Wed, 24 Sep 2014 10:17:36 -0500
From: John Black <jblack@nospam.com>
Subject: Re: perl and math need help with printf or other
Message-Id: <MPG.2e8c9c8889a8f6379897f6@news.eternal-september.org>

In article <874mvx5g1t.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com 
says...
> while/ until (<condition>) {
> }
> 
> or
> 
> do {
> } while/ until (<condition>)
> 
> whichever is more appropriate. And these two should be avoided in favour
> of iterating over a list aka 'a foreach loop' unless there are good
> reasons for not using one, eg, the list becomes so large that this is a
> tangible performance impact, because the latter enables writing 'natural
> loops' (process all available items) without having to deal with 'the
> mechanics of moving the boxes', ie, explict initializations of loop
> variables, explicitly written conditions and explicit step expression
> code.

Rainer, I cannot follow what you are saying here.  Are you saying that foreach loops are 
better performing than while loops for iterating over a list?

John Black


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

Date: Wed, 24 Sep 2014 19:42:34 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: perl and math need help with printf or other
Message-Id: <slrnm260kc.sho.hjp-usenet3@hrunkner.hjp.at>

On 2014-09-24 15:17, John Black <jblack@nospam.com> wrote:
> In article <874mvx5g1t.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com 
[while loops]
>> should be avoided in favour of iterating over a list aka 'a foreach
>> loop' unless there are good reasons for not using one, eg, the list
>> becomes so large that this is a tangible performance impact, because
>> the latter enables writing 'natural loops' (process all available
>> items) without having to deal with 'the mechanics of moving the
>> boxes', ie, explict initializations of loop variables, explicitly
>> written conditions and explicit step expression code.
>
> Rainer, I cannot follow what you are saying here.  Are you saying that
> foreach loops are better performing than while loops for iterating
> over a list?

No, on the contrary. He writes that in some cases you may need to use a
while loop instead of a foreach loop for performance reasons (I think
this is only a concern if the list has to generated at the time of the
loop), but otherwise a foreach loop loop is preferable for stylistic
reasons.

I agree with that.

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Mon, 22 Sep 2014 18:14:10 -0700 (PDT)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Tie::Alias -- Why does it bless() the original variable?
Message-Id: <67d99f54-358d-4ca0-89ba-6e17b622ef85@googlegroups.com>

Dear Perl community,

   Recently I've been playing around with the Pure-Perl Tie::Alias module.

   What surprises me is that making an alias of a variable bless()es the or=
iginal variable (if that variable is an array or hash, that is).

   Let me explain with a sample program:


#!/usr/bin/perl
# File:  tie-alias_test.pl

use strict;
use warnings;

use Tie::Alias;

sub doubleScalarValue($)
{
   my $scalar;
   Tie::Alias::alias $scalar =3D> $_[0];
   $scalar *=3D 2;
}

sub doubleArrayValues(\@)
{
   my @array;
   Tie::Alias::alias \@array =3D> $_[0];
   $_ *=3D 2  foreach @array;
}

sub doubleHashValues(\%)
{
   my %hash;
   Tie::Alias::alias \%hash =3D> $_[0];
   $_ *=3D 2  foreach values %hash;
}

### Main code begins here: ###

my $value =3D 7;
my @valueArray =3D (1, 2, 3);
my %valueHash =3D (one =3D> 1, two =3D> 2, three =3D> 3);

use Data::Dumper;
print "\nBEFORE:\n",
      Dumper($value, \@valueArray, \%valueHash);

doubleScalarValue($value);
doubleArrayValues(@valueArray);
doubleHashValues(%valueHash);

print "\nAFTER:\n",
      Dumper($value, \@valueArray, \%valueHash);

__END__

This program produces the output:


BEFORE:
$VAR1 =3D 7;
$VAR2 =3D [
          1,
          2,
          3
        ];
$VAR3 =3D {
          'three' =3D> 3,
          'one' =3D> 1,
          'two' =3D> 2
        };

AFTER:
$VAR1 =3D '14';
$VAR2 =3D bless( [
                 '2',
                 '4',
                 '6'
               ], 'Tie::Alias::Array' );
$VAR3 =3D bless( {
                 'three' =3D> '6',
                 'one' =3D> '2',
                 'two' =3D> '4'
               }, 'Tie::Alias::Hash' );

   Note that before the function calls, Data::Dumper shows \@valueArray and=
 \%valueHash as regular references to an array and a hash (as you'd expect)=
 .  But after the function calls (where the functions use Tie::Alias to crea=
te an alias to operate on), \@valueArray is now an array reference bless()e=
d into a 'Tie::Alias::Array', and \%valueHash is now a hash references bles=
s()ed into a 'Tie::Alias::Hash'.

   Why is that?  Am I calling Tie::Alias::alias() incorrectly?  (I notice t=
hat the same problem does not happen to the $value scalar variable.)

   I'm a bit concerned that arrays and hashes are bless()ed as a side-effec=
t.  I might have code later on that expects a pure array or hash reference,=
 and I don't want the fact that they're now a 'Tie::Alias::Array' or 'Tie::=
Alias::Hash' reference to make the code do something I didn't account for.

   Thanks for any help!

   -- Jean-Luc


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

Date: Tue, 23 Sep 2014 14:55:46 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Tie::Alias -- Why does it bless() the original variable?
Message-Id: <87egv2saot.fsf@sable.mobileactivedefense.com>

"jl_post@hotmail.com" <jl_post@hotmail.com> writes:
>
>    Recently I've been playing around with the Pure-Perl Tie::Alias module.
>
>    What surprises me is that making an alias of a variable bless()es the original variable (if that variable is an array or hash, that is).
>
>    Let me explain with a sample program:
>
>
> #!/usr/bin/perl
> # File:  tie-alias_test.pl
>
> use strict;
> use warnings;
>
> use Tie::Alias;
>
> sub doubleScalarValue($)
> {
>    my $scalar;
>    Tie::Alias::alias $scalar => $_[0];
>    $scalar *= 2;
> }
>
> sub doubleArrayValues(\@)
> {
>    my @array;
>    Tie::Alias::alias \@array => $_[0];
>    $_ *= 2  foreach @array;
> }
>
> sub doubleHashValues(\%)
> {
>    my %hash;
>    Tie::Alias::alias \%hash => $_[0];
>    $_ *= 2  foreach values %hash;
> }

The reason for this (as can be determined easily by looking at the code)
is that Tie::Alias::alias passes the object supposed to be aliased to
each of the TIE* constructors which use it as 'tie object' in case it
can be blessed, ie, is a reference. Since your scalar in
doubleScalarValue isn't a reference, the module creates one instead. If
it wasn't using the passed objects, it would need to create a suitable
something in order to store a reference to them.

This is a somewhat idiosyncratic design descision which should be
documented.


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

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


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