[comp.lang.perl] MS Perl Questions

russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) (03/01/91)

I have just ftp'ed lenmsp41.zoo from devax.jpl.... and unpacked it on my PC.

It appears to work fine. Thanks!

Just one question. Is there and equivalent of #!.../perl for MS dos .bat files?

I had some example files from a previous release of msperl (which I deleted
since I don't have MS C) and these had
some stuff:

@REM=("
@perl 0%.bat ...
@end=")

or some such incantation that would appear to be trying to invoke perl.
I can not find anything in my DOS manual about this.(Surprise!)

Can some kind soul enlighten me please.

BTW I tried to invoke the bat file and got screeds of errors. I am sure it
never got anywhere near perl!

Cheers Russell.
-- 
Russell Fulton, Computer Center, University of Auckland, New Zealand.
<rj_fulton@aukuni.ac.nz>

dcd@tc.fluke.COM (David Dyck) (03/03/91)

In article <1991Mar1.041441.4810@ccu1.aukuni.ac.nz> russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
 ....
>Just one question. Is there and equivalent of #!.../perl for MS dos .bat files?
 ...
>@REM=("
>@perl 0%.bat ...
>@end=")
>

I have found that sometimes using 'exit' instead of 'end' works.
also add "if 0;" to end of '@end' line.
This may be msdos version specific.

I have used it both ways on the appropriate versions of dos
and it does work.

I just ran this on my ms-dos pc.

@REM=("
@c:\usr\perl %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
@exit ") if 0;

print "hello world\n";

you may want to use your correct path to perl,
although I believe that @perl... will work well also.


            David Dyck			dcd@tc.fluke.COM
      UUCP: {uunet,uw-beaver,decwrl,microsof,sun}!fluke!dcd

lbr@holos0.uucp (Len Reed) (03/05/91)

In article <1991Mar1.041441.4810@ccu1.aukuni.ac.nz> russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
>I have just ftp'ed lenmsp41.zoo from devax.jpl.... and unpacked it on my PC.

>It appears to work fine. Thanks!

You're welcome.

>Just one question. Is there and equivalent of #!.../perl for MS dos .bat files?
>
>I had some example files from a previous release of msperl (which I deleted
>since I don't have MS C) and these had
>some stuff:

>@REM=("
>@perl 0%.bat ...
>@end=")

This often won't work, and it didn't really work in patch level 18 when d.d.s.
suggested doing it.  "End" doesn't mean anything to command.com.  (Perhaps
d.d.s. had a command.com that treated "end" as a directive to quit
interpreting the batch file'.)  After perl runs, command.com starts
executing the perl lines after "@end" which often--but not always--gives
errors.

I could not find a general solution to this problem.
-- 
Len Reed
Holos Software, Inc.
Voice: (404) 496-1358
UUCP: ...!gatech!holos0!lbr

tdinger@hiredgun.East.Sun.COM (Tom Dinger - Sun BOS SPA) (03/06/91)

In article <1991Mar4.165742.12909@holos0.uucp> lbr@holos0.uucp (Len Reed) writes:
>In article <1991Mar1.041441.4810@ccu1.aukuni.ac.nz> russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
>>Just one question. Is there and equivalent of #!.../perl for MS dos .bat files?
>>
>>I had some example files from a previous release of msperl (which I deleted
>>since I don't have MS C) and these had
>>some stuff:
>
>>@REM=("
>>@perl 0%.bat ...
>>@end=")
>
>This often won't work, and it didn't really work in patch level 18 when d.d.s.
>suggested doing it.  "End" doesn't mean anything to command.com.  (Perhaps
>d.d.s. had a command.com that treated "end" as a directive to quit
>interpreting the batch file'.)  After perl runs, command.com starts
>executing the perl lines after "@end" which often--but not always--gives
>errors.
>
>I could not find a general solution to this problem.
>-- 
>Len Reed
>Holos Software, Inc.
>Voice: (404) 496-1358
>UUCP: ...!gatech!holos0!lbr

I have had success wrapping the perl script in the following sequence:

    @REM=("
    perl -S %0.BAT %1 %2 %3 %4 %5 %6 %7 %8 %9
    @goto end ") if 0 ;

    print "Howdy, world!\n";

    @REM=("
    :end ") if 0 ;

If you are using a DOS version before 3.30, you have some problems, as the
'@' character is not recognised as suppressing echo of the command, so you
will get a DOS "unrecognized command" error for those lines.  In this case,
remove the '@' from the "goto" line, and the script will still work, (aside
from the error message).

Limitations of the above:
1. This will only work if you type the batch file name without an extension:
   If you enter the command "t.bat" to DOS, perl will look for "t.bat.BAT"
   and give the error "Can't execute t.bat.BAT".

2. The batch script must be in a directory on your PATH.  The code in perl
   that searches the path does not start by default with the current
   directory, as DOS does.  (Maybe I should post a patch for this -- it looks
   pretty easy, and would only apply to DOS).

TD
------------------------------------------
Tom Dinger	     consulting at:
TechnoLogics, Inc.        Sun Microsystems    Internet: tdinger@East.Sun.COM
(508)486-8500             (508)671-0521       UUCP: ...!sun!suneast!tdinger





Tom Dinger	     consulting at:
TechnoLogics, Inc.        Sun Microsystems    Internet: tdinger@East.Sun.COM
(508)486-8500             (508)671-0521       UUCP: ...!sun!suneast!tdinger

dak@hparc0.HP.COM (Dave Kruger) (03/06/91)

> I just ran this on my ms-dos pc.
> 
> @REM=("
> @c:\usr\perl %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
> @exit ") if 0;
> 
> print "hello world\n";
> 

Well, I just ran this (I called it sneg.bat) on *my* ms-dos pc [sic], and got
the following response ([D:\TMP] HH:MM is my MS-DOS prompt):

	[D:\TMP] 11:14 sneg
	Parameter format not correct - "hello
	hello world

	[D:\TMP] 11:14
	[D:\TMP] 11:14 print "hello world\n";
	Parameter format not correct - "hello

By the way, I'm running the Beta version of MS-DOS 5.0.  However, the following
works well:

	@rem="
	@perl -s %0.BAT %1 %2 %3 %4 %5 %6 %7 %8 %9
	@goto exit ";

	# Print a mindless message
	print "hello world\n";
	
	# DOS exit label.
	@rem="
	:exit ";

In the first example, I don't see any way that DOS could fail to fall through
to, and try to execute the perl commands.  My example jumps over them.  For
tyros (of which I am one) here is an explanation:

COMMAND.COM handles this file as follows: (i) the first line is a comment,
(ii) for the second line, it executes perl using this very batch file as
input along with any other arguments that the batch file was invoked with,
(iii) when perl finishes, control falls through to the third line and goes to
the :exit label, (iv) at the :exit label, the batch file terminates and
control passes back to the command line.

(Please pick me up on any errors in this bit--as I say, I'm a perl tyro.  If
I'm in error, a gentle reminder will do--please send all flames to /dev/null.)

Perl handles this file as follows: (i) the first three lines are legal perl
code; they define an array called REM that has one element consisting of the
string "\n@perl -s %0.BAT %1 %2 %3 %4 %5 %6 %7 %8 %9\n@GOTO exit ", (ii) perl
then executes the print statement, (iii) again, the final two lines are legal
perl code; they redefine the REM array to contain the string "\n:exit ",
(iv) perl returns control to the calling batch file, %0.BAT.  The REM array
is never used or referenced by the perl commands--it is a convenience only.

4DOS USERS PLEASE STAND UP!
===========================

This all works as expected under MS-DOS's COMMAND.COM, however, I'm getting the
following behaviour under 4DOS (a shareware replacement for COMMAND.COM that is
approximately 3 million times better--MS-DOS 5.0 addresses some of the
limitations like aliasing and command history, but still falls a *long* way
short of 4DOS):

When you execute this command under 4DOS, it prints the message alright, but
instead of falling through to the :exit label, it continues through the file,
trying to execute each line of perl code as a DOS command:

	[d:\tmp] 11:28 sneg
	No closing quote
	hello world
	No closing quote
	# Print a mindless message
	Unknown command "#"
	print "hello world\n";
	Parameter format not correct - "hello
	# DOS exit label
	Unknown command "#"
	No closing quote

4DOS doesn't like the single, double-quote when it parses the third line. 
I've tried doubling the quote, but then perl complains.  Apparently, MS-DOS
proceeds as soon as it has parsed enough of the line to do so (in this case,
after it has read @GOTO exit).  However, 4DOS keeps reading the line, finds
the unmatched quote, complains, ignores the line completely, and falls through
to the next line.  Can anyone suggest any way around this problem?  Any help
will be really appreciated.

Dave K.

===============================================================================
Phone: +613 579 1254 (Home)            InterNet:  dak@hpauto.hpausa1.hp.com
       +613 895 2798 (Work)
       +613 898 9257 (FAX)
===============================================================================

nagler@olsen.UUCP (Rob Nagler) (03/08/91)

Len Read writes:
>>@REM=("
>>@perl 0%.bat ...
>>@end=")
> This often won't work, and it didn't really work in patch level 18 
> when d.d.s. suggested doing it....
> I could not find a general solution to this problem.

The following works ok with pl18 and pl44.  Keep your industrial
strength barf bags handy.

Create a file (in your path) called _#perl.bat which contains:
    @perl %1 %2 %3 %4 %5 %6 %7

The top of your perl file should have.
    @_#perl -S %0.bat %1 %2 %3 %4 %5 %6 %7
    ;

This works because batch files under DOS do not nest, they chain.
You could do something fancier, e.g. rename perl.exe to _#perl.exe
and have an empty batch file called _#exit (or whatever) and have
    @_#perl -S %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
    ;@_#exit
    ;
at the top, but this seems a lot more work for the benefit of 2 extra
args.

One minor problem with DOS perl, it doesn't automatically seach the
current directory with the -S option.  You can either put "." in your
path.  Of course, don't put any batch files in "\", because it won't
find 'em; "." doesn't exist in the root directory.

Tom Dinger writes:
> If you are using a DOS version before 3.30, you have some problems, as the
> '@' character is not recognised as suppressing echo of the command

The above scheme will work for earlier DOS, but you'll have to name
the _#perl.bat to be @_#perl.bat.

> Limitations of the above:
> 1. This will only work if you type the batch file name without an extension:
>    If you enter the command "t.bat" to DOS, perl will look for "t.bat.BAT"

Technically, @_#perl.bat or _#perl.bat could really be a perl program
that seaches for your perl file and then calls "do".  However, there
may be problems with DOS perl expanding args before you are ready,
i.e. you can't call "exec" because the line is greater than 128
characters after the expansion.  You could write a non-perl program
that would do this work with the expansion problems.  In practice,
"can't execute foo.bat" doesn't occur very often and I don't pass
very many "words" to my perl scripts anyway.  Stick with the above.

Flames to Bill Gates.

Rob nagler@olsen.ch

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (03/08/91)

As quoted from <1991Mar3.030351.24749@tc.fluke.COM> by dcd@tc.fluke.COM (David Dyck):
+---------------
| In article <1991Mar1.041441.4810@ccu1.aukuni.ac.nz> russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
| >@REM=("
| >@perl 0%.bat ...
| >@end=")
| 
| I have found that sometimes using 'exit' instead of 'end' works.
| also add "if 0;" to end of '@end' line.
+---------------

This requires MS-DOS 3.3; I don't know if @ still works in 4.x or not, but I
suspect it does.  (The usual use is to have "@echo off" as the first line of a
.BAT file, so the file doesn't advertize itself to the screen.)

The "if 0;" is unnecessary.  All Perl sees is an assignment of a single-
element list to an array (`@REM=("string");').

BTW, the "0%" should be "%0".

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

lbr@holos0.uucp (Len Reed) (03/09/91)

In article <1170002@hparc0.HP.COM> dak@hparc0.HP.COM (Dave Kruger) writes:
>4DOS USERS PLEASE STAND UP!
>===========================
>
>This all works as expected under MS-DOS's COMMAND.COM, however, I'm getting the
>following behaviour under 4DOS (a shareware replacement for COMMAND.COM that is
>approximately 3 million times better--MS-DOS 5.0 addresses some of the
>limitations like aliasing and command history, but still falls a *long* way
>short of 4DOS):

A couple of months ago I pulled an MS-DOS argument analyzer off the network
that claims to be MKS *and* 4DOS compatible.  (My code merely handled
MKS arguments.)  Tom Dinger or I will probably get that into the next
release of Mess-DOS-perl.

-Len
-- 
Len Reed
Holos Software, Inc.
Voice: (404) 496-1358
UUCP: ...!gatech!holos0!lbr

lbr@holos0.uucp (Len Reed) (03/11/91)

In article <220@munz.UUCP> nagler@olsen.ch (Rob Nagler) writes:
>
>One minor problem with DOS perl, it doesn't automatically seach the
>current directory with the -S option.  You can either put "." in your
>path.

I don't have the code in front of me here, but I suspect this is simply
because it's just executing the Unix code.  I seem to remember hooking
the system, pipe, etc., code to use Unix-stle path searching
(. only when explictly in the path) or DOS (. first come hell or high water)
based upon whether the MKS Unix-tools were running.  Obviously -S should
work the same as these, however that is, for consistency.
-- 
Len Reed
Holos Software, Inc.
Voice: (404) 496-1358
UUCP: ...!gatech!holos0!lbr

tdinger@hiredgun.East.Sun.COM (Tom Dinger - Sun BOS SPA) (03/12/91)

In article <1991Mar8.181952.2377@holos0.uucp> lbr@holos0.uucp (Len Reed) writes:
>In article <1170002@hparc0.HP.COM> dak@hparc0.HP.COM (Dave Kruger) writes:
>>4DOS USERS PLEASE STAND UP!
>>===========================
>>
>>This all works as expected under MS-DOS's COMMAND.COM, however, I'm getting the
>>following behaviour under 4DOS (a shareware replacement for COMMAND.COM that is
>>approximately 3 million times better--MS-DOS 5.0 addresses some of the
>>limitations like aliasing and command history, but still falls a *long* way
>>short of 4DOS):
>
>A couple of months ago I pulled an MS-DOS argument analyzer off the network
>that claims to be MKS *and* 4DOS compatible.  (My code merely handled
>MKS arguments.)  Tom Dinger or I will probably get that into the next
>release of Mess-DOS-perl.
>
>-Len
>-- 
>Len Reed
>Holos Software, Inc.
>Voice: (404) 496-1358
>UUCP: ...!gatech!holos0!lbr

OK, in the short run, for you 4DOS users, the wrapper below will correctly
work with both COMMAND.COM and 4DOS.  I have tested both; I had to go download
4DOS to do it, too.

	@REM=(qq!
	perl -S %0.BAT %1 %2 %3 %4 %5 %6 %7 %8 %9
	@goto end !) if 0 ;

	print "Howdy, world!\n";

	@REM=(qq!
	:end !) if 0 ;

This works using the "qq" quoting mechanism instead of double-quotes --
apparently, 4DOS expects double-quoted strings not to span multiple lines.

If the '!' character is too likely to be in the arguments somewhere, I have
also tested it with the '!' character replaced by a Control-A (you know, the
little simley-face).  But I can't send that version :-).

TD





Tom Dinger	     consulting at:
TechnoLogics, Inc.        Sun Microsystems    Internet: tdinger@East.Sun.COM
(508)486-8500             (508)671-0521       UUCP: ...!sun!suneast!tdinger