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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with variable assignment

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Why doesn't this variable assignment for $a work ? The result should be today 11 not 0 ???

Thanks a lot for help.


THE CODE:
#! /usr/bin/perl -w

my $cmd="date -u +%e";
my $a=system($cmd);

print "*************\n";
print "$a\n";
print "*************\n";

THE RESULT:
11
*************
0
*************
 
The system function returns an exit status for the command that was executed by the system, not what the command printed to STDOUT. The get the results of the command (date), use backticks (grave's accents, upper left on the keyboard) or open a pipe to the command.

$date = `date`;
print "$date\n";

or

$cmd = 'date';
open(INSTUFF,"$cmd |") or die "Failed to open pipe.\n";
while (<INSTUFF>) { print; }
close INSTUFF;


The second approach will let you handle the incoming stream as it comes in, the backticks stuff all the output of date into the variable.

'hope this helps...



keep the rudder amid ship and beware the odd typo
 
Perl has a built-in function called &quot;time&quot; which returns the current date/time in epoch seconds. There are also functions to manipulate that such as &quot;localtime&quot;. eg:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);


There are also modules such as Date::Manip which give you tons of date functionality. Look at CPAN for those.

So, I think you should find a Perl solution rather than using the system command. It would probably make it quicker. And it would certainly make it more portable.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top