Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: calling .EXE through Perl script

by Anonymous Monk
on May 11, 2006 at 09:44 UTC ( [id://548655]=note: print w/replies, xml ) Need Help??


in reply to calling .EXE through Perl script

Here is my script which i tried...

use strict;
use warnings;

$dir = "C:\\parser_pgm\\test";
opendir(TXT, $dir) or die "Can't open $dir: $!";
while( defined ($file = readdir TXT) ) {
`parser_pgm.exe $file`;
}
closedir(XML);

Replies are listed 'Best First'.
Re^2: calling .EXE through Perl script
by Zaxo (Archbishop) on May 11, 2006 at 10:06 UTC

    Yes, you are calling backticks in void context. You're ignoring the screen output of your program, so you have no evidence that it ran. Try this modification:

    $dir = "C:\\parser_pgm\\test"; opendir(TXT, $dir) or die "Can't open $dir: $!"; while( defined ($file = readdir TXT) ) { print `parser_pgm.exe $file`; } closedir(TXT); # not XML

    Is that more like what you expect?

    After Compline,
    Zaxo

Re^2: calling .EXE through Perl script
by Thelonius (Priest) on May 11, 2006 at 10:38 UTC
    How are you running this script? If you run it from the command line, you'll get these errors:
    Global symbol "$dir" requires explicit package name at test.pl line 4. Global symbol "$dir" requires explicit package name at test.pl line 5. Global symbol "$dir" requires explicit package name at test.pl line 5. Global symbol "$file" requires explicit package name at test.pl line 6 +. Global symbol "$file" requires explicit package name at test.pl line 7 +. Execution of test.pl aborted due to compilation errors.
    If you're running the program by double-clicking on the file in Windows Explorer, you're probably not going to see the errors before the DOS box closes.

    I think this code is what you want:

    use strict; use warnings; my $dir = "C:\\parser_pgm\\test"; opendir(DIR, $dir) or die "Can't open $dir: $!"; while(my $file = readdir DIR) { my $pathname = "$dir\\$file"; if (-f $pathname) { # I added the -f test after running this and finding # out that readdir returns "." and ".." # (At least on the version of ActivePerl I am using) print STDERR "running parser_pgm.exe on $pathname\n"; # for debugg +ing system("parser_pgm.exe \"$pathname\""); # I added quotes in case there are spaces in the # file name (or in directory name in case you change it. if ($? != 0) { print STDERR "parser_pgm returned error $?\n"; } } } closedir(DIR);
      Hi Thelonius,
      Thanks a lot for your help. Indeed your code works. And as suggested by other monks I was not reading the output and hence was not aware if script really did the job.

      Thanks one and all for the needful.

      Regards
      Ashutosh
Re^2: calling .EXE through Perl script
by GrandFather (Saint) on May 11, 2006 at 09:58 UTC

    You should skip directories. At least '.' and '..' exist.

    use strict; use warnings; $dir = "C:\\parser_pgm\\test"; opendir(TXT, $dir) or die "Can't open $dir: $!"; while( defined ($file = readdir TXT) ) { next if ! -f $file; # Skip if not a file `parser_pgm.exe $file`; } closedir(TXT);

    Note also that I've changed the closedir(XML); to closedir(TXT); to match the opendir. The defined check is redundant by the way.


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://548655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-07-13 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.