Reg. Directory listing program

[email protected] ("Gunwant Singh")
Newsgroups perl.beginners
Message-ID <[email protected]>
Hi,

Let me thank you for your help. I think you guys are doing a great work, its
really appreciative.
Thanks to David and John. Now that I changed my *"perl"* code to the
following, its actually working!!!!

*use strict;
use warnings;
use File::stat;

opendir (DH, "subcode") or die "$!";
chdir("C:/Documents and Settings/Myself/Desktop/code/subcode");

foreach my $file(readdir DH)
{
my $value=stat($file);
my $perm=$value->mode & 07777;
printf "\n$file\t%04o\n", $perm;
}

closedir (DH);*


Leading to a question, why did we 'AND' *mode* to *07777*. What does each
bit stand for and why there  are 5 bits as compared to UNIX's
User-Group-Others bits. So actually 3 questions :)

Thank you.
-- 
Gunwant Singh


History:
-----------

Hi,

I am new to this mailing list and I am very new to PERL. So, please bear
with me for my questions.
I wrote a code that lists files in a directory with the permissions. I am
coding for MS-Windows.
Here is my code:

1 use strict;
2 use warnings;
3 use File::stat();
4
5 opendir (DH, "subcode") or die "$!";
6 foreach my $file(readdir DH)
7 {
8 my $perm=stat($file)->mode;
9 print "$file\t$perm\n";
10 }
11 closedir (DH);

I am getting the listing of file just fine if I dont add line 8. But as soon
as I try to display the permissions, I get this:

Can't call method "mode" without a package or object reference at
C:\Documents a
nd Settings\Myself\Desktop\code\dir-op.pl line 8.

What am I doing wrong!

Thanks.

-- 
Gunwant Singh
-------------------------------------------------------------------------------------------

> -----Original Message-----
> From: Gunwant Singh [mailto:[email protected]]
> Sent: Wednesday, June 04, 2008 10:02
> To: [email protected]
> Subject: Reg. Directory listing program
>
> Hi,
>
> I am new to this mailing list and I am very new to PERL. So,
> please bear
> with me for my questions.
> I wrote a code that lists files in a directory with the
> permissions. I am
> coding for MS-Windows.
> Here is my code:
>
> *1 use strict;
> 2 use warnings;
> 3 use File::stat();
> 4
> 5 opendir (DH, "subcode") or die "$!";
> 6 foreach my $file(readdir DH)
> 7 {
> 8 my $perm=stat($file)->mode;

       you need something like:
   my $filestat = stat($file);
   my $perm=$filestat->mode;
 but this will not work correctly unless you are already positioned on
the right directory location. So either you can do a chdir to the folder
you are doing or a concatenate of "subcode" . $file to get the file
status correctly.

Wags ;)

> 9 print "$file\t$perm\n";
> 10 }
> 11 closedir (DH);
>
> *I am getting the listing of file just fine if I dont add
> line 8. But as
> soon as I try to display the permissions, I get this:
>
> *Can't call method "mode" without a package or object reference at
> C:\Documents a
> nd Settings\Myself\Desktop\code\dir-op.pl line 8.
>
> *What am I doing wrong!
>
> Thanks.
>
> --
> Gunwant Singh
-----------------------------------------------------------------------------------------------

> -----Original Message-----
> From: Wagner, David --- Senior Programmer Analyst --- WGO
> [mailto:[email protected]]
> Sent: Wednesday, June 04, 2008 11:08
> To: Gunwant Singh; [email protected]
> Subject: RE: Reg. Directory listing program
>
> > -----Original Message-----
> > From: Gunwant Singh [mailto:[email protected]]
> > Sent: Wednesday, June 04, 2008 10:02
> > To: [email protected]
> > Subject: Reg. Directory listing program
> >
> > Hi,
> >
> > I am new to this mailing list and I am very new to PERL. So,
> > please bear
> > with me for my questions.
> > I wrote a code that lists files in a directory with the
> > permissions. I am
> > coding for MS-Windows.
> > Here is my code:
> >
> > *1 use strict;
> > 2 use warnings;
> > 3 use File::stat();
> > 4
> > 5 opendir (DH, "subcode") or die "$!";
> > 6 foreach my $file(readdir DH)
> > 7 {
> > 8 my $perm=stat($file)->mode;
>
>       you need something like:
>     my $filestat = stat($file);
>     my $perm=$filestat->mode;
       I was looking at Programming Perl and if you want to make sense
of the mode, then should 'and' with 07777 and print out using %04o (
octal output ), so something like:
       my $perm=$filestat->mode & 07777;

>  but this will not work correctly unless you are already positioned on
> the right directory location. So either you can do a chdir to
> the folder
> you are doing or a concatenate of "subcode" . $file to get the file
> status correctly.
>
> Wags ;)
>
> > 9 print "$file\t$perm\n";

       Then change print to
       printf "$file %04o\n", $perm;

Wags ;)

> > 10 }
> > 11 closedir (DH);
> >
> > *I am getting the listing of file just fine if I dont add
> > line 8. But as
> > soon as I try to display the permissions, I get this:
> >
> > *Can't call method "mode" without a package or object reference at
> > C:\Documents a
> > nd Settings\Myself\Desktop\code\dir-op.pl line 8.
> >
> > *What am I doing wrong!
> >
> > Thanks.
> >
> > --
> > Gunwant Singh
> >
>
> **********************************************************************
> This message contains information that is confidential and
> proprietary to FedEx Freight or its affiliates.  It is
> intended only for the recipient named and for the express
> purpose(s) described therein.  Any other use is prohibited.
> **********************************************************************
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>
Reply

Reply to all

Forward


John W. Krahn
Gunwant Singh wrote: > > Hi, Hello, perldoc -q "What.s the difference
between...

Jun 5 (2 days ago)
John W. KrahnLoading...
Jun 5 (2 days ago)
John W. Krahn to Perl

show details Jun 5 (2 days ago)


Reply


Gunwant Singh wrote:

    Hi,


Hello,


    I am new to this mailing list and I am very new to PERL.


perldoc -q "What.s the difference between .perl. and .Perl.?"



    So, please bear with me for my questions.
    I wrote a code that lists files in a directory with the permissions. I
am
    coding for MS-Windows.
    Here is my code:

    *1 use strict;
    2 use warnings;
    3 use File::stat();


perldoc -f use
[snip]
   use Module LIST
[snip]
           If you do not want to call the package's "import" method
           (for instance, to stop your namespace from being altered),
           explicitly supply the empty list:

                  use Module ();

So you are using the module File::stat but you are not importing any of its
methods.

perldoc File::stat
[snip]
DESCRIPTION
      This module's default exports override the core stat() and
      lstat() functions, replacing them with versions that return
      "File::stat" objects.



    4
    5 opendir (DH, "subcode") or die "$!";
    6 foreach my $file(readdir DH)
    7 {
    8 my $perm=stat($file)->mode;


Here you are calling the method "mode" but it is not available because
stat() is the Perl built-in function and not the overridden object that
File::stat provides.  Also you are trying to stat() $file in the current
directory and not in the "subcode" directory where it is actually located.



    9 print "$file\t$perm\n";
    10 }
    11 closedir (DH);

    *I am getting the listing of file just fine if I dont add line 8. But as
    soon as I try to display the permissions, I get this:

    *Can't call method "mode" without a package or object reference at
    C:\Documents a
    nd Settings\Myself\Desktop\code\dir-op.pl line 8.

    *What am I doing wrong!



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall
- Show quoted text -



-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply

Reply to all

Forward


John W. Krahn
> > >>> >>> From: Gunwant Singh [mailto:[email protected]] Better to do it
...

Jun 5 (1 day ago)
John W. KrahnLoading...
Jun 5 (1 day ago)
John W. Krahn to Perl

show details Jun 5 (1 day ago)


Reply


Wagner, David --- Senior Programmer Analyst --- WGO wrote:


        From: Wagner, David --- Senior Programmer Analyst --- WGO

            From: Gunwant Singh [mailto:[email protected]]
            9 print "$file\t$perm\n";


           Then change print to
           printf "$file %04o\n", $perm;


Better to do it this way:

       printf "%s %04o\n", $file, $perm;



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

-- 
- Show quoted text -
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply

Reply to all

Forward


Gunwant Singh to gunwant.singh

show details 12:23 AM (22 hours ago)


Reply




---------- Forwarded message ----------
From: Wagner, David --- Senior Programmer Analyst --- WGO <
[email protected]>
Date: Wed, Jun 4, 2008 at 11:37 PM
Subject: RE: Reg. Directory listing program
- Show quoted text -
To: Gunwant Singh <[email protected]>, [email protected]


> -----Original Message-----
> From: Gunwant Singh [mailto:[email protected]]
> Sent: Wednesday, June 04, 2008 10:02
> To: [email protected]
> Subject: Reg. Directory listing program
>
> Hi,
>
> I am new to this mailing list and I am very new to PERL. So,
> please bear
> with me for my questions.
> I wrote a code that lists files in a directory with the
> permissions. I am
> coding for MS-Windows.
> Here is my code:
>
> *1 use strict;
> 2 use warnings;
> 3 use File::stat();
> 4
> 5 opendir (DH, "subcode") or die "$!";
> 6 foreach my $file(readdir DH)
> 7 {
> 8 my $perm=stat($file)->mode;

       you need something like:
   my $filestat = stat($file);
   my $perm=$filestat->mode;
 but this will not work correctly unless you are already positioned on
the right directory location. So either you can do a chdir to the folder
you are doing or a concatenate of "subcode" . $file to get the file
status correctly.

Wags ;)

> 9 print "$file\t$perm\n";
> 10 }
> 11 closedir (DH);
>
> *I am getting the listing of file just fine if I dont add
> line 8. But as
> soon as I try to display the permissions, I get this:
>
> *Can't call method "mode" without a package or object reference at
> C:\Documents a
> nd Settings\Myself\Desktop\code\dir-op.pl line 8.
>
> *What am I doing wrong!
>
> Thanks.
>
> --
> Gunwant Singh
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.