[comp.sys.mac.hypercard] global variables in code resources

moore@cs.washington.edu (Charles Moore) (07/26/90)

I am becoming increasingly frustrated by the prohibition against
global variables in code resources.  Is there any way around this?

One possibility for me, since I am writing a HyperCard XFCN is to make
callbacks to HyperCard and use HyperCard globals.  I'd like to avoid
this, though, since I assume callbacks to HyperCard will generally be
slow.  Also, it seems inelegant since I won't need this information
anymore when my XFCN completes.

Does anybody have any suggestions?

Charles Moore

lemke@radius.com (Steve Lemke) (07/26/90)

moore@cs.washington.edu (Charles Moore) writes:

>I am becoming increasingly frustrated by the prohibition against
>global variables in code resources.  Is there any way around this?

>One possibility for me, since I am writing a HyperCard XFCN is to make
>callbacks to HyperCard and use HyperCard globals.  I'd like to avoid
>this, though, since I assume callbacks to HyperCard will generally be
>slow.  Also, it seems inelegant since I won't need this information
>anymore when my XFCN completes.

>Does anybody have any suggestions?

Sure.  Use THINK Pascal 3.0.  I quote from the manual (page 157):

     Global data in code resources:

     Code resources cannot have global data.  You can take advantage
     of Pascal's nested procedures to simulate global data.  Just nest
     all of the routines in your program within "main".

Thus you would have something like this:

unit blah;
interface
    procedure main;
implementation
    procedure main;
    var
    global:integer;
       procedure other;
       begin
       end;
       procedure another;
       main
       end;
    begin
    end;
end.

>Charles Moore

--Steve
-- 
----- Steve Lemke, Engineering Quality Assurance, Radius Inc., San Jose -----
----- Reply to: lemke@radius.com     (Note: NEW domain-style address!!) -----

siegel@endor.harvard.edu (Rich Siegel) (07/26/90)

In article <12642@june.cs.washington.edu> moore@cs.washington.edu (Charles Moore) writes:
>I am becoming increasingly frustrated by the prohibition against
>global variables in code resources.  Is there any way around this?

	Yes. Use THINK C 4.0, which allows you to have code resource with
an A4 world, which provides for things like globals and segmentation.

R.



~~~~~~~~~~~~~~~
 Rich Siegel
 Staff Software Developer
 Symantec Corporation, Language Products Group
 Internet: siegel@endor.harvard.edu
 UUCP: ..harvard!endor!siegel

"I have been known, on occasion, to howl at the moon."

~~~~~~~~~~~~~~~

lemke@radius.com (Steve Lemke) (07/27/90)

I received the following message, and thought I'd redeem myself publicly...

}In article <1296@radius.com> lemke@radius.com (Steve Lemke) writes:
}> Sure.  Use THINK Pascal 3.0.  I quote from the manual (page 157):
}
}Explain to me please why one can't do this in MPW just as well?  The MPW
}Pascal compiler most certainly allows one to nest procedures.
}
} Tom Dowdy                 Internet:  dowdy@apple.COM
} Apple Computer MS:81EQ    UUCP:      {sun,voder,amdahl,decwrl}!apple!dowdy
} 20525 Mariani Ave         AppleLink: DOWDY1
} Cupertino, CA 95014


Oops, geez, I was spacing last night.  That didn't even occur to me.  I just
got THINK Pascal 3.0, and thought I'd look up "global" in the index.  I found
the section, as it pertained to code resources, and merely spewed forth what
I read there.

My original thinking was that since THINK Pascal supports globals in DAs
(which, correct me if I'm wrong, MPW does not), then it probably would allow
the same in a code resource.  Well, the section I typed in indicated that it
didn't support NORMAL globals, but that you could nest the procedures.  I
didn't even think about the fact that it would probably also work in just
about any other Pascal or C.

Sorry.

-- 
----- Steve Lemke, Engineering Quality Assurance, Radius Inc., San Jose -----
----- Reply to: lemke@radius.com     (Note: NEW domain-style address!!) -----

beard@ux5.lbl.gov (Patrick C Beard) (07/27/90)

In article <1296@radius.com> lemke@radius.com (Steve Lemke) writes:
#moore@cs.washington.edu (Charles Moore) writes:
#
#>I am becoming increasingly frustrated by the prohibition against
#>global variables in code resources.  Is there any way around this?
#
#>One possibility for me, since I am writing a HyperCard XFCN is to make
#>callbacks to HyperCard and use HyperCard globals.  I'd like to avoid
#>this, though, since I assume callbacks to HyperCard will generally be
#>slow.  Also, it seems inelegant since I won't need this information
#>anymore when my XFCN completes.
#
#>Does anybody have any suggestions?
#
#Sure.  Use THINK Pascal 3.0.  I quote from the manual (page 157):
#
#     Global data in code resources:
#
#     Code resources cannot have global data.  You can take advantage
#     of Pascal's nested procedures to simulate global data.  Just nest
#     all of the routines in your program within "main".
#
#Thus you would have something like this:
#
#unit blah;
#interface
#    procedure main;
#implementation
#    procedure main;
#    var
#    global:integer;
#       procedure other;
#       begin
#       end;
#       procedure another;
#       main
#       end;
#    begin
#    end;
#end.
#
#>Charles Moore
#
#--Steve

I don't agree.  THINK Pascal 3.0's lack of support for global variables
is appalling.  THINK C 4.0 supports REAL global variables that persist and
are accessible across files.  The other day I was modifying a code
resource written in THINK Pascal, and because I couldn't use REAL globals,
I had to add a ton of if statements in awkward places.  Sometimes REAL
globals solve problems in the most elegant way.

Use THINK C!

--
-------------------------------------------------------------------------------
-  Patrick Beard, Macintosh Programmer                        (beard@lbl.gov) -
-  Berkeley Systems, Inc.  ".......<dead air>.......Good day!" - Paul Harvey  -
-------------------------------------------------------------------------------

mxmora@unix.SRI.COM (Matt Mora) (07/27/90)

In article <6287@helios.ee.lbl.gov> beard@ux5.lbl.gov (Patrick C Beard) writes:
>In article <1296@radius.com> lemke@radius.com (Steve Lemke) writes:
>#
>#Thus you would have something like this:
>#
>#unit blah;
>#interface
>#    procedure main;
>#implementation
>#    procedure main;
>#    var
>#    global:integer;
>#       procedure other;
>#       begin
>#       end;
>#       procedure another;
>#       main
>#       end;
>#    begin
>#    end;
>#end.
>#--Steve
>
>I don't agree.  THINK Pascal 3.0's lack of support for global variables
>is appalling.  THINK C 4.0 supports REAL global variables that persist and
>are accessible across files.  The other day I was modifying a code
>resource written in THINK Pascal, and because I couldn't use REAL globals,
>I had to add a ton of if statements in awkward places.  Sometimes REAL


I sure would like to see how you solved that problem.


>globals solve problems in the most elegant way.
>Use THINK C!

>- Patrick Beard, Macintosh Programmer                        (beard@lbl.gov) -




Also, you can't have call backs using the nested procedure method.
For example, in a code resource I'm writing, I need to pass a procptr
to the teclickloop but I can't do that if the procedure is nested.

The procedure needs to be at the same level as MAIN. One way to
get around that is to stuff a handle into the window's refcon and
use that handle as your pseudo globals. Of course this method only 
works if you have a window to stuff the handle  in. This won't
work if it is not your window because someone else might be using
the wrefcon field.

Globals sure would make my life easier!


If anybody knows a way to get around this problem let me
know.








P.s. sorry about all the extra lines but Pnews would barf 
without them :-)




-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________

dan@hayes.fai.alaska.edu (Dan LaSota) (07/28/90)

How much do callbacks to HyperCard from externals slow the execution
of an Xternal.  I'm not looking for exact measurements just an overall
feeling.  Along the same lines is it faster to execute HyperTalk
commands from within a script of is it faster to make callbacks
from the Xternal's code?

Thanks
Dan LaSota

stadler@Apple.COM (Andy Stadler) (07/28/90)

>How much do callbacks to HyperCard from externals slow the execution
>of an Xternal?

The callback mechanism itself is very quick and direct.  Low overhead.

>Along the same lines is it faster to execute HyperTalk
>commands from within a script of is it faster to make callbacks
>from the Xternal's code?

If you are doing something which can be handled through one of the specific
function callback (like SetGlobal or GetFieldByXXX or NumToStr) then by all
means use the callback - it will be quite efficient.

If you are just sending single lines of HyperTalk, through SendCardMessage,
SendHCMessage, or EvalExpr, however, the interpreter will actually have to
parse, tokenize, interpret, and execute your line.  This won't make much
difference if you do it a single time;  but the interpreter tokenizes scripts
and keeps the tokens around, which means that -repeated- execution of a given
line will be much faster if it's in a script.

--Andy      stadler@apple.com