-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED]
Sent: Saturday, October 16, 2004 2:54 AM
To: [EMAIL PROTECTED]
Subject: Re: creating modules from your code
John V. Pataki wrote:
> Bill -- thanks I will try that tomorrow...
>
> The references back to main are really defined in main -- because the
> call originates as a macro compiled from the main (could be anything
> that the user wants) and the way I created this whole psuedo-api is
> that the last macro statement passed to the app is the next subroutine
> to be called in main. So it isn't really explicitly called in the
> module -- I just built the communication mechanism in the code that
> lives in the module.... This may be hard to explain.... unless you know
more about what I am doing...
>
> Here is a sample of the code at hand:
>
> This is my routine which monitors the pipe from the app --
>
> sub IPC_check_incoming {
>
> while (<PIPE>){push @message,$_};
> foreach $message (@message) {
> chomp $message;
> $message =~ s/\"//g;
> eval $message unless $message eq "";
> #print $@;
> } # end foreach
> #print " \n$file{myname} << $message[0] << I-DEAS" if @message;
>
> } # end sub
>
>
> It is here where the incoming command will be called - I have created
> this pseudo-API to work by calling subroutines - so the eval line
> executes whateve subroutine name is sent back from the app as the last
> item in the macro. It will come into the module as $message but
> $message will evaluate to a subroutine in main...
>
> So maybe I should write it as
>
> eval main::$message unless $message eq "";
I'm pretty sure that won't get it. First of all that's improper syntax.
What are the possible contents of $message ? Are they all sub calls ?
Always try to avoid eval if possible. What if your $message was a hash key
?
Then you coulddo something like:
my %hash = (fubar => \&fubar_sub, barfu => \&barfu_sub); if (exists
$hash{$message}) {
$coderef = $hash{$message};
&$coderef(maybe get some args from somewhere); }
The hash is safer than an eval.
--------------------------------------------------------------------
Bill,
Yes the are sub calls.
1) Why is eval unsafe? Doesn't it execute outside of the context of the
script so that it can't kill the program? I thought this made it safer than
calling it directly? Perhaps not? This syntax (as shown above in my
subroutine IPC_check_incoming) works for me when all of the code is in the
main program.
2) If I use your example of a hash to call the coderef instead of eval..
Does it imply that the coderef to be called has been defined in the Module?
If this is the case it won't get me where I would like to be which is that
the module should not have to in advance what the subroutine is to be
called.
Here is an example:
MAIN1
sub build_block_done {
...
}
sub build_cylinder_done {
...
}
-----------
MAIN2
Sub list_library_done {
...
}
Sub calc_mass_prop_done {
...
}
-----------
MODULE
IPC_check_incoming {
# the code in here should be able read the incoming command from the PIPE
# and execute the appropriate subroutine ... As the MAIN is waiting for the
response back from the app on the othe end of the PIPE - O
}
...
If I use your hash example -- can I define the elements of the hash in the
MAIN????? Hmmmm.... Perhaps that is what you are eluding to?
JOhn
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic
http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs