PERL: return command

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

PERL: return command

Post 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!
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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.
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

Post by vietboy505 »

Buddha443556, you're sure the master! thx alot :roll:

what is the one at the code for

1;
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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.
Post Reply