[26478] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8640 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 7 11:05:22 2005

Date: Mon, 7 Nov 2005 08:05:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 7 Nov 2005     Volume: 10 Number: 8640

Today's topics:
    Re: Cannot get exec syntax correct <usenet739_yahoo_com_au>
    Re: Cannot get exec syntax correct <1usa@llenroc.ude.invalid>
    Re: Cannot get exec syntax correct xhoster@gmail.com
    Re: threads in perl <zentara@highstream.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 8 Nov 2005 01:41:25 +1100
From: "Scott Bass" <usenet739_yahoo_com_au>
Subject: Re: Cannot get exec syntax correct
Message-Id: <436f6797$0$14234$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

Hi Matt, good points, see below...

"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message 
news:1dIbf.3787$EK.100468@news20.bellglobal.com...
>
> "Scott Bass" <usenet739_yahoo_com_au> wrote in message 
> news:436f437f$0$14227$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
>> Active State Perl 5.8
>> Windoze
>>
>> I'm trying to write a simple script that will glob all files with a 
>> particular extension, then run a system command against each file.  I 
>> want the script to fork off a process for each invocation, and not wait 
>> for the system command to finish.
>>
>> Here is what I have so far:
>>
>> #!perl
>>
>> # set pragmas
>> use strict;
>> use warnings;
>>
>> # define SAS command string(s)
>> my ($SAS,$args);
>>
>> # SAS 8
>> $SAS = q("C:/Program Files/SAS Institute/SAS/V8/sas.exe" -CONFIG 
>> "C:/Program Files/SAS Institute/SAS/V8/SASV8.CFG");
>>
>> # SAS 9
>> $SAS = q("C:/Program Files/SAS/SAS 9.1/sas.exe" -CONFIG "C:/Program 
>> Files/SAS/SAS 9.1/nls/en/SASV9.CFG");
>>
>
> Is this your real code? If so, why are you immediately overwriting the 
> $SAS variable?

Yes, if they want to run SAS 9, leave the code as is.  If they want to run 
SAS 8, comment out the SAS 9 line.  Sorry for the confusion.

>
>> # the rest of the options
>> $args = 
>> q( -rsasuser -icon -nosplash -batch -noterminal -no$syntaxcheck -initstmt 
>> "%include '../init.sas';" -sysin );
>>
>
> Uh, $syntaxcheck and %include have not been declared and are not 
> interpolated when you quote using q//. This may be why your command won't 
> run...

Sorry again, they are SAS options, and need to be quoted and not 
interpolated.

>
> [snip more code]
>
>> It's failing on line 44 (exec command).  I've read the doc 
>> file:///C:/Perl/html/lib/Pod/perlfunc.html, exec command, but am still 
>> having problems.
>>
>
> Could you please provide a better description of your problem. You're 
> expectation is that other people have SAS and can test this code, which is 
> rarely the case unless you're using built-in system commands...

I'm getting this error in the command window:

C:\Temp>'C:/Program' is not recognized as an internal or external command,
operable program or batch file.

I think this is due to the quoting of the SAS command string.

If I change code tidbits to:

# SAS 9
$SAS = q("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE");

 ...

   my $cmd = "$SAS $basename";

   if (! $debug) {
      chdir($dirname);
      exec ("$cmd");
   }

it works...sort of.  MS Word invokes, but only on the first file.  I need 
multiple MS Word sessions to launch for each file.

The doc says:

Using an indirect object with exec or system is also more secure. This usage 
(which also works fine with system()) forces interpretation of the arguments 
as a multivalued list, even if the list had just one argument. That way 
you're safe from the shell expanding wildcards or splitting up words with 
whitespace in them.
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I've tried a number of permutations from the example in the doc.  I either 
get the above error or:

Can't exec ""C:/Program Files/SAS/SAS 9.1/sas.exe" -CONFIG "C:/Program 
Files/SAS/SAS 
9.1/nls/en/SASV9.CFG"  -rsasuser -icon -nosplash -batch -noterminal -no$syntaxcheck 
 -initstmt "%include '../init.sas';" -sysin  print.sas": Invalid argument at 
C:\Documents and Settings\Scott Bass\My Documents\My Perl 
Scripts\RunSasPgms.pl line 44.

If I were to execute this in the command window, I would type (exactly, 
quotes and all):

"C:/Program Files/SAS/SAS 9.1/sas.exe" -CONFIG "C:/Program Files/SAS/SAS 
9.1/nls/en/SASV9.CFG" -rsasuser -icon -nosplash -batch -noterminal -no$syntaxcheck 
 -initstmt "%include '../init.sas';" -sysin test.sas

I have to 1) figure out how to do this with exec, and 2) is exec the proper 
approach (fork?  threads?) - I need it to invoke say 10 batch SAS sessions 
for say 10 files specified on the command line via wildcards.  I need the 
program to run asynchronously (not pause for each SAS session to finish).

Thanks...

>
> Matt
> 




------------------------------

Date: Mon, 07 Nov 2005 15:35:05 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Cannot get exec syntax correct
Message-Id: <Xns97076BAD81C98asu1cornelledu@127.0.0.1>

"Scott Bass" <usenet739_yahoo_com_au> wrote in
news:436f437f$0$14227$5a62ac22@per-qv1-newsreader-01.iinet.net.au: 

> I'm trying to write a simple script that will glob all files with a 
> particular extension, then run a system command against each file.  I
> want the script to fork off a process for each invocation, and not
> wait for the system command to finish.

Since you are running on Windows, why not use start?

> Here is what I have so far:

See below for my comments.

> #!perl
> 
> # set pragmas
> use strict;
> use warnings;
> 
> # define SAS command string(s)
> my ($SAS,$args);
> 
> # SAS 8
> $SAS = q("C:/Program Files/SAS Institute/SAS/V8/sas.exe" -CONFIG
> "C:/Program Files/SAS Institute/SAS/V8/SASV8.CFG");
> 
> # SAS 9
> $SAS = q("C:/Program Files/SAS/SAS 9.1/sas.exe" -CONFIG "C:/Program 
> Files/SAS/SAS 9.1/nls/en/SASV9.CFG");
> 
> # the rest of the options
> $args = 
> q( -rsasuser -icon -nosplash -batch -noterminal -no$syntaxcheck
> -initstmt "%include '../init.sas';" -sysin );
> 
> # include desired modules
> use Getopt::Long;
> use File::Basename;
> 
> # process command line options
> my $debug = '0';
> GetOptions ('debug!' => \$debug);
> 
> # glob files specified on the command line
> my @files;
> while (@ARGV) {
>    push @files, glob(shift @ARGV);
> }
> 
> # process each file
> my ($dirname, $basename);
> while (@files) {
>    $_ = shift @files;
>    $basename = basename($_);
>    $dirname  = dirname($_);
> 
>    my @cmd = ( "$SAS $args $basename" );
> 
>    if (! $debug) {
>       chdir($dirname);
>       exec { $cmd[0] } @cmd;

You are not really passing a list of arguments to exec, but a single 
string. This is really messed up. Do read perldoc -f system.

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw(canonpath catfile);

my %defaults = (
   dir => '.',
   ext => 'sas',
);

my %args = (
   %defaults,
   @ARGV,
);

opendir my $dir, canonpath($args{dir})
   or die "Cannot open directory: $args{dir}: $!";

FILE:
while(my $file = readdir $dir) {
   next FILE unless $file =~ m{ \.$args{ext} \z }x;
   run_sas($file);
}

closedir $dir
   or die "Cannot close directory: $args{dir}: $!";

my @SAS = (
   q{C:/Program Files/SAS Institute/SAS/V8/sas.exe},
   q{-CONFIG "C:/Program Files/SAS Institute/SAS/V8/SASV8.CFG"},
   qw(-rsasuser -icon -nosplash -batch -noterminal -no$syntaxcheck
   -initstmt "%include '../init.sas';" -sysin),
);

sub run_sas {
   my ($sas_file) = @_;

   system('start', @SAS, $sas_file);
}

__END__

I cannot test this as I do not have any SAS files that I wouldn't mind 
risking at this point.

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



------------------------------

Date: 07 Nov 2005 15:51:41 GMT
From: xhoster@gmail.com
Subject: Re: Cannot get exec syntax correct
Message-Id: <20051107105141.456$Wb@newsreader.com>

"Scott Bass" <usenet739_yahoo_com_au> wrote:
> Active State Perl 5.8
> Windoze
>
> I'm trying to write a simple script that will glob all files with a
> particular extension, then run a system command against each file.  I
> want the script to fork off a process for each invocation,
                     !!!!

> and not wait
> for the system command to finish.

 ...
>
>    my @cmd = ( "$SAS $args $basename" );

Why is @cmd an array if you are only assigning one thing to it?

>
>    if (! $debug) {
>       chdir($dirname);
>       exec { $cmd[0] } @cmd;
>    }
>    else {
>       print "cd $dirname\n";
>       print @cmd,"\n";
>    }
> }
> exit;
>
> It's failing on line 44 (exec command).

What does that mean?  Does it give an error message?  Does you computer
catch on fire?


> I've read the doc
> file:///C:/Perl/html/lib/Pod/perlfunc.html, exec command, but am still
> having problems.

Yes, reading docs doesn't magically fix your code.  You have to act on
the things you learned there.  For example, you should have learned that
exec *never returns*.  So why do you have it inside a loop?

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Mon, 07 Nov 2005 14:41:59 GMT
From: zentara <zentara@highstream.net>
Subject: Re: threads in perl
Message-Id: <6lpum113rqagigii3j65leci3upsv9ip9j@4ax.com>

On Mon, 07 Nov 2005 11:41:43 +0100, Krystian <nobody@this.home.com>
wrote:

>Hi there
>What options do i have to build an application with threads that will
>share some data? i found that below example doesn't work, probably it
>uses fork under the curtain:

You have it the opposite. Threads are used to emulate fork on win32
platforms.

The code you've shown does exactly what it is supposed to do.
You spawn 2 threads, and each gets a copy of $s, but each is a
different copy.

> output:
> s = blabla, ptr = SCALAR(0x1907a6c)
> s = blabla, ptr = SCALAR(0x29706e4)


If you want to use shared variables, you must declare it as shared.
Watch out for Threads vs. threads capitalization errors.

#!/usr/bin/perl
use threads;
use threads::shared;

my $foo : shared = 1;
my $bar = 1;

my $thr1=threads->new(sub { $foo++; $bar++ })->join;
my $thr2=threads->new(sub { $foo++; $bar++ })->join;

print "$foo\n";  #prints 3 since $foo is shared 
print "$bar\n";  #prints 1 since $bar is not shared 



-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8640
***************************************


home help back first fref pref prev next nref lref last post