Perl
Jul 12 2005, 02:44 PM
I have this perl script which is taking value from an external appliaction
and then after processing it is passing back a value to same application.
But I was not able to figure out how can I define the variable my $path
(which is taking value from external app). what should be the syntex.
Can somebody help me to set the syntex of the variable to accept value from
app.
use strict;
use File::Basename;
my $path = Value from other app;
my $base = basename($path);
my $dir = dirname($path);
print STDOUT $base;
Chris Devers
Jul 12 2005, 07:14 PM
On Tue, 12 Jul 2005, Perl wrote:
QUOTE |
I have this perl script which is taking value from an external appliaction and then after processing it is passing back a value to same application. But I was not able to figure out how can I define the variable my $path (which is taking value from external app). what should be the syntex. Can somebody help me to set the syntex of the variable to accept value from app.
use strict; use File::Basename; my $path = Value from other app; my $base = basename($path); my $dir = dirname($path); print STDOUT $base;
|
Try something like this:
use strict;
use File::Basename;
my $path = get_value_from_other_app();
my $base = basename($path);
my $dir = dirname($path);
print STDOUT $base;
sub get_value_from_other_app {
my $app = '/path/to/app';
my $value = system( "$app" )
or die "Couldn't successfully run $app: $!n";
return $value;
}
The get_value_from_other_app() could really just be reduced to
my $path = system( '/path/to/app' )
or die "Couldn't successfully run $app: $!n";
but if you want to get any fancier than that -- for example, if you want
more robust error handling, etc -- then having that code broken out into
a separate subroutine can make things easier to maintain, as the high
order functionality of the script is all together up top, and the
details of how sub-steps are done come later in chunks.
--
Chris Devers
Chris Devers
Jul 13 2005, 12:12 AM
On Wed, 13 Jul 2005, Dhanashri Bhate wrote:
QUOTE |
"system" will give the exit status of the application execution..
|
Whoops! I've had that backwards twice recently. Sorry for the confusion.
And, yes, the original problem could do for some clarification.
If nothing else, the platform this runs on may be a factor -- the
details of getting output from another program may differ on Windows,
for example...
--
Chris Devers
Dhanashri Bhate
Jul 13 2005, 03:21 AM
-> -----Original Message-----
-> From: Chris Devers [mailto:[Email Removed]]
-> Sent: Wednesday, July 13, 2005 5:44 AM
-> To: Perl
-> Cc: Perl Beginners List
-> Subject: Re: Pass a value to Perl script
->
->
-> On Tue, 12 Jul 2005, Perl wrote:
->
-> > I have this perl script which is taking value from an external
-> > appliaction and then after processing it is passing back a
-> value to
-> > same application. But I was not able to figure out how can
-> I define
-> > the variable my $path (which is taking value from external
-> app). what
-> > should be the syntex. Can somebody help me to set the
-> syntex of the
-> > variable to accept value from app.
-> Try something like this:
-> sub get_value_from_other_app {
-> my $app = '/path/to/app';
-> my $value = system( "$app" )
"system" will give the exit status of the application execution..
to get the actual output of the appln (i.e. stdout) , run the application putting it in back-quotes..
i.e. my $value=`/path/to/app`
as for passing the output of the perl script back to the application, you can pipe the output..
e.g. my-perl-script.pl | /path/to/app
but from your post, i did not get exactly what you are trying to do, by accepting in a perl script a value from an application , and passing a value back.. are you trying to write a perl subroutine to becalled from some another application??
Dhanashri
Tim Johnson
Jul 13 2005, 05:22 AM
If it is a console mode program then you can still use backticks in Windows. The only thing to watch out for is when a program prints to STDERR, which a few of the standard Windows command-line utilities will do in some circumstances. That, I believe, won't be caught by backticks (someone correct me if I'm wrong, I can't test it here and it's been a while since I used backticks).
________________________________
From: Chris Devers [mailto:[Email Removed]]
Sent: Tue 7/12/2005 10:12 PM
To: Dhanashri Bhate
Cc: Perl Beginners List; Perl
Subject: RE: Pass a value to Perl script
<snip>
If nothing else, the platform this runs on may be a factor -- the
details of getting output from another program may differ on Windows,
for example...
<snip>
Perl
Jul 13 2005, 01:36 PM
Dhanashri/ Chris
Yes!! exactly that is what I am trying to do. I am sorry for not making it
clear before.
here is the true picture. I have an APP which can run a perl script but just
before running the script I have a variable in that app which is holding a
value let say "c:documentstest.pl" also that value changes with every
record.
Now in my perl script I want to parse the string and just want to take the
file name which is "Test.pl" in my case and want to pass back (filename)
value of $base to that APP using STDOUT.
so far script is working fine If I am giving value to $path variable as a
constant (C:documentstest.p) but I do not how can get the value of $path
which APP is passing to script?
Thanks
"Dhanashri Bhate" <[Email Removed]> wrote in message
news:[Email Removed]...
-> -----Original Message-----
-> From: Chris Devers [mailto:[Email Removed]]
-> Sent: Wednesday, July 13, 2005 5:44 AM
-> To: Perl
-> Cc: Perl Beginners List
-> Subject: Re: Pass a value to Perl script
->
->
-> On Tue, 12 Jul 2005, Perl wrote:
->
-> > I have this perl script which is taking value from an external
-> > appliaction and then after processing it is passing back a
-> value to
-> > same application. But I was not able to figure out how can
-> I define
-> > the variable my $path (which is taking value from external
-> app). what
-> > should be the syntex. Can somebody help me to set the
-> syntex of the
-> > variable to accept value from app.
-> Try something like this:
-> sub get_value_from_other_app {
-> my $app = '/path/to/app';
-> my $value = system( "$app" )
"system" will give the exit status of the application execution..
to get the actual output of the appln (i.e. stdout) , run the application
putting it in back-quotes..
i.e. my $value=`/path/to/app`
as for passing the output of the perl script back to the application, you
can pipe the output..
e.g. my-perl-script.pl | /path/to/app
but from your post, i did not get exactly what you are trying to do, by
accepting in a perl script a value from an application , and passing a value
back.. are you trying to write a perl subroutine to becalled from some
another application??
Dhanashri
Errin M HMMA/Information Technol
Jul 13 2005, 03:07 PM
Perl wrote:
QUOTE |
Dhanashri/ Chris
Yes!! exactly that is what I am trying to do. I am sorry for not making it clear before. here is the true picture. I have an APP which can run a perl script but just before running the script I have a variable in that app which is holding a value let say "c:documentstest.pl" also that value changes with every record. Now in my perl script I want to parse the string and just want to take the file name which is "Test.pl" in my case and want to pass back (filename) value of $base to that APP using STDOUT. so far script is working fine If I am giving value to $path variable as a constant (C:documentstest.p) but I do not how can get the value of $path which APP is passing to script?
Thanks
|
Hi Dhanashri,
First, let me ask how your process is designed. Does your 'APP' call
the perl script? So you run the APP, the APP does stuff, then calls
your perl script, then returns to the APP and loops? Correct? Can you
tell us more about this 'APP'? Windows or UNIX (or other)? Is the
'APP' a script itself?
Also, have you checked out the File::Basename module? Here is some
code:
#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;
my $filename = shift;
my $base = basename $filename
print "The basename is $basen";
This code produces the following:
# basenametest /tmp/this/is/a/test.p
The base name is test.p
Hope this was helpful.
--Errin
Dhanashri Bhate
Jul 14 2005, 03:43 AM
-> -----Original Message-----
-> From: Larsen, Errin M HMMA/Information Technology Department
->
-> Hi Dhanashri,
->
Thanks for responding Errin, but the question was from "Perl [[Email Removed]]" , not from me! :)
-> tell us more about this 'APP'? Windows or UNIX (or other)? Is the -> 'APP' a script itself?
exactly what i would like to know.
-> Also, have you checked out the File::Basename module? Here is some code:
surely this will help if the APP is another perl scirpt..
QUOTE |
script is working fine If I am giving value to $path variable as a constant (C:documentstest.p) but I do not how can get the value of $path which APP is passing to script?
|
question is not clear enough to me.. if posible could you send me the part of code in ur APP where the perl script is called?
dhanashri
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.