Page 1 of 1

PERL: return command

Posted: Fri Feb 24, 2006 5:11 pm
by vietboy505
How can I use Perl to return to another script that I call?
Any help?

Here is my nameconfig.pl file.

Code: Select all

#!/usr/local/bin/perl

$names="a";

%name =(
"a" => "andy",
"b" => "ben",
"c" => "cat"
);

return "$name{$names}";
and here is my other script to call this nameconfig.pl

Code: Select all

#!/usr/local/bin/perl

$what=`nameconfig.pl` . " was here!";

print("NAME: " . $what . "\n\n");

$another=`nameconfig.pl`;

print("NAME: " . $another . "was here and it did work!\n\n");
OUTPUT

Code: Select all

Can't return outside a subroutine at C:\SCRIPT\nameconfig.pl line 11.
NAME:  was here!

Can't return outside a subroutine at C:\SCRIPT\nameconfig.pl line 11.
NAME: was here and it did work!

Posted: Fri Feb 24, 2006 5:44 pm
by Buddha443556
You could use require something like this:

Code: Select all

$names="a";

%name =(
"a" => "andy",
"b" => "ben",
"c" => "cat"
);

1;

Code: Select all

#!/usr/local/bin/perl
require 'nameconfig.pl';

$what = $name{$names} . " was here!";

print("NAME: " . $what . "\n\n");
Might want to look up use too.

Posted: Fri Feb 24, 2006 8:35 pm
by vietboy505
Buddha443556, you're sure the master! thx alot :roll:

what is the one at the code for

1;

Posted: Mon Feb 27, 2006 7:00 pm
by Buddha443556
The file required needs to return true to indicate any initialization executed successfully. Traditionally you end required files with "1;" to be safe.

Say you creating the config file with an empty hash:

Code: Select all

%config = ();
That would cause an error when required because the last value evaluates to false. Putting a "1;" at the end of the file solves the problem and you can handle the empty %config any way you like.