Home > Archive > PERL Programming > January 2006 > Re: Exported constants in module (HELP WITH)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Exported constants in module (HELP WITH)
|
|
| Paul Lalli 2006-01-30, 6:56 pm |
| Gregory Layman wrote:
> In article <1138630603.478498.318420@o13g2000cwo.googlegroups.com>,
> mritty@gmail.com says...
> Can you explain or direct me towards a link or book that would explain
> constants and how they are used?
perldoc constant
Basically, Perl constants are implemented as subroutines that do
nothing but return a value. That way, they can be "read", but not
assigned to. When I type "READONLY" up there, I'm actually calling a
subroutine READONLY(), and using the return value of that subroutine
(which in this case is the number 1. HIDDEN() returns 2, COMPRESSED()
returns 2048, etc).
> What does the "&" symbol do?
perldoc perlop
& is the bit-wise 'AND' operator. It takes both of its arguments and
does a bit-wise AND'ing of those arguments. For example, let's say
$attr came out to be 2050. In binary, 2050 is represented as
0000 1000 0000 0010
2048 in binary is:
0000 1000 0000 0000
We "and" together each of those bits, meaning that the result of `2050
& 2048` is the number comprised of 1s where both arguments have a 1,
and 0s where either argument has a 0:
0000 1000 0000 0000
That is the number 2048. 2048 is a true value, and so `2050 & 2048` is
true, meaning that our example is a COMPRESSED() file.
Alternatively, the READONLY() check determines if `2050 & 1` is true.
Now we are AND'ing:
0000 1000 0000 0010
0000 0000 0000 0001
Which results in:
0000 0000 0000 0000
Which is the number 0, and is false. So READONLY is not set on this
file.
> I have tried to look this stuff up, but it seems I am lacking some basics on
> constants and modules (so I am not getting very far).
Out of curiousity, where are you looking? Allow me to recommend you
use the built-in perldoc program at the command line, or visit
http://perldoc.perl.org
Paul Lalli
P.S. If you don't like Perl's default implementation of constants,
you're not alone. There is a nicer alternative, known as Readonly,
available on CPAN:
http://search.cpan.org/~roode/Readonly-1.03/Readonly.pm
| |
| Gregory Layman 2006-01-30, 6:56 pm |
| In article <1138645647.206904.86250@g49g2000cwa.googlegroups.com>,
mritty@gmail.com says...
> Gregory Layman wrote:
>
> perldoc constant
>
> Basically, Perl constants are implemented as subroutines that do
> nothing but return a value. That way, they can be "read", but not
> assigned to. When I type "READONLY" up there, I'm actually calling a
> subroutine READONLY(), and using the return value of that subroutine
> (which in this case is the number 1. HIDDEN() returns 2, COMPRESSED()
> returns 2048, etc).
>
>
> perldoc perlop
>
> & is the bit-wise 'AND' operator. It takes both of its arguments and
> does a bit-wise AND'ing of those arguments. For example, let's say
> $attr came out to be 2050. In binary, 2050 is represented as
> 0000 1000 0000 0010
> 2048 in binary is:
> 0000 1000 0000 0000
> We "and" together each of those bits, meaning that the result of `2050
> & 2048` is the number comprised of 1s where both arguments have a 1,
> and 0s where either argument has a 0:
> 0000 1000 0000 0000
> That is the number 2048. 2048 is a true value, and so `2050 & 2048` is
> true, meaning that our example is a COMPRESSED() file.
>
> Alternatively, the READONLY() check determines if `2050 & 1` is true.
> Now we are AND'ing:
> 0000 1000 0000 0010
> 0000 0000 0000 0001
> Which results in:
> 0000 0000 0000 0000
> Which is the number 0, and is false. So READONLY is not set on this
> file.
Paul,
Thanks for this info. It is making sense now. I am basically stuck
to my two O'Reilly books on Perl and scouring the Internet. I have
heard of perldoc before, but never looked into using it as reference.
That will now change.
Again thanks for all your help.
Greg
|
|
|
|
|