Re: Reg. Directory listing program

[email protected] ("John W. Krahn")
Newsgroups perl.beginners
Message-ID <[email protected]>
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
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.