How to execute a command line in Perlscript

E

Eric SALGON

Hi,

Is there someone who knows how to execute a command line (such a simple echo
cmd) in PerlScript.

I hve tryed '' or system() but the result is the same: no result but no error
!

This syntax works fine in a perl program but not in PerlScript

An idea ?

Eric
 
C

Chris

Eric said:
Hi,

Is there someone who knows how to execute a command line (such a simple echo
cmd) in PerlScript.

I hve tryed '' or system() but the result is the same: no result but no error
!

This syntax works fine in a perl program but not in PerlScript

An idea ?

Well, to answer your question successfully, I'll have to make some
assumptions. Some people, when they say "PerlScript," they really mean
"Perl script." But since there IS a product called "PerlScript," I'll
answer in that context. And even within THAT, there are two ways this
could be viewed: PerlScript under ASP and PerlScript under Windows
Scripting Host. I'll assume the later, but answer both.

PerlScript ASP cannot use 'print' or 'echo' to write to the end client
browser. You have to use $Response->Write(). Assuming you know that
already...

....the answer when running PerlScript in a WSH context is:

$WScript->Echo( "This is how to print in PerlScript\n" );

A tip: I personally like Ruby's use of a call named puts() which, named
in C-style convention, out Puts a string. I've adopted THAT as my
standard "outputing call" and I write wrappers in each environment I'm
in to handle it the right way (so I don't have to remember any more how
to write a string; it gets ridiculously out of hand esp. in the Windows
world where Microsoft can't get it together and each side of their house
invents a new standard for something so simple -- in ASP it's
Response.Write in WSH it's WScript.Echo, in Access it's print.debug or
whatever...)

So... I recommend this approach and then you never have to remember any
more:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

Chris
 
C

Chris

Eric said:
Hi,

Is there someone who knows how to execute a command line (such a simple echo
cmd) in PerlScript.

I hve tryed '' or system() but the result is the same: no result but no error
!

This syntax works fine in a perl program but not in PerlScript

An idea ?

I just realized I didn't answer your question in my last response.

First of all, as a general rule, and one that is outlined in the FAQ
(and asked about a lot here is) system() does NOT provide output. But
most especially in the Windows world where Microsoft writes their own
rules, this is also not the case in either ASP or WSH (still assuming
you are talking about "PerlScript" and not "Perl script.")

Using output wrappers in the ASP and WSH worlds for outputing a string,
the following code will work in both worlds for outputing a "system"
command:

sub shell {

my @output;
for (@_) { push( @output, `$_` ) }
chomp( @output );
return wantarray ? @output : \@output;

}

puts( shell( 'dir /l/on/' ) );

or you can:

puts( shell(
'dir',
'type c:\\autoexec.bat',
'dir c:\\winnt\\system32',
'command4',
'command5',
));

If:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

Incidentially using PerlScript in Windows allows you this convenience.
The "official" Microsoft "interface" (read "complicated and
convolluted") for executing a command in a scripting environment (both
ASP and WSH) is WshShell.Exec() (which takes two calls to set up.) You
can do this in PerlScript if you want, but the regular Perl way of
executing a system command works much smoother.

HTH,
Chris
 
E

Eric SALGON

Thx Chris for your answer,

My question was for "PerlScript" under ASP, and the 'Echo' command was only a
sample.

In fact, my goal is to run the 'dnscmd' command (which is used to manage dns
on w2000/w2003 server) and analyse the result.

Eric
 
E

Eric SALGON

Unfortunatly, I have implemented "shell" and "puts" functions in my ASP code
but `$_` return no result in @output array with a simple 'dir' command.

I'm afraid it's a problem of right on my IIS server.

Eric
 
C

Chris

Eric said:
echo

error


Well, to answer your question successfully, I'll have to make some
assumptions. Some people, when they say "PerlScript," they really mean
"Perl script." But since there IS a product called "PerlScript," I'll
answer in that context. And even within THAT, there are two ways this
could be viewed: PerlScript under ASP and PerlScript under Windows
Scripting Host. I'll assume the later, but answer both.

PerlScript ASP cannot use 'print' or 'echo' to write to the end client
browser. You have to use $Response->Write(). Assuming you know that
already...

...the answer when running PerlScript in a WSH context is:

$WScript->Echo( "This is how to print in PerlScript\n" );

A tip: I personally like Ruby's use of a call named puts() which, named
in C-style convention, out Puts a string. I've adopted THAT as my
standard "outputing call" and I write wrappers in each environment I'm
in to handle it the right way (so I don't have to remember any more how
to write a string; it gets ridiculously out of hand esp. in the Windows
world where Microsoft can't get it together and each side of their house
invents a new standard for something so simple -- in ASP it's
Response.Write in WSH it's WScript.Echo, in Access it's print.debug or
whatever...)

So... I recommend this approach and then you never have to remember any
more:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }
[Top Posting Fixed]
Thx Chris for your answer,

My question was for "PerlScript" under ASP, and the 'Echo' command was only a
sample.

In fact, my goal is to run the 'dnscmd' command (which is used to manage dns
on w2000/w2003 server) and analyse the result.

Well, hopefully you read my other answer where I was reading and
comprehending instead of reading and jumping the gun...

Chris
 
J

Jürgen Exner

[TOFU snipped]

Eric said:
Unfortunatly, I have implemented "shell" and "puts" functions in my
ASP code but `$_` return no result in @output array with a simple
'dir' command.

I'm afraid it's a problem of right on my IIS server.

But PerlScript is executed client side.
The web server (be it IIS or Apache or whatever) has nothing to do with it.

jue
 
K

ko

Jürgen Exner said:
[TOFU snipped]

Eric said:
Unfortunatly, I have implemented "shell" and "puts" functions in my
ASP code but `$_` return no result in @output array with a simple
'dir' command.

I'm afraid it's a problem of right on my IIS server.


But PerlScript is executed client side.
The web server (be it IIS or Apache or whatever) has nothing to do with it.

jue

Actually, ActiveState Perl allows you to do something like this with
PerlScript and IIS:

<% $Response->write( $_ . '<br>') foreach qx(dir) %>

To the OP - have you looked at the HTML documentation that comes with AS
Perl? The IIS/PerlScript stuff is pretty much right under the Table of
Contents...

keith
 
C

Chris

Jürgen Exner said:
[TOFU snipped]

Eric said:
Unfortunatly, I have implemented "shell" and "puts" functions in my
ASP code but `$_` return no result in @output array with a simple
'dir' command.

I'm afraid it's a problem of right on my IIS server.


But PerlScript is executed client side.
The web server (be it IIS or Apache or whatever) has nothing to do with it.

jue

Jurgen...! We've gone through this before once a year or so ago. :cool:
PERLSCRIPT IS NOT JUST CLIENT-SIDE!!!! In fact, it's run FAR more often
SERVER-SIDE than it is CLIENT-SIDE. Nobody uses it client-side. It's
useless. If you install ActiveState Perl on a server running IIS, it
gives you a chance to install the PerlScript ASP engine that runs JUST
LIKE VBScript and JScript on an IIS server... :cool: (It also allows you
to use it as a WSH language as well.)

I say all that as kindly as possible since it's so easy to misconstrue
things in print. But you keep saying that PerlScript is "client-side",
which is CAN BE, but it's FAR more prevalent (and far more useful)
server-side.

So the question and my answer has EVERYTHING to do with IIS...

Chris
 
C

Chris

Eric said:
echo

error


I just realized I didn't answer your question in my last response.

First of all, as a general rule, and one that is outlined in the FAQ
(and asked about a lot here is) system() does NOT provide output. But
most especially in the Windows world where Microsoft writes their own
rules, this is also not the case in either ASP or WSH (still assuming
you are talking about "PerlScript" and not "Perl script.")

Using output wrappers in the ASP and WSH worlds for outputing a string,
the following code will work in both worlds for outputing a "system"
command:

sub shell {

my @output;
for (@_) { push( @output, `$_` ) }
chomp( @output );
return wantarray ? @output : \@output;

}

puts( shell( 'dir /l/on/' ) );

or you can:

puts( shell(
'dir',
'type c:\\autoexec.bat',
'dir c:\\winnt\\system32',
'command4',
'command5',
));

If:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

Incidentially using PerlScript in Windows allows you this convenience.
The "official" Microsoft "interface" (read "complicated and
convolluted") for executing a command in a scripting environment (both
ASP and WSH) is WshShell.Exec() (which takes two calls to set up.) You
can do this in PerlScript if you want, but the regular Perl way of
executing a system command works much smoother.
[Top Posting Fixed]

Unfortunatly, I have implemented "shell" and "puts" functions in my ASP code
but `$_` return no result in @output array with a simple 'dir' command.

I'm afraid it's a problem of right on my IIS server.

If your tick marks above are an accurate portrayal of what you used in
your shell() code, then, no you will not get output. (You would only
get an echo back of the command(s) you wanted to run that way.) The
single tick marks in my shell() routine above are backward ticks. Use
qx() if this is confusing:

sub shell {

my @output;
for (@_) { push( @output, qx($_) } }
chomp( @output );
return wantarray ? @output : \@output;

}

I would be very surprised that you can't execute 'dir' under IIS. The
username running IIS -- usually the default IUSR_machinename account --
should have enough privilege to do a 'dir' (and under most circumstances
will give you a directory listing of C:\WINNT\SYSTEM32).

I'm wondering if it's not just a matter of the backwards tick, which the
function written using qx() above should solve.

Chris
 
C

Chris

Chris said:
Eric said:
Eric SALGON wrote:

Hi,

Is there someone who knows how to execute a command line (such a simple

echo

cmd) in PerlScript.

I hve tryed '' or system() but the result is the same: no result but no

error

!

This syntax works fine in a perl program but not in PerlScript

An idea ?


I just realized I didn't answer your question in my last response.

First of all, as a general rule, and one that is outlined in the FAQ
(and asked about a lot here is) system() does NOT provide output.
But most especially in the Windows world where Microsoft writes their
own rules, this is also not the case in either ASP or WSH (still
assuming you are talking about "PerlScript" and not "Perl script.")

Using output wrappers in the ASP and WSH worlds for outputing a
string, the following code will work in both worlds for outputing a
"system" command:

sub shell {

my @output;
for (@_) { push( @output, `$_` ) }
chomp( @output );
return wantarray ? @output : \@output;

}

puts( shell( 'dir /l/on/' ) );

or you can:

puts( shell(
'dir',
'type c:\\autoexec.bat',
'dir c:\\winnt\\system32',
'command4',
'command5',
));

If:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

Incidentially using PerlScript in Windows allows you this
convenience. The "official" Microsoft "interface" (read "complicated
and convolluted") for executing a command in a scripting environment
(both ASP and WSH) is WshShell.Exec() (which takes two calls to set
up.) You can do this in PerlScript if you want, but the regular Perl
way of executing a system command works much smoother.
[Top Posting Fixed]
Unfortunatly, I have implemented "shell" and "puts" functions in my
ASP code but `$_` return no result in @output array with a simple
'dir' command.

I'm afraid it's a problem of right on my IIS server.

If your tick marks above are an accurate portrayal of what you used in
your shell() code, then, no you will not get output. (You would only
get an echo back of the command(s) you wanted to run that way.) The
single tick marks in my shell() routine above are backward ticks. Use
qx() if this is confusing:

sub shell {

my @output;
for (@_) { push( @output, qx($_) } }
chomp( @output );
return wantarray ? @output : \@output;

}

The extra "}" at the end of the line "for (@_) { push..." in the shell()
routine should be removed. Should have been:

for (@_) { push( @output, qx($_) ) }

Chris
-----
Chris Olive
chris -at- --spammers-are-vermin-- technologEase -dot- com
http://www.technologEase.com
(pronounced "technologies")

"Uh? Yes... well... You don't know *everything*..."
--Puddleglum, _The Silver Chair_
 
E

Eric SALGON

Chris,

It was not a problem of backward ticks, no chance.
The result is unfortunatly the same ...

another idea ?

Thx

Eric

Eric said:
Eric SALGON wrote:

Hi,

Is there someone who knows how to execute a command line (such a simple


echo

cmd) in PerlScript.

I hve tryed '' or system() but the result is the same: no result but no


error

!

This syntax works fine in a perl program but not in PerlScript

An idea ?


I just realized I didn't answer your question in my last response.

First of all, as a general rule, and one that is outlined in the FAQ
(and asked about a lot here is) system() does NOT provide output.
But most especially in the Windows world where Microsoft writes their
own rules, this is also not the case in either ASP or WSH (still
assuming you are talking about "PerlScript" and not "Perl script.")

Using output wrappers in the ASP and WSH worlds for outputing a
string, the following code will work in both worlds for outputing a
"system" command:

sub shell {

my @output;
for (@_) { push( @output, `$_` ) }
chomp( @output );
return wantarray ? @output : \@output;

}

puts( shell( 'dir /l/on/' ) );

or you can:

puts( shell(
'dir',
'type c:\\autoexec.bat',
'dir c:\\winnt\\system32',
'command4',
'command5',
));

If:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

Incidentially using PerlScript in Windows allows you this
convenience. The "official" Microsoft "interface" (read "complicated
and convolluted") for executing a command in a scripting environment
(both ASP and WSH) is WshShell.Exec() (which takes two calls to set
up.) You can do this in PerlScript if you want, but the regular Perl
way of executing a system command works much smoother.

[Top Posting Fixed]
Unfortunatly, I have implemented "shell" and "puts" functions in my
ASP code but `$_` return no result in @output array with a simple
'dir' command.

I'm afraid it's a problem of right on my IIS server.

If your tick marks above are an accurate portrayal of what you used in
your shell() code, then, no you will not get output. (You would only
get an echo back of the command(s) you wanted to run that way.) The
single tick marks in my shell() routine above are backward ticks. Use
qx() if this is confusing:

sub shell {

my @output;
for (@_) { push( @output, qx($_) } }
chomp( @output );
return wantarray ? @output : \@output;

}

The extra "}" at the end of the line "for (@_) { push..." in the shell()
routine should be removed. Should have been:

for (@_) { push( @output, qx($_) ) }

Chris
-----
Chris Olive
chris -at- --spammers-are-vermin-- technologEase -dot- com
http://www.technologEase.com
(pronounced "technologies")

"Uh? Yes... well... You don't know *everything*..."
--Puddleglum, _The Silver Chair_
 
C

Chris

Eric said:
Chris said:
Eric SALGON wrote:


Eric SALGON wrote:


Hi,

Is there someone who knows how to execute a command line (such a simple


echo


cmd) in PerlScript.

I hve tryed '' or system() but the result is the same: no result but no


error


!

This syntax works fine in a perl program but not in PerlScript

An idea ?


I just realized I didn't answer your question in my last response.

First of all, as a general rule, and one that is outlined in the FAQ
(and asked about a lot here is) system() does NOT provide output.
But most especially in the Windows world where Microsoft writes their
own rules, this is also not the case in either ASP or WSH (still
assuming you are talking about "PerlScript" and not "Perl script.")

Using output wrappers in the ASP and WSH worlds for outputing a
string, the following code will work in both worlds for outputing a
"system" command:

sub shell {

my @output;
for (@_) { push( @output, `$_` ) }
chomp( @output );
return wantarray ? @output : \@output;

}

puts( shell( 'dir /l/on/' ) );

or you can:

puts( shell(
'dir',
'type c:\\autoexec.bat',
'dir c:\\winnt\\system32',
'command4',
'command5',
));

If:

ASP: sub puts { for (@_) { $Response->Write( "$_\n" ) } }
WSH: sub puts { for (@_) { $WScript->Echo( "$_\n" ) } }

Incidentially using PerlScript in Windows allows you this
convenience. The "official" Microsoft "interface" (read "complicated
and convolluted") for executing a command in a scripting environment
(both ASP and WSH) is WshShell.Exec() (which takes two calls to set
up.) You can do this in PerlScript if you want, but the regular Perl
way of executing a system command works much smoother.


[Top Posting Fixed]


Unfortunatly, I have implemented "shell" and "puts" functions in my
ASP code but `$_` return no result in @output array with a simple
'dir' command.

I'm afraid it's a problem of right on my IIS server.


If your tick marks above are an accurate portrayal of what you used in
your shell() code, then, no you will not get output. (You would only
get an echo back of the command(s) you wanted to run that way.) The
single tick marks in my shell() routine above are backward ticks. Use
qx() if this is confusing:

sub shell {

my @output;
for (@_) { push( @output, qx($_) } }
chomp( @output );
return wantarray ? @output : \@output;

}

The extra "}" at the end of the line "for (@_) { push..." in the shell()
routine should be removed. Should have been:

for (@_) { push( @output, qx($_) ) }
Chris,

It was not a problem of backward ticks, no chance.
The result is unfortunatly the same ...

another idea ?

Really weird. I suppose the "on topic" question is, what is in the
array that is returned by 'shell()' when you call it? Nothing? If you
are trying to do a directory listing or something like that, redirect
errors to the CONsole so you can see what the issue is:

puts( shell( 'dir 2>&1' ) );

Another thing to try would be to issue a command that leaves results
that can be otherwise inspected after the command is done. Try to
delete some file you know you should be able to delete and after the
script runs, go see if it's still there. Something like that. Perhaps
it *is* a permissions issue. Sounds like IIS might be setup in some
weird way after all.

Redirect all output to the CONsole no matter what as a trouble-shooting
measure (you may have already thought of that). If it's a permissions
issue, you should get SOMETHING back in the return array of 'shell()'.

Chris
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top