Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with system call

Status
Not open for further replies.

dneid

Technical User
Joined
Sep 1, 2007
Messages
3
Location
US
Hey, Guys,
I am writing a simple daily logger script and I want to get the system date to use as a tag in the file. I know this can be done, but I am doing something wrong (I am an occasional perl programmer).

So, what I want is execute the system date function (UNIX date) and get the returned data shoved into a variable. Something like:

$sillyVarName = system(date);

When I try this though the variable is empty. Can some one tell me what I am doing wrong. BTW I tried system('date')... all to no avail.

Any help would be appreciated.

Thanks,
Dale
 
$time = `date`;

[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
you may need to use the full path to the date command as well:

$time = `/bin/date`;

but there is really no need to shell out to the system date, use perl instead. Use POSIX to format the dte if necessary:

use POSIX qw/strftime/;



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Its unnecessarily expensive to fork a new process when you could get the same result from
Code:
my $time = scalar localtime();

[tt]localtime()[/tt] returns a properly formatted date string if called in scalar context and an array of date/time values
Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
if called in an array context.



["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top