Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to embed a tool in my script

by angel_perl (Initiate)
on Feb 12, 2012 at 06:44 UTC ( #953288=perlquestion: print w/ replies, xml ) Need Help??
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 ); }

Comment on How to embed a tool in my script
Download Code
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`;
    Cheers,
    Darren

      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 foo.pl #!/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 foo.pl 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953288]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-12-19 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (85 votes), past polls