Nathan_84 has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise Monks,
I have a script below which i wish to play around but I cant even get it to open! When execute I recieve the following error
"Can't open /path/of/file/filename.ext! at ./modi.pl line 11."
now is it the case that the path has to be entered inside the script. I much rather have the user enter the path at the terminal. How would I do this?
As always Monks your advise is really appreciated.
#!/usr/bin/perl use File::stat; # Declare variables my $filename; my $FILE_HANDLE; my $file_stat; my $current_time; my $diff_seconds; $filename = '/path/of/file/filename.ext'; # Open the file open ($FILE_HANDLE, $filename) or die "Can't open $filename!"; # Get the file status structure $file_stat = stat($FILE_HANDLE); # Get the current time $current_time = time; # Get the elapsed time in seconds $diff_seconds = time - $file_stat->mtime; # Do whatever you want with the $diff_seconds print "\n$filename was modified $diff_seconds seconds back.\n"; # All done

Replies are listed 'Best First'.
Re: Error executing
by toolic (Bishop) on May 14, 2010 at 23:43 UTC
    or die "Can't open $filename!";
    Get more information on why the open failed by printing $!
    or die "Can't open $filename $!";
    Perhaps the file doesn't exist or you don't have permissions to open it.
    I much rather have the user enter the path at the terminal. How would I do this?
    You could prompt the user like this:
    use strict; use warnings; use File::stat; # Declare variables my $filename; my $FILE_HANDLE; my $file_stat; my $current_time; my $diff_seconds; print "enter a file:\n"; $filename = <STDIN>; chomp $filename; # Open the file open ($FILE_HANDLE, $filename) or die "Can't open $filename: $!";

    Or, pass it in on the command line, then use shift:

    use strict; use warnings; use File::stat; # Declare variables my $filename; my $FILE_HANDLE; my $file_stat; my $current_time; my $diff_seconds; $filename = shift; # Open the file open ($FILE_HANDLE, $filename) or die "Can't open $filename: $!";
      Hi Toolic,
      Thanks for your help.
      I used the first script you mentioned for prompting the user. Running the script showed when the file was last modified in seconds. I tried changing to show when the script was created but failed when changing the line $file_stat->mtime to $file_stat->ctime.
      It also got me wondering whether it would be better to produce a script which showed when created and last modified?
      Thanks again for time and help Toolic!

        Just a side note: the 'c' in ctime doesn't stand for "create" (a common misconception, though). Better think of it as "change", as it refers to when the file's status (i.e. inode meta information) was last changed.

        Now, you may wonder what the difference then is to modification time (mtime)...  The latter isn't updated when you change the mode/owner/group of the file, or the link count changes, etc.  For example:

        #!/bin/sh rm -f foo touch foo stat foo sleep 3 echo bar >>foo stat foo sleep 3 chmod o-r foo # changes ctime only stat foo

        Output:

        File: `foo' Size: 0 Blocks: 0 IO Block: 4096 regular empt +y file Device: 811h/2065d Inode: 36356982 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ almut) Gid: ( 1000/ almu +t) Access: 2010-05-15 11:08:00.000000000 +0200 Modify: 2010-05-15 11:08:00.000000000 +0200 Change: 2010-05-15 11:08:00.000000000 +0200 File: `foo' Size: 4 Blocks: 8 IO Block: 4096 regular file Device: 811h/2065d Inode: 36356982 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ almut) Gid: ( 1000/ almu +t) Access: 2010-05-15 11:08:00.000000000 +0200 Modify: 2010-05-15 11:08:03.000000000 +0200 <--- Change: 2010-05-15 11:08:03.000000000 +0200 <--- File: `foo' Size: 4 Blocks: 8 IO Block: 4096 regular file Device: 811h/2065d Inode: 36356982 Links: 1 Access: (0640/-rw-r-----) Uid: ( 1000/ almut) Gid: ( 1000/ almu +t) Access: 2010-05-15 11:08:00.000000000 +0200 Modify: 2010-05-15 11:08:03.000000000 +0200 Change: 2010-05-15 11:08:06.000000000 +0200 <---

        As you can see, the file was modified before it had been created... ;)

Re: Error executing
by Marshall (Canon) on May 16, 2010 at 08:46 UTC
    If you don't want to, there is no need for File::stat, you can the plain stat() function and get parameters that you want via list slice. See below for examples. Using Perl Stat

    The core stat function takes a $filepath, not a FILEHANDLE. my $mtime = (stat($filepath)[10] for example. The OO module File::stat works fine too.

      Thank you Marshall for the tip. I shall give that a go.