capture return value from a perl program

D

dufffman

Hi,

I am new to perl and trying out sample programs right now. I have a
perl script that does some DB work for me, and in the end always
returns me one number that it obtains from the DB. This perl program is
being called by a bourne shell script. What would I need to write at
the end of the perl script so that it returns a value at the end (not
an exit code, and value that I have extracted from the DB)?

Essentially I am treating the perl program as a function, where I give
it a sql, it analyzes it for me (because of its DB friendly libraries
as opposed to shell) and I want it to return me a value. So in my shell
script I will have a statement of the form,

RETURN_VALUE=`$HOME/sqlAnalyzer.pl`

and then I in my shell I could process the RETURN_VALUE variable such
as..

if [ $RETURN_VALUE -ge 10 ]
then
do this...
else
do this...
fi


Help is much appreciated.

Thanks,
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Hi,

I am new to perl and trying out sample programs right now. I have a
perl script that does some DB work for me, and in the end always
returns me one number that it obtains from the DB. This perl program
is being called by a bourne shell script. What would I need to write
at the end of the perl script so that it returns a value at the end
(not an exit code, and value that I have extracted from the DB)?

Essentially I am treating the perl program as a function, where I give
it a sql, it analyzes it for me (because of its DB friendly libraries
as opposed to shell) and I want it to return me a value.

No you don't. That is what exit codes are for.
So in my shell script I will have a statement of the form,

RETURN_VALUE=`$HOME/sqlAnalyzer.pl`

I am assuming backticks in a shell script capture the output of the
program run. If that is the case, you can print nothing but the value
you want to print at the end of the Perl script.
Help is much appreciated.

Good, but this has nothing to do with Perl.

Please read the posting guidelines for this group.

Sinan
 
I

it_says_BALLS_on_your forehead

Hi,

I am new to perl and trying out sample programs right now. I have a
perl script that does some DB work for me, and in the end always
returns me one number that it obtains from the DB. This perl program is
being called by a bourne shell script. What would I need to write at
the end of the perl script so that it returns a value at the end (not
an exit code, and value that I have extracted from the DB)?

Essentially I am treating the perl program as a function, where I give
it a sql, it analyzes it for me (because of its DB friendly libraries
as opposed to shell) and I want it to return me a value. So in my shell
script I will have a statement of the form,

RETURN_VALUE=`$HOME/sqlAnalyzer.pl`

and then I in my shell I could process the RETURN_VALUE variable such
as..

if [ $RETURN_VALUE -ge 10 ]
then
do this...
else
do this...
fi

Why use a shell script at all? Why not simply have your if/then logic
directly in the Perl script? if you need to call other scripts, or even
employ Unix commands, you can always use Perl's system() function.

#!/usr/bin/perl

use strict; use warnings;

my $return_value;
#...DB CALL, assigns a value to $return_value.

my $command2 = "script2.pl";
my $command3 = "script3.pl";
$return_value >= 10 ?
do { print "ge 10!\n"; system($command2) == 0 or die "$command2
failed: $?\n"; }
: do { print "lt 10\n"; system($command3) == 0 or die "$command3
failed: $?\n"; };
 
P

Paul Lalli

I am new to perl and trying out sample programs right now. I have a
perl script that does some DB work for me, and in the end always
returns me one number that it obtains from the DB. This perl program is
being called by a bourne shell script. What would I need to write at
the end of the perl script so that it returns a value at the end (not
an exit code, and value that I have extracted from the DB)?

I'm confused. Didn't you just say you have a perl script that returns
the number from the DB?

Do you already have such a program, or are you looking to create one?
Essentially I am treating the perl program as a function, where I give
it a sql, it analyzes it for me (because of its DB friendly libraries
as opposed to shell) and I want it to return me a value. So in my shell
script I will have a statement of the form,

RETURN_VALUE=`$HOME/sqlAnalyzer.pl`

Now I'm more confused. I thought you wanted the perl program to return
a value. This does not capture the program's return value, it
capture's the program's output. Which do you want?
and then I in my shell I could process the RETURN_VALUE variable such
as..

if [ $RETURN_VALUE -ge 10 ]
then
do this...
else
do this...
fi
Help is much appreciated.

I still don't understand what you're going for here.

If you want your perl program to print some value at the end of
execution, regardless of how it exists:

END {
print "Some value\n";
}

If you want your perl program to return a specific value via an exit
code:

exit(12);

Perhaps you could show short-but-complete perl and shell scripts that
demonstrate what you have currently and what you want to have instead?

Paul Lalli
 
T

Tad McClellan

What would I need to write at
the end of the perl script so that it returns a value at the end


print $some_special_value;

(not
an exit code, and value that I have extracted from the DB)?


The exit code _is_ the return value.

If you want something else, then what you want is not a "return value".

Essentially I am treating the perl program as a function, where I give
it a sql, it analyzes it for me (because of its DB friendly libraries
as opposed to shell) and I want it to return me a value.


No, you want it to make some *output* that you can capture.

So in my shell
script I will have a statement of the form,

RETURN_VALUE=`$HOME/sqlAnalyzer.pl`


Backticks in the shell capture *output*, not return values.
 
D

dufffman

I'm confused. Didn't you just say you have a perl script that returns
the number from the DB?

Do you already have such a program, or are you looking to create one?

I already have such a program.


Sorry for the confusing post. Let me try and clear it up.

I have written the perl script that connects to the DB, executes a
stored proc, and retrieves the value in one variable, and that variable
is back in subroutine Main at the end of the script. The point of this
perl program is to retrieve a value from the stored proc.

So I suppose I should be first asking, should I pass this value back to
the shell script as a return code or should I print it and let the
shell capture the output... which is better?

And regardless, I have been able to do neither with a test program.. I
will put up the sample .pl and .sh files..


sqlAnalyzer.pl

use strict;
use locale

Main(); #this is again something I am not clear on, but its working for
now, and I will try to
# get the proper syntax later.

sub Main {
my $value;
$value = 4;
print $value;
}


tester.sh

VALUE=`sqlAnalyzer.pl`

if [ $VALUE=1 ]
then
echo "its equal!"
else
echo "not equal"
fi

exit 0


The shell always prints out "its equal!"... Is that a little better?
Sorry for being unclear...

help is much appreciated.

thanks,
 
X

xhoster

VALUE=`sqlAnalyzer.pl`

if [ $VALUE=1 ]
then
echo "its equal!"
else
echo "not equal"
fi

exit 0
The shell always prints out "its equal!"... Is that a little better?

By setting the value of VALUE manually from the shell, It appears that your
problem is in your bash, not your perl.
 
D

dufffman

Yes thanks. That I concluded.. the only reason I am still posting here
is because the way the value is outputed from perl somehow seems to be
the problem.. And I was wondering if somoene could help me resolve
that...

Thanks,
VALUE=`sqlAnalyzer.pl`

if [ $VALUE=1 ]
then
echo "its equal!"
else
echo "not equal"
fi

exit 0
The shell always prints out "its equal!"... Is that a little better?

By setting the value of VALUE manually from the shell, It appears that your
problem is in your bash, not your perl.
 
X

xhoster

Yes thanks. That I concluded.. the only reason I am still posting here
is because the way the value is outputed from perl somehow seems to be
the problem..

No, the way it is outputted from Perl does *NOT* seem to be the problem.
As I said, when I set VALUE manually from the bash command line, having
absolutely nothing to do with Perl, your bash scripts still reports
"its equal!" no matter what I set VALUE to.


Xho
 
D

dufffman

ahh.. but of course.. my apologies...

learning too many new thigns and confusing them..

it should be

if [ $VALUE -eq 1 ]
.....

Thanks again for your help.. w/that change, I am also able to process
the value extracted from the perl script..

Just a couple more questions about perl in general..

- regarding the above issue, what is better practive... to capture the
output in this way from perl or use the return code from perl for my
purpose?

- As I had said in my earlier post, what is the subrouine Main()? Is
it like in java, or is it just a conventional name given to the
subroutine that hanldes the program? If it is just a name, then a perl
program is like a script in that, Main will have to be explicitly
called?

Thanks again..
 
D

dufffman

I am thinking the value passed as a return value is a better option as
there could be other print statements in the perl script which would
cause this logic to fail...

any feedback much appreciated.

many thanks,


ahh.. but of course.. my apologies...

learning too many new thigns and confusing them..

it should be

if [ $VALUE -eq 1 ]
....

Thanks again for your help.. w/that change, I am also able to process
the value extracted from the perl script..

Just a couple more questions about perl in general..

- regarding the above issue, what is better practive... to capture the
output in this way from perl or use the return code from perl for my
purpose?

- As I had said in my earlier post, what is the subrouine Main()? Is
it like in java, or is it just a conventional name given to the
subroutine that hanldes the program? If it is just a name, then a perl
program is like a script in that, Main will have to be explicitly
called?

Thanks again..

No, the way it is outputted from Perl does *NOT* seem to be the problem.
As I said, when I set VALUE manually from the bash command line, having
absolutely nothing to do with Perl, your bash scripts still reports
"its equal!" no matter what I set VALUE to.


Xho
 
A

A. Sinan Unur

(e-mail address removed) wrote in
any feedback much appreciated.

I am thinking you need to stop, take a deep breath, and read the posting
guidelines for this group. You started out with an off-topic posting, and
now you are rapidly shooting TOFU at us.

Sinan
 
D

dufffman

Fine. Thanks to everyone who did indeed help..

A. Sinan Unur said:
(e-mail address removed) wrote in


I am thinking you need to stop, take a deep breath, and read the posting
guidelines for this group. You started out with an off-topic posting, and
now you are rapidly shooting TOFU at us.

Sinan
 
J

J. Gleixner

ahh.. but of course.. my apologies...

learning too many new thigns and confusing them..

Yes. Learning how to debug your own scripts will save you, and us, a
lot of time.
it should be

if [ $VALUE -eq 1 ]
....

Thanks again for your help.. w/that change, I am also able to process
the value extracted from the perl script..

Just a couple more questions about perl in general..

- regarding the above issue, what is better practive... to capture the
output in this way from perl or use the return code from perl for my
purpose?

Use the return code, more appropriately termed the 'exit status'
or 'exit code' to detect success/failure. It's typically 0 for
success and some non-zero value for a failure.

perldoc -f die
perldoc -f exit

Read the value(s) from STDOUT, like you're doing, to get
data/output from your perl script into your shell script.
- As I had said in my earlier post, what is the subrouine Main()? Is
it like in java, or is it just a conventional name given to the
subroutine that hanldes the program? If it is just a name, then a perl
program is like a script in that, Main will have to be explicitly
called?

Just some subroutine someone named "Main". Nothing special. It could
have been "run_me", or "get_data_from_db", or anything else that
makes sense to you or others that have to read your code. It's not
needed at all, no subroutines are needed. A "Main" subroutine isn't
usually found in perl programs, it's usually something more
descriptive of the actual function of the subroutine.

Read some of the perldocs to learn more than you ever wanted
to know about perl.
 
X

xhoster

ahh.. but of course.. my apologies...

learning too many new thigns and confusing them..

it should be

if [ $VALUE -eq 1 ]
....

Thanks again for your help.. w/that change, I am also able to process
the value extracted from the perl script..

Just a couple more questions about perl in general..

- regarding the above issue, what is better practive... to capture the
output in this way from perl or use the return code from perl for my
purpose?

I would capture the output. For one thing, it is not clear to me that the
value you wish to relay will always fit into whatever integer range it is
that your system allows for return values. For another thing, if your
program fails for some unforeseen reason, you probably don't want that
failure-inspired return value to be interpreted as if it were an answer,
rather than the non-answer which it is.

- As I had said in my earlier post, what is the subrouine Main()? Is
it like in java, or is it just a conventional name given to the
subroutine that hanldes the program?

Is just a subroutine that happens to be named "Main", nothing special about
it. It may be conventional among some people to have a "Main" subroutine
in Perl, but if see it seems to be fairly narrow convention.

If it is just a name, then a perl
program is like a script in that, Main will have to be explicitly
called?

Yes.
 
T

Tad McClellan

I am thinking the value passed as a return value is a better option as


Using the "return value" is a worse option.

The exit value should be zero if your program succeeded, and
non-zero if your program failed.

there could be other print statements in the perl script which would
cause this logic to fail...


Then remove the other print statements. What's the problem?

any feedback much appreciated.


Do not top-post!


[snip TOFU]
 
T

Tad McClellan

[ text rearranged into chronological order, please stop posting
upside-down.
]




Does that mean that you _have_ read the posting guidelines for this group?

If so, then why do you continue to post upside-down?

Are you trying to make folks mad on purpose?

Thanks to everyone who did indeed help..


A. Sinan Unur did indeed help you, but I think your bad attitude may
preclude you from realizing how helpful it really was.

You can probably expect a good deal less help in your future posts.

So long!
 
J

Jürgen Exner

I am new to perl and trying out sample programs right now. I have a
perl script that does some DB work for me, and in the end always
returns me one number that it obtains from the DB. This perl program
is being called by a bourne shell script. What would I need to write
at the end of the perl script so that it returns a value at the end

exit() does that.
(not an exit code, and value that I have extracted from the DB)?

Then give exit the extracted value as argument:
exit ($myextractedvalue);
Essentially I am treating the perl program as a function, where I give
it a sql, it analyzes it for me (because of its DB friendly libraries
as opposed to shell) and I want it to return me a value. So in my
shell script I will have a statement of the form,

RETURN_VALUE=`$HOME/sqlAnalyzer.pl`

But here you are reading the _output_ of the perl script, not it's return
value.
You should make up your mind if you want to use the return value or the
output of the Perl script to communicate between the callee and the caller.

jue
 
E

Eckstein C.

A. Sinan Unur said:
(e-mail address removed) wrote in


I am thinking you need to stop, take a deep breath, and read the
posting guidelines for this group. You started out with an off-topic
posting, and now you are rapidly shooting TOFU at us.

Sinan, what is with this attitude? Perl has always had a rather
symbiotic realtionship with unix, and it's nothing new for it to get
some unix side question mixed in, if by mistake. Thee is no need ot be
so harsh when you encounter them.

Just step back a moment, relax, take a vaction, do something to get your
mind off computers, becasuee you've obviously been glued to yours way
too long if you're getting worked up about a bloody news group posting
liek this. I really don't mean to appear rude or callas, or demeaning in
any way or form, but this level of obsession just isn't healthy.

Just get a side hobby of sorts, like golf (or mini golf even), sloty car
racing, roller skating/blading, painting, music. Run a marathon. Take a
train cross country.

All I'm saying thre are far too manythings you cna do in life then
simply sit and monitor a news group for off topic posts all day. Like is
too short, and not to mention it's jsut better to get out from time to
time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top