For Programmers: Free Programming Magazines  


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:
> 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

George Bouras

2006-01-30, 6:56 pm


"Paul Lalli" <mritty@gmail.com> wrote in message
news:1138630603.478498.318420@o13g2000cwo.googlegroups.com...
> Gregory Layman wrote:
>
> 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'm afraid I don't know what that sentence means.
>
>
> 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 ...


George Bouras

2006-01-30, 6:56 pm

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
}



Gregory Layman

2006-01-31, 7:55 am

In article <drlkv4$1fno$1@ulysses.noc.ntua.gr>, "George Bouras" <Find me
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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com