Return a value from sub script to main script in Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jyothiakki
    New Member
    • Jul 2014
    • 1

    Return a value from sub script to main script in Perl

    Hi all,

    I have a main script MAIN.pl
    I want to call all my five subscripts :
    SUBSCRIPT1.pl
    SUBSCRIPT2.pl
    SUBSCRIPT3.pl
    SUBSCRIPT4.pl
    SUBSCRIPT5.pl
    to may main script MAIN.pl . All this script will have a return value like true or false . depending on this input " if all the subscripts pass then we have to print pass to the Main script ".

    I am new to perl . Please help me in this .

    Main program :::

    Code:
        use strict;
        use warnings;
     
        my $result = 'fail'; 
        print "How old are you? ";
        my $age = <STDIN>;
        if ($age < 13) {
        print "You are too young for this\n";
    	print $age;
        $result = 'pass';
    
        exit ;
        }

    one such subscript :

    Code:
        use strict;
        use warnings;
          
       # my $scar = system "perl script.pl";
       my $scar = `perl script.pl`;
        print " $scar \n";
    Here it is not printing the output as true for $scar.
    Last edited by RonB; Jul 2 '14, 02:03 PM. Reason: Added code tags
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Please use the code tags whenever you post any code. I added them for you here, but please remember to use them in the future.

    Your "main.pl" script isn't calling the "subscript" , unless you reversed those examples in your post.

    A better approach would be to convert each of those "subscript" scripts into subroutines and call each sub as needed. If you want, those subs could be defined in a separate (.pm module) file which is loaded via a use statement.

    Additionally, instead of having the "subscripts " output "pass" or "fail", they should output (or via a return statement) send back 1 or 0 which is the numerical equivalent of indicating true/false pass/fail.

    Comment

    Working...