LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2012, 06:02 AM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Rep: Reputation: 61
Perl: Get file modified date without using stat


The stat.pm module in several server Perl installations appears to be broken with this error, although it works elsewhere: -

Use of uninitialized value in stat at /usr/lib/perl5/5.8.8/File/stat.pm line 49.

Is there a way of getting a file's modified date without using stat?
 
Old 01-27-2012, 06:21 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
You can always use a system call ... in regard to the error, I'd guess that you may be sending it unsanitised data.
 
1 members found this post helpful.
Old 01-27-2012, 06:25 AM   #3
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Where is the unsanitised data here?

Code:
$file = "/path/to/some/file";
$file_handle = IO::File->new($file,'r');
$dttm_raw_modified = stat($file_handle)->mtime;
If I can't fix this, how do I do a system call?
 
Old 01-27-2012, 06:44 AM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
When I say unsanitised I mean that there may be strange characters or spaces or the file may not exist or may be a relative path that's not correct from your scripts working directory. Basically the data may not be accurate.

You can use system() to call any executable in the system, for example:
Code:
system("stat $filename")
 
1 members found this post helpful.
Old 01-27-2012, 07:10 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
Hi.

You will get that error if $file does not exists.
Try this:
Code:
use IO;
use File::stat;

$file = "/path/to/some/file";
$file_handle = IO::File->new($file,'r') or die "No $file: $!";
$dttm_raw_modified = stat($file_handle)->mtime;
print $dttm_raw_modified;
 
1 members found this post helpful.
Old 01-27-2012, 08:04 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Or without file handle
Code:
#!/usr/bin/perl

use File::stat;

$file = '/path/to/some/file';
$st = stat($file) or die "No $file: $!";

$dttm_raw_modified = $st->mtime;
print $dttm_raw_modified;
 
1 members found this post helpful.
Old 01-27-2012, 08:09 AM   #7
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Ah, got it - misleading error making me think it was a problem with stat.pm, when it's actually a null file handle. Cheers
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat file with its modified date n time coolplanet Programming 7 10-25-2009 09:44 PM
struct stat for a file , how can it be modified linuxdoniv Programming 1 08-05-2008 09:02 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 10:59 AM
[bash] Seeing if a file has been modified before/after a date w3stfa11 Programming 7 10-15-2006 07:02 PM
Where can I look for the config file for modified date and time ForumKid Linux - General 1 09-19-2003 09:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration