Sponsored Content
Top Forums Shell Programming and Scripting How to get the return code of subroutines executed as standalone as command line in Perl ? Post 302420216 by ennstate on Tuesday 11th of May 2010 03:40:28 AM
Old 05-11-2010
How to get the return code of subroutines executed as standalone as command line in Perl ?

How to do I get the return code of a subroutine in a perl module if invoke the subroutine as standalone,

I have an module say TestExit.pm and in that i have a subroutine say myTest() which is returns 12, if i were to call the subroutine from
command line like

CASE:1 ( Without an explict exit in END routine )
Code:
perl -I/tmp/ -MTestExit -e 'myTest()'
echo $?
0

The above statement doesnot give me the return value of the subroutine ( which is 12 ) I get exit code as 0


Where as if I explicity do an exit in the END routine I am able to get the return code properly

Case2: ( With an explict an exit in END )
Code:
perl -I/tmp/ -MTestExit -e 'myTest()'
echo $?
12

Is there a way to pass an argument to the module or Is there a trick to get the exit code without modifying the END routing to an explict exit,


END routine exit is commented to get to match the case 1
TestExit.pm
Code:
package TestExit;

use strict;
use warnings;

our $status;

BEGIN
{
   $status = 0;
}

require Exporter;
our @ISA = qw(Exporter);

our %EXPORT_TAGS = (
                        'basic'    => [qw( myTest
                                       )],
                   );

Exporter::export_tags('basic');

sub myTest {
   print "Called myTest routine, Setting return code as 12\n";
   $status = 12;
}

1;

END {
  print "RETURN VAL:$status \n";
  #exit $status;
}

__END__

 

10 More Discussions You Might Find Interesting

1. Programming

How to get system() function executed cmd return value ?

Hi, How I can get system function executed command return value ? I want to know mv command success or not ? #include <stdio.h> main() { int ret; ret = system( "mv x.dat y.dat" ); printf( "system ret:\n", ret ); } (3 Replies)
Discussion started by: haiudhaya
3 Replies

2. Shell Programming and Scripting

how to get return code in one line

I know how to get the returning code of a function but wonder if I can combine the follwoing two lines into one: e.g.: #!/bin/shell ... #line 1 MyFunction arg1 arg 2 #line 2 rec=$? #this will be evaluated later .... like in c/c++, we'd write one line: rec=MyFunction(arg1, arg 2) ... (6 Replies)
Discussion started by: bluemoon1
6 Replies

3. Shell Programming and Scripting

perl - why is the shell script executed before the print command?

i'm writing some simple scripts to help me learn perl. why does the print command get called after the shell script is executed? the purpose of the shell script is to simply echo to the screen "script run". which is does, but before the print command, you can clearly see the shell script is... (3 Replies)
Discussion started by: mjays
3 Replies

4. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

5. Shell Programming and Scripting

C, sh, perl, system(): Can't transfer a return code appropriately: help, pls

I am using a perl-script from C-code, executing it by the 'system(..)' comand. The problem is to return the perl-return code to the C correctly. Default the 'system()' shell is Bourne: sh My try: (perl_src.c_pl - the perl script; t_sys - C-program with system() call (I will show it... (7 Replies)
Discussion started by: alex_5161
7 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

8. Homework & Coursework Questions

Help with perl subroutines

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This subroutine needs to check if there was a file name given on the command line. If so, return that. Otherwise... (1 Reply)
Discussion started by: yonkers062986
1 Replies

9. Shell Programming and Scripting

How can i use switches type arguments for subroutines in perl

i want to call subroutines in perl like: sub temp { ---- some code ----- } temp(-switchName, value1, --switchName2, value2) Like i know getoptions::Long is there for command line switches type arguments. So i want to know for subroutine type arguments. (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

10. Red Hat

Displaying command return in one line

Hello all I have a query (SQL) that returns a rather long field from an Oracle database. The field in question is defined on 400 characters but all these 400 cannot be displayed by the echo command. Thus when I launch the following command: echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s... (9 Replies)
Discussion started by: S. BASU
9 Replies
Sub::Install(3) 					User Contributed Perl Documentation					   Sub::Install(3)

NAME
Sub::Install - install subroutines into packages easily VERSION
version 0.927 SYNOPSIS
use Sub::Install; Sub::Install::install_sub({ code => sub { ... }, into => $package, as => $subname }); DESCRIPTION
This module makes it easy to install subroutines into packages without the unsightly mess of "no strict" or typeglobs lying about where just anyone can see them. FUNCTIONS
install_sub Sub::Install::install_sub({ code => &subroutine, into => "Finance::Shady", as => 'launder', }); This routine installs a given code reference into a package as a normal subroutine. The above is equivalent to: no strict 'refs'; *{"Finance::Shady" . '::' . "launder"} = &subroutine; If "into" is not given, the sub is installed into the calling package. If "code" is not a code reference, it is looked for as an existing sub in the package named in the "from" parameter. If "from" is not given, it will look in the calling package. If "as" is not given, and if "code" is a name, "as" will default to "code". If "as" is not given, but if "code" is a code ref, Sub::Install will try to find the name of the given code ref and use that as "as". That means that this code: Sub::Install::install_sub({ code => 'twitch', from => 'Person::InPain', into => 'Person::Teenager', as => 'dance', }); is the same as: package Person::Teenager; Sub::Install::install_sub({ code => Person::InPain->can('twitch'), as => 'dance', }); reinstall_sub This routine behaves exactly like "install_sub", but does not emit a warning if warnings are on and the destination is already defined. install_installers This routine is provided to allow Sub::Install compatibility with Sub::Installer. It installs "install_sub" and "reinstall_sub" methods into the package named by its argument. Sub::Install::install_installers('Code::Builder'); # just for us, please Code::Builder->install_sub({ name => $code_ref }); Sub::Install::install_installers('UNIVERSAL'); # feeling lucky, punk? Anything::At::All->install_sub({ name => $code_ref }); The installed installers are similar, but not identical, to those provided by Sub::Installer. They accept a single hash as an argument. The key/value pairs are used as the "as" and "code" parameters to the "install_sub" routine detailed above. The package name on which the method is called is used as the "into" parameter. Unlike Sub::Installer's "install_sub" will not eval strings into code, but will look for named code in the calling package. EXPORTS
Sub::Install exports "install_sub" and "reinstall_sub" only if they are requested. exporter Sub::Install has a never-exported subroutine called "exporter", which is used to implement its "import" routine. It takes a hashref of named arguments, only one of which is currently recognize: "exports". This must be an arrayref of subroutines to offer for export. This routine is mainly for Sub::Install's own consumption. Instead, consider Sub::Exporter. SEE ALSO
Sub::Installer This module is (obviously) a reaction to Damian Conway's Sub::Installer, which does the same thing, but does it by getting its greasy fingers all over UNIVERSAL. I was really happy about the idea of making the installation of coderefs less ugly, but I couldn't bring myself to replace the ugliness of typeglobs and loosened strictures with the ugliness of UNIVERSAL methods. Sub::Exporter This is a complete Exporter.pm replacement, built atop Sub::Install. EXTRA CREDITS
Several of the tests are adapted from tests that shipped with Damian Conway's Sub-Installer distribution. AUTHOR
Ricardo SIGNES <rjbs@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2005 by Ricardo SIGNES. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.18.2 2013-10-15 Sub::Install(3)
All times are GMT -4. The time now is 12:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy