angel_perl has asked for the wisdom of the Perl Monks concerning the following question:

I am very new to perl programming and i was trying to write a program which read multiple files (*.pdb) from a folder and then i want those files should be read by a tool. So, how can i incorporate the tool in this script. I have the precompiled version of the tool.

use strict; my $directory = "c:\\"; opendir( DIR, $directory ) || die "Unable to open directory - $!\n"; my @files = grep /\.pdb/, readdir( DIR ); closedir( DIR ); foreach my $file (@files) { open( FH, "$directory\\$file" ) || die "Unable to open $file - $!\n"; while( <FH> ) { } close( FH ); }

Replies are listed 'Best First'.
Re: How to embed a tool in my script
by McDarren (Abbot) on Feb 12, 2012 at 07:19 UTC
    If you just want to pass these files one by one to your "tool", and you don't care about any output, then you can use system

    However, if your "tool" produces output that you want to capture and process, then you'll probably want to use backticks, eg:

    $output_from_my_precompiled_tool = `/path/to/my/tool $file`;

      Thanks @Darren, will the code look like this? and also i want the output to be printed for respective files as different files in different directory

      my $dir = '/path/to/dir'; # read only pd files: opendir DIR, $dir or die "read dir $dir - $!"; my @pdb_files = grep /\.pdb/, readdir DIR; closedir DIR; foreach my $file (@pdb_files) { open( FH,"$dir\\$file") while(<FH>) { $output_from_my_precompiled_tool = `/path/to/my/tool $file`; } close(FH); }
        Well, no. Probably not.

        I'm not exactly clear whether you want to pass each entire file to your external tool, or whether you want to open each file and then pass it line by line. I guess probably the former. If that's the case, then perhaps this example will help.

        $ echo 'some text' > 1.pdb $ echo 'some more text' > 2.pdb
        Lets say for example your external tool is /bin/cat, and you want to cat each file and capture the output.
        $ cat #!/usr/bin/perl use strict; use warnings; my $dir = '.'; my $cat = '/bin/cat'; opendir DIR, $dir or die "Cannot open directory $dir:$!\n"; my @pdb_files = grep { -f $_ && $_ =~ /\.pdb$/ } readdir DIR; closedir DIR; for my $file (@pdb_files) { my $content = `$cat "$dir/$file"`; chomp $content; print "Content of $file is $content\n"; }
        $ perl Content of 1.pdb is some text Content of 2.pdb is some more text
        That should be enough to get you going?
Re: How to embed a tool in my script
by zentara (Archbishop) on Feb 12, 2012 at 09:48 UTC
    Read "perldoc perlipc" for the many methods possible. IPC stands for Inter-Process Communication

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh