[28858] in Perl-Users-Digest
Perl-Users Digest, Issue: 102 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 9 21:55:58 2007
Date: Fri, 9 Feb 2007 18:55:24 -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 Fri, 9 Feb 2007 Volume: 11 Number: 102
Today's topics:
beginner's question <dave.slayton@gmail.com>
Re: beginner's question <spamtrap@dot-app.org>
Re: beginner's question <dave.slayton@gmail.com>
Re: beginner's question <DJStunks@gmail.com>
Re: beginner's question <bik.mido@tiscalinet.it>
Re: beginner's question <tadmc@augustmail.com>
Re: beginner's question <yankeeinexile@gmail.com>
Re: beginner's question <mark.clementsREMOVETHIS@wanadoo.fr>
Re: beginner's question <mark.clementsREMOVETHIS@wanadoo.fr>
Re: beginner's question <bik.mido@tiscalinet.it>
Re: beginner's question <bik.mido@tiscalinet.it>
Re: beginner's question <bik.mido@tiscalinet.it>
Re: beginner's question <hjp-usenet2@hjp.at>
Re: beginner's question <DJStunks@gmail.com>
Re: beginner's question <bik.mido@tiscalinet.it>
Re: beginner's question <spamtrap@dot-app.org>
Re: beginner's question <beliavsky@aol.com>
Re: beginner's question <joe@inwap.com>
Re: beginner's question <hjp-usenet2@hjp.at>
Re: beginner's question anno4000@radom.zrz.tu-berlin.de
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 2 Feb 2007 19:55:04 -0700
From: "Dave Slayton" <dave.slayton@gmail.com>
Subject: beginner's question
Message-Id: <2JidnUP6zO3lZF7YnZ2dnUVZ_sOknZ2d@comcast.com>
Ok, let's see if I may ask a fairly basic question (as opposed to an
advanced one) and get a useful answer. I hope so.
I see that this:
*PI = \3.14159265358979;
will create a constant that I may not change. Sure, why change $PI anyway,
even if you could, otherwise why set up the constant in the first place?
But, if I understand, it only modifies the scalar entry in the *PI typeglob,
making it refer now to some address in memory that holds this value for pi.
So I don't see why it should be IMPOSSIBLE to change its value, unless
there's something I don't yet understand about typeglobs. It would make
sense if it's merely POLICY, and if, for instance, it's implemented by Perl
underneath as a pointer to constant, once Perl notices it's a reference to a
scalar literal. Would someone please offer me a little guidance on this
issue? Thanks!
------------------------------
Date: Fri, 02 Feb 2007 23:15:18 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: beginner's question
Message-Id: <m264ajc1wp.fsf@Sherm-Pendleys-Computer.local>
"Dave Slayton" <dave.slayton@gmail.com> writes:
> Ok, let's see if I may ask a fairly basic question (as opposed to an
> advanced one) and get a useful answer.
Why do you imagine you wouldn't? This group can be harsh to those who ask
FAQs, or ignore the guidelines, but that's in response to rude behavior,
not just random flames.
> I see that this:
>
> *PI = \3.14159265358979;
>
> will create a constant that I may not change.
I'm not sure why you'd do it that way. The "normal" way of creating constants
in Perl is the constant module. For example:
use constant PI => 3.14159265358979;
Constants declared this way are evaluated at compile-time, and their value
treated as if it were a literal value in your code. You can see that by using
the B::Deparse module to turn the compiled bytecodes back into Perl source:
sherm:~ sherm$ perl -MO=Deparse -e 'use constant PI=>3.14; print PI'
use constant ('PI', 3.14);
print 3.14;
-e syntax OK
Note the deparsed print statement - it's not printing the value of a variable,
it's printing a literal value.
Also, note that PI is PI - not $PI. Constants aren't variables, and you don't
need a type identifier when you use them.
> But, if I understand, it only modifies the scalar entry in the *PI typeglob,
> making it refer now to some address in memory that holds this value for pi.
> So I don't see why it should be IMPOSSIBLE to change its value
It's impossible to change because the thing the typeglob is now referring to
is a literal, which can't be changed. On the other hand, you *can* change the
typeglob itself, so that it refers to something else, or if you declare it as
a reference to a variable, you can change that variable's value:
#!/usr/bin/perl
use strict;
use warnings;
*PI = \3.14;
print $main::PI, "\n";
*PI = \15;
print $main::PI, "\n";
my $newPI = 55;
*PI = \$newPI;
print $main::PI, "\n";
$newPI = 3.14;
print $main::PI, "\n";
I would hesitate to call something like that a "constant", given that there
are so many ways to change its value, and that there are actual constants
available in Perl.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Sat, 3 Feb 2007 09:31:04 -0700
From: "Dave Slayton" <dave.slayton@gmail.com>
Subject: Re: beginner's question
Message-Id: <damdnXlhRNxMJVnYnZ2dnUVZ_tGsnZ2d@comcast.com>
"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:m264ajc1wp.fsf@Sherm-Pendleys-Computer.local...
> "Dave Slayton" <dave.slayton@gmail.com> writes:
>
>> Ok, let's see if I may ask a fairly basic question (as opposed to an
>> advanced one) and get a useful answer.
>
> Why do you imagine you wouldn't? This group can be harsh to those who ask
> FAQs, or ignore the guidelines, but that's in response to rude behavior,
> not just random flames.
Ok! (But I'm unsure what the guidelines might be, beyond trying as hard as
one can to search the FAQs before
coming here to ask questions.)
>
>> I see that this:
>>
>> *PI = \3.14159265358979;
>>
>> will create a constant that I may not change.
>
> I'm not sure why you'd do it that way. The "normal" way of creating
> constants
> in Perl is the constant module. For example:
>
> use constant PI => 3.14159265358979;
because it's on page 295 of Programming Perl (as is the solution you
offered), which I'm reading and
trying to absorb and understand...my purpose is not so much to define a
constant as to understand
how it all works
>
> Constants declared this way are evaluated at compile-time, and their value
> treated as if it were a literal value in your code. You can see that by
> using
> the B::Deparse module to turn the compiled bytecodes back into Perl
> source:
>
> sherm:~ sherm$ perl -MO=Deparse -e 'use constant PI=>3.14; print PI'
> use constant ('PI', 3.14);
> print 3.14;
> -e syntax OK
>
> Note the deparsed print statement - it's not printing the value of a
> variable,
> it's printing a literal value.
>
> Also, note that PI is PI - not $PI. Constants aren't variables, and you
> don't
> need a type identifier when you use them.
>
>> But, if I understand, it only modifies the scalar entry in the *PI
>> typeglob,
>> making it refer now to some address in memory that holds this value for
>> pi.
>> So I don't see why it should be IMPOSSIBLE to change its value
>
> It's impossible to change because the thing the typeglob is now referring
> to
> is a literal, which can't be changed. On the other hand, you *can* change
> the
> typeglob itself, so that it refers to something else, or if you declare it
> as
> a reference to a variable, you can change that variable's value:
Ok, well it's sounding more and more like if I knew where to look in the
Perl source code,
I'd find the literal placed in a memory location defined as something like
double *const hisliteralvalue;
thus forbidding it to be changed...otherwise, why couldn't it be changed?
the value has
to be stored in memory somewhere...maybe it boils down to the underlying
implementation
of lvalues and rvalues, which I'm also unsure of...
I see it doesn't have anything to do with the fact it's part of a typeglob,
because I see
that
$huh = \3.14159;
$$huh = \2.71828;
is likewise disallowed.
Anyhow, thanks for answering.
------------------------------
Date: 3 Feb 2007 11:24:50 -0800
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: beginner's question
Message-Id: <1170530690.574500.277580@j27g2000cwj.googlegroups.com>
On Feb 2, 9:15 pm, Sherm Pendley <spamt...@dot-app.org> wrote:
> "Dave Slayton" <dave.slay...@gmail.com> writes:
> Subject: beginner's question
Please put the subject of your post in the Subject of the post.
> > I see that this:
>
> > *PI = \3.14159265358979;
>
> > will create a constant that I may not change.
>
> I'm not sure why you'd do it that way. The "normal" way of creating constants
> in Perl is the constant module. For example:
>
> use constant PI => 3.14159265358979;
And the new "normal" way is to use Readonly, which allows constants to
be interpolated, allows the creation of complex constants (like
hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
constants (among other advantages).
use Readonly;
Readonly my $PI => 3.14159265358979;
Although I suppose if I just wanted pi, I might use Math::Trig which
defines pi.
:-)
-jp
------------------------------
Date: Sat, 03 Feb 2007 20:43:40 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: beginner's question
Message-Id: <jto9s2pslks5rsmtvvdvetvn1a52u7rjja@4ax.com>
On Sat, 3 Feb 2007 09:31:04 -0700, "Dave Slayton"
<dave.slayton@gmail.com> wrote:
>Ok, well it's sounding more and more like if I knew where to look in the
>Perl source code,
>I'd find the literal placed in a memory location defined as something like
> double *const hisliteralvalue;
Really, if you want to do so, then I suppose it may be rewarding. But
seriously: you don not *need* to be familiar with the underlying
internals. Sherm explained in detail what is going on. From that POV
you can safely assume a black box approach to Perl.
>I see it doesn't have anything to do with the fact it's part of a typeglob,
>because I see
>that
> $huh = \3.14159;
$huh now contains a reference to something. This "something" happens
to be a read-only value.
> $$huh = \2.71828;
Here you are dereferencing $huh on the rhs and trying to assign to
this rhs something, which is also a reference (but that doesn't really
matter).
>is likewise disallowed.
Which is reasonable, because the above boils down to
3.14159=2.71828; # Would you expect this to work?!?
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sat, 3 Feb 2007 14:26:40 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: beginner's question
Message-Id: <slrnes9s00.udv.tadmc@tadmc30.august.net>
Dave Slayton <dave.slayton@gmail.com> wrote:
> "Sherm Pendley" <spamtrap@dot-app.org> wrote in message
> news:m264ajc1wp.fsf@Sherm-Pendleys-Computer.local...
>> "Dave Slayton" <dave.slayton@gmail.com> writes:
>>
>>> Ok, let's see if I may ask a fairly basic question (as opposed to an
>>> advanced one) and get a useful answer.
>>
>> Why do you imagine you wouldn't? This group can be harsh to those who ask
>> FAQs, or ignore the guidelines, but that's in response to rude behavior,
>> not just random flames.
>
> Ok! (But I'm unsure what the guidelines might be,
They are posted to the newsgroup twice each week, and are
also available at:
http://www.augustmail.com/~tadmc/clpmisc.shtml
> beyond trying as hard as
> one can to search the FAQs before
> coming here to ask questions.)
That's one of them alright.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 03 Feb 2007 19:16:30 -0600
From: Lawrence Statton XE2/N1GAK <yankeeinexile@gmail.com>
Subject: Re: beginner's question
Message-Id: <87sldmoh75.fsf@gmail.com>
Michele Dondi <bik.mido@tiscalinet.it> writes:
>
> 3.14159=2.71828; # Would you expect this to work?!?
>
You were never a Fortran programmer, were you :)
--
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.
------------------------------
Date: Sun, 04 Feb 2007 09:19:50 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: beginner's question
Message-Id: <45c5971e$0$27372$ba4acef3@news.orange.fr>
DJ Stunks wrote:
> On Feb 2, 9:15 pm, Sherm Pendley <spamt...@dot-app.org> wrote:
>> "Dave Slayton" <dave.slay...@gmail.com> writes:
>> Subject: beginner's question
>
> Please put the subject of your post in the Subject of the post.
>
>>> I see that this:
>>> *PI = \3.14159265358979;
>>> will create a constant that I may not change.
>> I'm not sure why you'd do it that way. The "normal" way of creating constants
>> in Perl is the constant module. For example:
>>
>> use constant PI => 3.14159265358979;
>
> And the new "normal" way is to use Readonly, which allows constants to
> be interpolated, allows the creation of complex constants (like
> hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
> constants (among other advantages).
Just to nitpick: use constant also allows you to define complex constants.
F:\Documents and Settings\Mark3>cat testconstant.pl
use strict;
use warnings;
use Data::Dumper;
use constant HoH => {
abcd => {
a=> 10,
b=> 20,
},
efgh => {
c => 30,
d => 40,
},
};
print Dumper HoH;
print HoH->{abcd}->{b}."\n";
F:\Documents and Settings\Mark3>perl testconstant.pl
$VAR1 = {
'efgh' => {
'c' => 30,
'd' => 40
},
'abcd' => {
'a' => 10,
'b' => 20
}
};
20
F:\Documents and Settings\Mark3>perl -v
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
Admittedly I can't get it to work for hash slices :)
Mark
------------------------------
Date: Sun, 04 Feb 2007 09:35:16 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: beginner's question
Message-Id: <45c59ac5$0$5103$ba4acef3@news.orange.fr>
Mark Clements wrote:
> DJ Stunks wrote:
>> On Feb 2, 9:15 pm, Sherm Pendley <spamt...@dot-app.org> wrote:
>>> "Dave Slayton" <dave.slay...@gmail.com> writes:
>>> Subject: beginner's question
>>
>> Please put the subject of your post in the Subject of the post.
>>
>>>> I see that this:
>>>> *PI = \3.14159265358979;
>>>> will create a constant that I may not change.
>>> I'm not sure why you'd do it that way. The "normal" way of creating
>>> constants
>>> in Perl is the constant module. For example:
>>>
>>> use constant PI => 3.14159265358979;
>>
>> And the new "normal" way is to use Readonly, which allows constants to
>> be interpolated, allows the creation of complex constants (like
>> hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
>> constants (among other advantages).
>
> Just to nitpick: use constant also allows you to define complex constants.
>
<snip>
> Admittedly I can't get it to work for hash slices :)
... and it won't work with hash operators eg keys, values.
I knew there was a reason why I rarely use this in anger.
Mark
------------------------------
Date: Sun, 04 Feb 2007 11:47:13 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: beginner's question
Message-Id: <acebs2hetpeser89cm0phrhucq60j5vdh6@4ax.com>
On 03 Feb 2007 19:16:30 -0600, Lawrence Statton XE2/N1GAK
<yankeeinexile@gmail.com> wrote:
>> 3.14159=2.71828; # Would you expect this to work?!?
>
>You were never a Fortran programmer, were you :)
No, I weren't! But now that you wrote this... c'mon, please explain!!
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sun, 04 Feb 2007 11:49:41 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: beginner's question
Message-Id: <1febs2ld4io9gk5f8caheqlp79r2u7gilg@4ax.com>
On 3 Feb 2007 11:24:50 -0800, "DJ Stunks" <DJStunks@gmail.com> wrote:
>> use constant PI => 3.14159265358979;
>
>And the new "normal" way is to use Readonly, which allows constants to
>be interpolated, allows the creation of complex constants (like
>hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
>constants (among other advantages).
Well, this is a *slightly* flame-baiting subject: some prefer one
module, some the other. In doubt let's mention both and not start a
value attribution competition...
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sun, 04 Feb 2007 12:07:07 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: beginner's question
Message-Id: <asebs2d0rhrh3ooof3ia0e1kbocrpgs9r8@4ax.com>
On Sun, 04 Feb 2007 09:35:16 +0100, Mark Clements
<mark.clementsREMOVETHIS@wanadoo.fr> wrote:
>> Admittedly I can't get it to work for hash slices :)
But you can! Except that you just can't doit like this:
print Dumper HOH->{qw/abcd efgh/};
Because, as with *any* hashref (and thus not limited too C<use
constant>'s ones) due to the lack of an informative sigil, perl can't
tell whether you want a hash slice or a "multidimentional subscrit",
and by default assumes the latter. In fact this is what B::Deparse
tells me about the above:
print Dumper({abcd => {a => 10, b => 20},
efgh => {c => 30, d => 40}}->{join $;,
'abcd','efgh'});
(slightly reformatted.)
You have to be more explicit about the kind of dereferentiation you
need:
print Dumper @{+HOH}{qw/abcd efgh/};
Here of course you have to put something in the first pair of curlies
to make it clear that it's not a bareword but some code.
>... and it won't work with hash operators eg keys, values.
Well, keys() and values() act on hashes, not hashrefs. In Perl 5
transformations between these objects must be explicit. Thus the
following *does* work:
print Dumper [ keys %{+HOH} ];
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sun, 4 Feb 2007 14:53:36 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: beginner's question
Message-Id: <slrnesbpb0.30u.hjp-usenet2@yoyo.hjp.at>
On 2007-02-04 10:47, Michele Dondi <bik.mido@tiscalinet.it> wrote:
> On 03 Feb 2007 19:16:30 -0600, Lawrence Statton XE2/N1GAK
><yankeeinexile@gmail.com> wrote:
>
>>> 3.14159=2.71828; # Would you expect this to work?!?
>>
>>You were never a Fortran programmer, were you :)
>
> No, I weren't! But now that you wrote this... c'mon, please explain!!
In FORTRAN (like in perl) arguments are passed to subroutines by
reference. If you pass a literal to a subroutine, the compiler puts the
literal into a temporary variable and passes a reference to that
variable to the subroutine. Conceptionally the argument is read-only in
this case, and must not be modified. However, the compiler isn't
required to assure this (FORTRAN, like C, is one of those languages
where just about any programming error leads to undefined behaviour).
Also, since FORTRAN was developed at a time when memory was very tight,
most FORTRAN compilers fold all occurrences of a given constant into
one.
so if you have fortran code like this
call f(5)
do 1 i=1,5
print *,i
1 continue
subroutine f(n)
n=10
return
the compiler would replace all occurences of 5 with a single internal
variable, which would be set to 10 in the subroutine f, so the loop
would run 10 times instead of 5 times.
hp
--
_ | Peter J. Holzer | Es ist ganz einfach ihn zu verstehen, wenn
|_|_) | Sysadmin WSR | man nur alle wichtigen Worte im Satz durch
| | | hjp@hjp.at | andere ersetzt.
__/ | http://www.hjp.at/ | -- Nils Ketelsen in danr
------------------------------
Date: 4 Feb 2007 07:57:02 -0800
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: beginner's question
Message-Id: <1170604622.310494.37910@j27g2000cwj.googlegroups.com>
On Feb 4, 3:49 am, Michele Dondi <bik.m...@tiscalinet.it> wrote:
> On 3 Feb 2007 11:24:50 -0800, "DJ Stunks" <DJStu...@gmail.com> wrote:
>
> >> use constant PI => 3.14159265358979;
>
> >And the new "normal" way is to use Readonly, which allows constants to
> >be interpolated, allows the creation of complex constants (like
> >hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
> >constants (among other advantages).
>
> Well, this is a *slightly* flame-baiting subject: some prefer one
> module, some the other. In doubt let's mention both and not start a
> value attribution competition...
No flame intended, I assure you. The primary (though minor)
disadvantage of Readonly as I see it, is that you have to download
it. Maybe at some point it will make it into Perl core. Also, as the
module's author admits, it's slow, but that's why there's Readonly::XS
- the issue there being that you have to compile it.
-jp
------------------------------
Date: Sun, 04 Feb 2007 18:52:52 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: beginner's question
Message-Id: <lt6cs2t9cc9nu0tjhtb366gae69hfp7ilo@4ax.com>
On 4 Feb 2007 07:57:02 -0800, "DJ Stunks" <DJStunks@gmail.com> wrote:
>> Well, this is a *slightly* flame-baiting subject: some prefer one
>> module, some the other. In doubt let's mention both and not start a
>> value attribution competition...
>
>No flame intended, I assure you. The primary (though minor)
I wasn't suggesting that the flame was intended, nay I'm quite
convinced it was not. I was warning that IMHO it is a flame-baiting
subject. But in any case it would be a *minor* flame.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Sun, 04 Feb 2007 14:50:55 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: beginner's question
Message-Id: <m2ejp5aehs.fsf@Sherm-Pendleys-Computer.local>
Michele Dondi <bik.mido@tiscalinet.it> writes:
> On 3 Feb 2007 11:24:50 -0800, "DJ Stunks" <DJStunks@gmail.com> wrote:
>
>>> use constant PI => 3.14159265358979;
>>
>>And the new "normal" way is to use Readonly, which allows constants to
>>be interpolated, allows the creation of complex constants (like
>>hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
>>constants (among other advantages).
>
> Well, this is a *slightly* flame-baiting subject: some prefer one
> module, some the other. In doubt let's mention both and not start a
> value attribution competition...
Just for the record, the only reason I failed to mention Readonly was that
I wasn't aware of it. I've never "hit my head" on the core constants mod's
limits yet, so never found the need to research alternatives.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: 4 Feb 2007 17:38:27 -0800
From: "Beliavsky" <beliavsky@aol.com>
Subject: Re: beginner's question
Message-Id: <1170639507.559380.21470@l53g2000cwa.googlegroups.com>
On Feb 4, 8:53 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> On 2007-02-04 10:47, Michele Dondi <bik.m...@tiscalinet.it> wrote:
>
> > On 03 Feb 2007 19:16:30 -0600, Lawrence Statton XE2/N1GAK
> ><yankeeinex...@gmail.com> wrote:
>
> >>> 3.14159=2.71828; # Would you expect this to work?!?
>
> >>You were never aFortranprogrammer, were you :)
>
> > No, I weren't! But now that you wrote this... c'mon, please explain!!
>
> InFORTRAN(like in perl) arguments are passed to subroutines by
> reference. If you pass a literal to a subroutine, the compiler puts the
> literal into a temporary variable and passes a reference to that
> variable to the subroutine. Conceptionally the argument is read-only in
> this case, and must not be modified. However, the compiler isn't
> required to assure this (FORTRAN, like C, is one of those languages
> where just about any programming error leads to undefined behaviour).
>
> Also, since FORTRAN was developed at a time when memory was very tight,
> most FORTRAN compilers fold all occurrences of a given constant into
> one.
>
> so if you have fortran code like this
>
> call f(5)
> do 1 i=1,5
> print *,i
> 1 continue
>
> subroutine f(n)
> n=10
> return
>
> the compiler would replace all occurences of 5 with a single internal
> variable, which would be set to 10 in the subroutine f, so the loop
> would run 10 times instead of 5 times.
The code given is not standard Fortran (as its author knows), so the
compiler is free to do what it wants. For the code
program main
call f(5)
do i=1,5
print*,i
end do
end program main
subroutine f(n)
n=10
return
end subroutine f
On Windows, the g95, Intel, and Lahey/Fujitsu compilers give output
1
2
3
4
5
I think Mr. Holzer's comments describe what Fortran compilers used to
do -- I have heard similar stories before -- but not what they do
know, fortunately.
------------------------------
Date: Sun, 04 Feb 2007 21:47:09 -0800
From: Joe Smith <joe@inwap.com>
Subject: Re: beginner's question
Message-Id: <9sKdnSWP7Pt_WVvYnZ2dnUVZ_qKknZ2d@comcast.com>
Dave Slayton wrote:
> Ok, well it's sounding more and more like if I knew where to look in the
> Perl source code,
> I'd find the literal placed in a memory location defined as something like
> double *const hisliteralvalue;
> thus forbidding it to be changed...otherwise, why couldn't it be changed?
> the value has to be stored in memory somewhere...
One of the questions on a Purity Test for programmers is:
Q1a: Have you ever changed the value of 4?
Q1b: ... In a language other than FORTRAN?
The FORTRAN compiler I worked with passed literals by reference, exactly
the same way it passed variables by reference to functions and subroutines.
If a function/subroutine modified its arguments, it would change the
constant, and the changed constant would be passed to the next
function/subroutine to be called with that same literal value.
Perl does not have this problem. Read-only variables and constants are
marked as such. Perl execution triggers a fatal error on attempt to
modify read-only data.
-Joe
------------------------------
Date: Mon, 5 Feb 2007 10:01:13 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: beginner's question
Message-Id: <slrnesdsip.1ad.hjp-usenet2@yoyo.hjp.at>
On 2007-02-05 01:38, Beliavsky <beliavsky@aol.com> wrote:
> On Feb 4, 8:53 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
>> On 2007-02-04 10:47, Michele Dondi <bik.m...@tiscalinet.it> wrote:
>>
>> > On 03 Feb 2007 19:16:30 -0600, Lawrence Statton XE2/N1GAK
>> ><yankeeinex...@gmail.com> wrote:
>>
>> >>> 3.14159=2.71828; # Would you expect this to work?!?
>>
>> >>You were never aFortranprogrammer, were you :)
>>
>> > No, I weren't! But now that you wrote this... c'mon, please explain!!
>>
>> InFORTRAN(like in perl) arguments are passed to subroutines by
>> reference. If you pass a literal to a subroutine, the compiler puts the
>> literal into a temporary variable and passes a reference to that
>> variable to the subroutine. Conceptionally the argument is read-only in
>> this case, and must not be modified. However, the compiler isn't
>> required to assure this (FORTRAN, like C, is one of those languages
>> where just about any programming error leads to undefined behaviour).
>>
>> Also, since FORTRAN was developed at a time when memory was very tight,
>> most FORTRAN compilers fold all occurrences of a given constant into
>> one.
>>
>> so if you have fortran code like this
>>
[horrible pseudo-fortran77 deleted]
>>
>> the compiler would replace all occurences of 5 with a single internal
>> variable, which would be set to 10 in the subroutine f, so the loop
>> would run 10 times instead of 5 times.
>
> The code given is not standard Fortran (as its author knows),
Yes, sorry. My Fortran is very rusty - I never did much Fortran
programming and the little bit I did was 15+ years ago. I get to look at
some Fortran code (mostly F77, with a few bits of F90 and Fortran IV
sprinkled into it) every year or two, though.
> I think Mr. Holzer's comments describe what Fortran compilers used to
> do -- I have heard similar stories before -- but not what they do
> know, fortunately.
Yup. Compiler technology has come a long way in the last 20 years. It
also depends on the platform - I don't think folding integer constants
makes much sense (performance- or space-wise) on Intel processors (but
the Intel Fortran compiler I used in the late 1980's did it anyway).
hp
--
_ | Peter J. Holzer | Es ist ganz einfach ihn zu verstehen, wenn
|_|_) | Sysadmin WSR | man nur alle wichtigen Worte im Satz durch
| | | hjp@hjp.at | andere ersetzt.
__/ | http://www.hjp.at/ | -- Nils Ketelsen in danr
------------------------------
Date: 7 Feb 2007 14:15:24 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: beginner's question
Message-Id: <52u57sF1qbe5qU4@mid.dfncis.de>
Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr> wrote in comp.lang.perl.misc:
> DJ Stunks wrote:
> > On Feb 2, 9:15 pm, Sherm Pendley <spamt...@dot-app.org> wrote:
> >> "Dave Slayton" <dave.slay...@gmail.com> writes:
> >> Subject: beginner's question
> >
> > Please put the subject of your post in the Subject of the post.
> >
> >>> I see that this:
> >>> *PI = \3.14159265358979;
> >>> will create a constant that I may not change.
> >> I'm not sure why you'd do it that way. The "normal" way of creating constants
> >> in Perl is the constant module. For example:
> >>
> >> use constant PI => 3.14159265358979;
> >
> > And the new "normal" way is to use Readonly, which allows constants to
> > be interpolated, allows the creation of complex constants (like
> > hashes, arrays, HoAs, HoHs, etc) and allows the creation of lexical
> > constants (among other advantages).
>
> Just to nitpick: use constant also allows you to define complex constants.
Not quite. You can put a complex structure in a constant, but that
doesn't make the structure constant. With Readonly you can make
deeply immutable structures.
> F:\Documents and Settings\Mark3>cat testconstant.pl
> use strict;
> use warnings;
>
> use Data::Dumper;
>
> use constant HoH => {
> abcd => {
> a=> 10,
> b=> 20,
> },
> efgh => {
> c => 30,
> d => 40,
> },
>
> };
>
> print Dumper HoH;
>
> print HoH->{abcd}->{b}."\n";
Now do
HoH->{abcd}->{b} = 123;
and print it again.
Anno
------------------------------
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 V11 Issue 102
**************************************