Exported constants in module (HELP WITH)

Exported constants in module (HELP WITH)

am 30.01.2006 09:21:57 von Gregory Layman

I am needing some explanation as to how to use exported constants from
modules. I really don't even know what I am talking about, but maybe I
can explain. I am trying to pull the attributes from files in Perl on
Windows (Active State v5.8). I am using the Win32::File module
(http://www.xav.com/perl/site/lib/Win32/File.html).

I can get ORed value that it states, but what are the constants for at
the end of the document? I don't even know what values are set to what
attributes without trial and error (which I have already figured out).
I just would like to know what I losing out on with not knowing about
these exported constants.

Can anyone help with an somewhat high level explaination?

Thanks,
Greg Layman

Re: Exported constants in module (HELP WITH)

am 30.01.2006 15:16:43 von Paul Lalli

Gregory Layman wrote:
> I am needing some explanation as to how to use exported constants from
> modules. I really don't even know what I am talking about, but maybe I
> can explain. I am trying to pull the attributes from files in Perl on
> Windows (Active State v5.8). I am using the Win32::File module
> (http://www.xav.com/perl/site/lib/Win32/File.html).
>
> I can get ORed value that it states, but what are the constants for at
> the end of the document? I don't even know what values are set to what
> attributes without trial and error (which I have already figured out).

Well, that's generally the point of these kinds of constants. You
shouldn't need to know what their actual values are. You just use
them:
#!/usr/bin/perl
use strict;
use warnings;

use Win32::File;

my $attr;
Win32::File::GetAttributes($ARGV[0], $attr) or die "Could not get
attrs: $!";
print "File is readonly\n" if $attr & READONLY;
print "File is hidden\n" if $attr & HIDDEN;
print "File is compressed\n" if $attr & COMPRESSED;
print "File is a directory\n" if $attr & DIRECTORY;
print "File is normal\n" if $attr & NORMAL;
print "File is archiveable\n" if $attr & ARCHIVE;

> I just would like to know what I losing out on with not knowing about
> these exported constants.

I'm afraid I don't know what that sentence means.

> Can anyone help with an somewhat high level explaination?

Sorry, but you're going to need to explain what it is you want us to
explain... :-)

Paul Lalli

Re: Exported constants in module (HELP WITH)

am 30.01.2006 15:56:30 von Gregory Layman

In article <1138630603.478498.318420@o13g2000cwo.googlegroups.com>,
mritty@gmail.com says...
> Gregory Layman wrote:
> > I am needing some explanation as to how to use exported constants from
> > modules. I really don't even know what I am talking about, but maybe I
> > can explain. I am trying to pull the attributes from files in Perl on
> > Windows (Active State v5.8). I am using the Win32::File module
> > (http://www.xav.com/perl/site/lib/Win32/File.html).
> >
> > I can get ORed value that it states, but what are the constants for at
> > the end of the document? I don't even know what values are set to what
> > attributes without trial and error (which I have already figured out).
>
> Well, that's generally the point of these kinds of constants. You
> shouldn't need to know what their actual values are. You just use
> them:
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> use Win32::File;
>
> my $attr;
> Win32::File::GetAttributes($ARGV[0], $attr) or die "Could not get
> attrs: $!";
> print "File is readonly\n" if $attr & READONLY;
> print "File is hidden\n" if $attr & HIDDEN;
> print "File is compressed\n" if $attr & COMPRESSED;
> print "File is a directory\n" if $attr & DIRECTORY;
> print "File is normal\n" if $attr & NORMAL;
> print "File is archiveable\n" if $attr & ARCHIVE;
>
> > I just would like to know what I losing out on with not knowing about
> > these exported constants.
>
> I'm afraid I don't know what that sentence means.
>
> > Can anyone help with an somewhat high level explaination?
>
> Sorry, but you're going to need to explain what it is you want us to
> explain... :-)
>
> Paul Lalli
>
>
Paul,

First off, thanks for your response. I now know how to get it to
work, but I don't understand how it works.

Can you explain or direct me towards a link or book that would explain
constants and how they are used? What does the "&" symbol do? 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).

Thanks again,
Greg

Re: Exported constants in module (HELP WITH)

am 30.01.2006 19:10:02 von NTUA

"Paul Lalli" wrote in message
news:1138630603.478498.318420@o13g2000cwo.googlegroups.com.. .
> Gregory Layman wrote:
>> I am needing some explanation as to how to use exported constants from
>> modules. I really don't even know what I am talking about, but maybe I
>> can explain. I am trying to pull the attributes from files in Perl on
>> Windows (Active State v5.8). I am using the Win32::File module
>> (http://www.xav.com/perl/site/lib/Win32/File.html).
>>
>> I can get ORed value that it states, but what are the constants for at
>> the end of the document? I don't even know what values are set to what
>> attributes without trial and error (which I have already figured out).
>
> Well, that's generally the point of these kinds of constants. You
> shouldn't need to know what their actual values are. You just use
> them:
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> use Win32::File;
>
> my $attr;
> Win32::File::GetAttributes($ARGV[0], $attr) or die "Could not get
> attrs: $!";
> print "File is readonly\n" if $attr & READONLY;
> print "File is hidden\n" if $attr & HIDDEN;
> print "File is compressed\n" if $attr & COMPRESSED;
> print "File is a directory\n" if $attr & DIRECTORY;
> print "File is normal\n" if $attr & NORMAL;
> print "File is archiveable\n" if $attr & ARCHIVE;
>
>> I just would like to know what I losing out on with not knowing about
>> these exported constants.
>
> I'm afraid I don't know what that sentence means.
>
>> Can anyone help with an somewhat high level explaination?
>
> Sorry, but you're going to need to explain what it is you want us to
> explain... :-)
>
> Paul Lalli
>



You should also add the line

print "File is not for indexing\n" if $attr & 8192;

otherelese big suprises can occur ...

Re: Exported constants in module (HELP WITH)

am 30.01.2006 19:12:57 von NTUA

use Win32::File;

my $attr = Detailed_GetAttributes("$ENV{SYSTEMROOT}\\notepad.exe");

foreach my $a (keys %{$attr})
{
print "$a\n"
}



# Returns a hash reference with the following keys if the corresponded
# attribytes exists. If the attributes are missing, the keys will also
absent
# NORMAL is set when no attr is set and the file is ready for indexing.
#
# ARCHIVE 32 COMPRESSED 2048
# DIRECTORY 16 HIDDEN 2
# NORMAL 128 OFFLINE 4096
# READONLY 1 SYSTEM 4
# NOTINDEXING 8192 TEMPORARY 256
#

sub Detailed_GetAttributes ($)
{
local $_;
my $file = $_[0];
my $result = {};
our $AttribVal = {13=>'NOTINDEXING', 5=>'ARCHIVE', 11=>'COMPRESSED',
4=>'DIRECTORY', 1=>'HIDDEN', 7=>'NORMAL', 12=>'OFFLINE', 0=>'READONLY',
2=>'SYSTEM', 8=>'TEMPORARY'} unless defined $AttribVal; # as powers of 2

Win32::File::GetAttributes($file, my $attrib) || die "Could not get
attributes for file \"$file\" because : $^E\n";

foreach ( Powers_of_two($attrib) )
{
if ( exists $AttribVal->{$_} )
{
$result->{$AttribVal->{$_}} = 1
}
else
{
$result->{'UKNOWN_ATTRIBUTE_'.(2**$_)} = 0
}
}
$result
}









# Returns a list with the powers of 2 of the passed number

sub Powers_of_two ($)
{
my $number=shift || return undef;
my @powers=();

while ($number>0)
{
my $Log2 = int(log($number)/log 2);
$number -= 2**$Log2;
push @powers, $Log2
}
@powers
}

Re: Exported constants in module (HELP WITH)

am 30.01.2006 19:27:27 von Paul Lalli

Gregory Layman wrote:
> In article <1138630603.478498.318420@o13g2000cwo.googlegroups.com>,
> mritty@gmail.com says...
> > my $attr;
> > Win32::File::GetAttributes($ARGV[0], $attr) or die "Could not get
> > attrs: $!";
> > print "File is readonly\n" if $attr & READONLY;
> > print "File is hidden\n" if $attr & HIDDEN;
> > print "File is compressed\n" if $attr & COMPRESSED;
> > print "File is a directory\n" if $attr & DIRECTORY;
> > print "File is normal\n" if $attr & NORMAL;
> > print "File is archiveable\n" if $attr & ARCHIVE;
> >
> 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

Re: Exported constants in module (HELP WITH)

am 30.01.2006 21:26:00 von Gregory Layman

In article <1138645647.206904.86250@g49g2000cwa.googlegroups.com>,
mritty@gmail.com says...
> Gregory Layman wrote:
> > In article <1138630603.478498.318420@o13g2000cwo.googlegroups.com>,
> > mritty@gmail.com says...
> > > my $attr;
> > > Win32::File::GetAttributes($ARGV[0], $attr) or die "Could not get
> > > attrs: $!";
> > > print "File is readonly\n" if $attr & READONLY;
> > > print "File is hidden\n" if $attr & HIDDEN;
> > > print "File is compressed\n" if $attr & COMPRESSED;
> > > print "File is a directory\n" if $attr & DIRECTORY;
> > > print "File is normal\n" if $attr & NORMAL;
> > > print "File is archiveable\n" if $attr & ARCHIVE;
> > >
> > 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.

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

Re: Exported constants in module (HELP WITH)

am 30.01.2006 21:41:21 von Paul Lalli

Gregory Layman wrote:
> 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.

Good, good. perldoc should not be used as 'a' reference, it should be
used as *the* reference. :-) That is the first place you should go for
help and information, and only "scour the internet" if you can't
understand something it tells you or you can't find the information
within it.

> Again thanks for all your help.

Quite welcome.

Paul Lalli

Re: Exported constants in module (HELP WITH)

am 31.01.2006 06:24:35 von Gregory Layman

In article , "George Bouras" at : athens-pm@pm.org> says...
> use Win32::File;
>
> my $attr = Detailed_GetAttributes("$ENV{SYSTEMROOT}\\notepad.exe");
>
> foreach my $a (keys %{$attr})
> {
> print "$a\n"
> }
>
>
>
> # Returns a hash reference with the following keys if the corresponded
> # attribytes exists. If the attributes are missing, the keys will also
> absent
> # NORMAL is set when no attr is set and the file is ready for indexing.
> #
> # ARCHIVE 32 COMPRESSED 2048
> # DIRECTORY 16 HIDDEN 2
> # NORMAL 128 OFFLINE 4096
> # READONLY 1 SYSTEM 4
> # NOTINDEXING 8192 TEMPORARY 256
> #
>
> sub Detailed_GetAttributes ($)
> {
> local $_;
> my $file = $_[0];
> my $result = {};
> our $AttribVal = {13=>'NOTINDEXING', 5=>'ARCHIVE', 11=>'COMPRESSED',
> 4=>'DIRECTORY', 1=>'HIDDEN', 7=>'NORMAL', 12=>'OFFLINE', 0=>'READONLY',
> 2=>'SYSTEM', 8=>'TEMPORARY'} unless defined $AttribVal; # as powers of 2
>
> Win32::File::GetAttributes($file, my $attrib) || die "Could not get
> attributes for file \"$file\" because : $^E\n";
>
> foreach ( Powers_of_two($attrib) )
> {
> if ( exists $AttribVal->{$_} )
> {
> $result->{$AttribVal->{$_}} = 1
> }
> else
> {
> $result->{'UKNOWN_ATTRIBUTE_'.(2**$_)} = 0
> }
> }
> $result
> }
>
>
George,

Thanks for this. I had to catch up on some mathematics before I
understood what was going on, but I got through that. It's always
interesting to see the different ways that people disect a problem.
hehe

Thanks,
Greg