Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-10

How do I load external Pascal code?

16 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrew Pennebaker

If I were using C, I would type:

#include "code.h"

If I were using Python, I would type:

import code

What's the command to load code in Pascal?

And where do the load commands go? Before variables? After? Within?

Cheers,

Andrew Pennebaker
www.yellosoft.us

Andrew Pennebaker
to Jonas Maebe

Is it possible to have a begin/end declaration in a unit a la C's main function? I would like my units to double as libraries and executables.

In other words, I'd like to be able to do
scriptedmain<http://rosettacode.org/wiki/ScriptedMain#Python>in Pascal.

Cheers,

Andrew Pennebaker
www.yellosoft.us

Jonas Maebe wrote 22 lines
On 13 Oct 2011, at 20:36, Andrew Pennebaker wrote: > If I were using C, I would type: > > #include "code.h" > > If I were using Python, I would type: > > import code > > What's the command to load code in Pascal? http://www.learn-programming.za.net/programming_pascal_learn12.html Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Andrew Pennebaker
to Jonas Maebe

Hmm. I tried rewriting begin/end as initialization/end, but scriptedmain.p still won't compile to an executable. fpc just creates a .PPU file.

scriptedmain.p
https://github.com/mcandre/scriptedmain/tree/master/pascal

Cheers,

Andrew Pennebaker
www.yellosoft.us

Jonas Maebe wrote 14 lines
On 13 Oct 2011, at 21:37, Andrew Pennebaker wrote: > Is it possible to have a begin/end declaration in a unit a la C's main > function? I would like my units to double as libraries and executables. Yes: http://www.freepascal.org/docs-html/ref/refse72.html Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to Andrew Pennebaker
Andrew Pennebaker wrote 10 lines
On Thu, Oct 13, 2011 at 3:40 PM, Jonas Maebe wrote: > On 13 Oct 2011, at 21:37, Andrew Pennebaker wrote: > >> Is it possible to have a begin/end declaration in a unit a la C's main >> function? I would like my units to double as libraries and executables. > > Yes: http://www.freepascal.org/docs-html/ref/refse72.html Hmm. I tried rewriting begin/end as initialization/end, but scriptedmain.p still won't compile to an executable. fpc just creates a .PPU file.

As the above-mentioned page says (emphasis mine):

***
A unit contains a set of declarations, procedures and functions that can be *used by a program* or another unit. ...
The initialization parts of the units are executed in the order that the compiler loaded the units *when compiling a program*. They are *executed before the first statement of the program* is executed. ***

Here's how you write a program: http://www.freepascal.org/docs-html/ref/refse71.html

You may also want to go through the entire tutorial I linked to earlier: http://www.learn-programming.za.net/programming_pascal_learn01.html

Jonas

Andrew Pennebaker
to Jonas Maebe

I do know how to compile a basic Pascal program:

$ cat hello.p
program Hello;
begin
writeln('Hello World!')
end.
$ fpc hello.p
$ ./hello
Hello World!

The problem I'm trying to solve is that it appears Pascal can't build executables from units, that is units by themselves.

$ cat scriptedmain.p
unit ScriptedMain;
interface
function MeaningOfLife () : integer;
implementation
function MeaningOfLife () : integer;
 begin
MeaningOfLife := 42
end;
initialization
write('Main: The meaning of life is: ');
writeln(MeaningOfLife())
end.
$ fpc scriptedmain.p
$ ./scriptedmain
-bash: ./scriptedmain: No such file or directory

I can USE the code just fine in a second file, test.p, and compile and run a test executable. But I'd like to be able to turn my units into self-sufficient executables (and later add command line options).

Cheers,

Andrew Pennebaker
www.yellosoft.us

Jonas Maebe wrote 39 lines
On 13 Oct 2011, at 22:32, Andrew Pennebaker wrote: > On Thu, Oct 13, 2011 at 3:40 PM, Jonas Maebe < >wrote: > >> On 13 Oct 2011, at 21:37, Andrew Pennebaker wrote: >> >>> Is it possible to have a begin/end declaration in a unit a la C's main >>> function? I would like my units to double as libraries and executables. >> >> Yes: http://www.freepascal.org/docs-html/ref/refse72.html > Hmm. I tried rewriting begin/end as initialization/end, but scriptedmain.p > still won't compile to an executable. fpc just creates a .PPU file. As the above-mentioned page says (emphasis mine): *** A unit contains a set of declarations, procedures and functions that can be *used by a program* or another unit. ... The initialization parts of the units are executed in the order that the compiler loaded the units *when compiling a program*. They are *executed before the first statement of the program* is executed. *** Here's how you write a program: http://www.freepascal.org/docs-html/ref/refse71.html You may also want to go through the entire tutorial I linked to earlier: http://www.learn-programming.za.net/programming_pascal_learn01.html Jonas_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Ralf A. Quint
Andrew Pennebaker wrote
I can USE the code just fine in a second file, test.p, and compile and run a test executable. But I'd like to be able to turn my units into self-sufficient executables (and later add command line options).

Looks like you do not understand the basic concept behind the use of units. They are not supposed to be "self-sufficient". Code in units are supposed to be re-usable code (snippets) across projects or to devide a larger program into smaller, better maintainable "pieces"...

Ralf

Andrew Pennebaker
to Ralf A. Quint

I understand that units are designed to be reusable code / libraries / APIs. It's a practical way to organize code, and a pretty good name for it, too.

In some languages, especially scripting languages, units can serve dual purposes. They can act as both a library and a self-sufficient program. RosettaCode describes this behavior as scripted
main<http://rosettacode.org/wiki/ScriptedMain>.
If it's possible to compile a Pascal unit such that it executes initialization code as if it were a typical program/begin/end file, then this dual behavior can be achieved.

Let's say I want to write a dual-purpose Fibonacci program, fib.p. It should:

1) Export a Fib (n : integer) function.
2) Compile to an executable fib.exe (or fib in Unix)
3) When the executable is run, it prompts the user for n, then prints Fib (n).

Cheers,

Andrew Pennebaker
www.yellosoft.us

Ralf A. Quint wrote 17 lines
At 08:02 PM 10/13/2011, Andrew Pennebaker wrote: I can USE the code just fine in a second file, test.p, and compile and run > a test executable. But I'd like to be able to turn my units into > self-sufficient executables (and later add command line options). > Looks like you do not understand the basic concept behind the use of units. They are not supposed to be "self-sufficient". Code in units are supposed to be re-usable code (snippets) across projects or to devide a larger program into smaller, better maintainable "pieces"... Ralf ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Søren Ager
to Andrew Pennebaker
Andrew Pennebaker wrote
In some languages, especially scripting languages,

Pascal is not a scripted language so it is not really something you want (or should imo) to do in Pascal

Let's say I want to write a dual-purpose Fibonacci program, fib.p. It should:

Then you should create 2 .p files 1 for the unit and 1 for the exe (that uses the unit)

If you really want to do it you will have to do it with ifdef's:

{$IFDEF exe}
program ScriptedMain;
{$ELSE}
unit ScriptedMain;
{$ENDIF}

and do that with everything that is different between exe and unit - not an elegant solution but it should work.

Hilsen Søren
Jürgen Hestermann
to Andrew Pennebaker: I understand that units are designed to be reusable code / libraries / APIs. It's a…
I understand that units are designed to be reusable code / libraries

/ APIs. It's a practical way to organize code, and a pretty good name for it, too.

In Pascal, units can have optional "initialization" and "finalization" code (syntax as shown in the link by Jonas). For all units you link into your program (with the "uses" clause) the initialization part is executed on startup and the finalization part is executed before the program ends in the order the units are loaded.

Jürgen Hestermann
to Andrew Pennebaker: I understand that units are designed to be reusable code / libraries / APIs. It's a…
In some languages, especially scripting languages, units can serve

dual purposes. They can act as both a library and a self-sufficient program. RosettaCode describes this behavior as scripted main. If it's possible to compile a Pascal unit such that it executes initialization code as if it were a typical program/begin/end file, then this dual behavior can be achieved.

In Pascal, units are only libraries of code. You need a separate program that loads the unit (with "uses") to get an executable. If you don't reuse the code in the unit you don't even need a unit, just write a program with the code. Units serve the only purpose of code libraries you can use in multiple programs.

Andrew Pennebaker
to Søren Ager: Pascal is not a scripted language so it is not really something you want (or should imo)…

Thanks, the conditional execution code helps a lot. Does $IFDEF look for an environment or Makefile variable?

Cheers,

Andrew Pennebaker
www.yellosoft.us

Søren Ager wrote 38 lines
On 2011-10-14 05:17, Andrew Pennebaker wrote: In some languages, especially scripting languages, > Pascal is not a scripted language so it is not really something you want (or should imo) to do in Pascal Let's say I want to write a dual-purpose Fibonacci program, fib.p. It > should: > Then you should create 2 .p files 1 for the unit and 1 for the exe (that uses the unit) If you really want to do it you will have to do it with ifdef's: {$IFDEF exe} program ScriptedMain; {$ELSE} unit ScriptedMain; {$ENDIF} and do that with everything that is different between exe and unit - not an elegant solution but it should work. -- Hilsen Søren ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Vladimir Zhirov
to Andrew Pennebaker
Andrew Pennebaker wrote
Thanks, the conditional execution code helps a lot. Does $IFDEF look for an environment or Makefile variable?

No, it looks for previously defined variables. You can define variables using -dMyVar command line switch when running the compiler, or {$DEFINE MyVar} in source code. It is possible to use the first approach in makefiles.

Andrew Pennebaker
to Vladimir Zhirov

Thanks Ager and Zhirov, the macros work!

StackOverflow<http://stackoverflow.com/questions/7759667/can-pascal-units-be-compiled-as-executables/7774138#7774138>

RosettaCode <http://rosettacode.org/wiki/Scripted_main#Free_Pascal>

Cheers,

Andrew Pennebaker
www.yellosoft.us

Vladimir Zhirov wrote 13 lines
Andrew Pennebaker wrote: > Thanks, the conditional execution code helps a lot. Does > $IFDEF look for an environment or Makefile variable? No, it looks for previously defined variables. You can define variables using -dMyVar command line switch when running the compiler, or {$DEFINE MyVar} in source code. It is possible to use the first approach in makefiles. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal