how to run dos command in perl

J

jenson

I want to return the output and a dos command to a scalar variable and
display it.

The following code did not work. I think I made a mistake in using the `
sign. Please advise what needs to be changed to make the following code work
in window xp.

Any help is much appreciated.

open (DATE, `echo %date%`);
$theDate = <DATE>;
close(DATE);
print "This is $theDate \n";
 
A

A. Sinan Unur

I want to return the output and a dos command to a scalar variable and
display it.

The following code did not work. I think I made a mistake in using the
` sign. Please advise what needs to be changed to make the following
code work in window xp.

Any help is much appreciated.

open (DATE, `echo %date%`);
$theDate = <DATE>;
close(DATE);
print "This is $theDate \n";

Let's see.

First:

echo is an internal command implemented in cmd.exe. Unless you have
installed cygwin or something like that, the line above will not do what
you want it to do.

C:\Home> type ttt.pl
use strict;
use warnings;

unless(defined (my $temp = `echo %temp%`)) {
print "Command failed\n";
}

C:\Home> ttt
Command failed: Bad file descriptor at C:\Home\asu1\ttt.pl line 7.

On the other hand:

C:\Home> type ttt.pl
use strict;
use warnings;

if(defined (my $temp = `cmd.exe /c echo \%temp\%`)) {
print "TEMP = $temp\n";
} else {
die "Command failed: $!";
}

C:\Home> ttt
TEMP = C:\DOCUME~1\user\LOCALS~1\Temp

Second, do you have a program that sets the environment variable DATE
which then points to a file by that name or something. I am having
trouble making any sense of the four lines of code you posted.
 
T

Tintin

jenson said:
I want to return the output and a dos command to a scalar variable and
display it.

The following code did not work. I think I made a mistake in using the `
sign. Please advise what needs to be changed to make the following code work
in window xp.

Any help is much appreciated.

open (DATE, `echo %date%`);
$theDate = <DATE>;
close(DATE);
print "This is $theDate \n";

print "This is " . localtime() . "\n";
 
T

Tad McClellan

^^^^^^^^^^^^^^
^^^^^^^^^^^^^^

Please consider using something else there.

With that address, your posts become nearly invisible due to
others who've used it previously (with "X-No-Archive: yes",
so it can't be googled but I've saved the post in my "cemetery" :).


If this really is tintin, I'd like to not have you scored into oblivion...
 
T

Tintin

Tad McClellan said:
^^^^^^^^^^^^^^
^^^^^^^^^^^^^^

Please consider using something else there.

With that address, your posts become nearly invisible due to
others who've used it previously (with "X-No-Archive: yes",
so it can't be googled but I've saved the post in my "cemetery" :).

As I use new.individual.net, using the privacy.net address was the
recommended way of specifying a non valid address. However, I've just read
their FAQ (which has obviously been updated since I last read it) and have
updated it based on http://news.individual.net/faq.html#5.3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top