Loading ...
Sorry, an error occurred while loading the content.

27265Re: [PBML] stat function

Expand Messages
  • Paul.G
    Feb 2, 2012
    • 0 Attachment
      Thank you everybody for your help. I am currently looking at everybodies response however, Shaun I think answered my particular question.

      cheers



      ________________________________
      From: Shawn H Corey <shawnhcorey@...>
      To: perl-beginner@yahoogroups.com
      Sent: Sunday, 29 January 2012 11:30 PM
      Subject: Re: [PBML] stat function


       
      On 12-01-29 04:46 AM, Paul.G wrote:
      > Hi All
      >
      > Been looking at the stat function lately and have written a small bit of
      > code in perl, but having difficulty getting it to work when using a
      > while or foreach loop.
      >
      > #!/usr/bin/perl
      > use strict;
      > use warnings;
      > use File::stat;
      >
      > opendir (DIR, "/home");
      > while( my $file = readdir(DIR) ) {
      > my $fileattrib = stat($file)->size;
      > print "$file $fileattrib \n";
      > }
      > close DIR;
      >
      > I am getting the following error message, can someone tell me why this
      > might be the case.
      >
      > Can't call method "size" on an undefined value at ./compare3.pl line 8.
      >
      > I believe it is telling me that $file is underfined, but not sure why.
      >
      > cheers

      Oops, got distracted by the wrong error. Try this instead:

      #!/usr/bin/perl
      use strict;
      use warnings;
      use File::stat;

      my $dir = '/home';

      opendir( my $dir_handle, $dir );
      while( my $file = readdir($dir_handle) ) {
      my $fileattrib = stat( "$dir/$file" )->size;
      print "$file $fileattrib \n";
      }
      close $dir_handle;

      When you do the stat, you have to use the full path to the file. Your
      version was trying to file /home files in the current directory.

      --
      Just my 0.00000002 million dollars worth,
      Shawn

      Programming is as much about organization and communication
      as it is about coding.

      Strength is not a measure of the body.
      It's a measure of the heart.



      [Non-text portions of this message have been removed]
    • Show all 5 messages in this topic