Execute function stored as string literal

General FreeBASIC programming questions.
Julcar
Posts: 72
Joined: Oct 19, 2010 18:52
Contact:

Execute function stored as string literal

Postby Julcar » Sep 28, 2020 20:58

Hi, maybe this was asked before, or sounds crazy but anyway, here comes

let's supposse I have something like this:

Code: Select all

Sub mySub()
  Print "hello world!"
End Sub

Dim as string theSubToExec
theSubToExec = "mySub"

Call (theSubToExec)


And thus, the last line would execute the procedure...

Is this possible? if possible, how?

many thanks.
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Execute function stored as string literal

Postby fxm » Sep 28, 2020 21:30

During run-time, the only way to choose which procedure to call from a variable is to use a procedure pointer:

Code: Select all

Sub mySub()
  Print "hello world!"
End Sub

Dim As Sub theSubToExec
theSubToExec = @mySub

theSubToExec()
Last edited by fxm on Sep 28, 2020 21:35, edited 1 time in total.
badidea
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Execute function stored as string literal

Postby badidea » Sep 28, 2020 21:35

You can use a procedure pointer: SubPtr or FunctionPtr

Code: Select all

Sub mySub()
  Print "hello world!"
End Sub

Dim theSubToExec As Sub() = @mySub

theSubToExec()

The wiki on the call keyword: "This keyword is a holdover from earlier dialects of BASIC, and is mainly deprecated."

Edit: fxm was faster :-)
Julcar
Posts: 72
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Postby Julcar » Sep 28, 2020 21:44

Looks just what I was looking for.

Another question: can the pointer variable be Dimmed as Shared or stored in an array?

Thanks so much
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Execute function stored as string literal

Postby fxm » Sep 28, 2020 21:49

Julcar wrote:Another question: can the pointer variable be Dimmed as Shared or stored in an array?

Yes.
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Postby MrSwiss » Sep 28, 2020 21:53

In FB you simply write (procedure) Identifier(argument-list):

Code: Select all

Sub PrintMSG(ByVal MSG As String)
    Print MSG
End Sub

PrintMSG("Hi, there")
PrintMSG("current time: " + Time)
PrintMSG("current date: " + Date)

#Ifdef __FB_PCOS__
Sleep
#endif
Julcar
Posts: 72
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Postby Julcar » Sep 28, 2020 22:52

MrSwiss wrote:In FB you simply write (procedure) Identifier(argument-list):

Code: Select all

Sub PrintMSG(ByVal MSG As String)
    Print MSG
End Sub

PrintMSG("Hi, there")
PrintMSG("current time: " + Time)
PrintMSG("current date: " + Date)

#Ifdef __FB_PCOS__
Sleep
#endif


is something way more complex (send output based on a env variable) executing an entire program, but I don't want to select-case every possible text
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Postby MrSwiss » Sep 28, 2020 23:00

Julcar wrote:is something way more complex (send output based on a env variable) executing an entire program, but I don't want to select-case every possible text

How about using a string array instead of a 'endless' select case?
Your env. number = array index ...
(strings could even be loaded from file, more maintenance friendly anyway)
Julcar
Posts: 72
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Postby Julcar » Sep 28, 2020 23:11

In this file I have a module loader procedure that receipts the request query string
http://chiselapp.com/user/julcar/reposi ... 97d595ba3f

In modules.bas I select-case the module name and execute the proper function declared in modules.bi
http://chiselapp.com/user/julcar/reposi ... 97d595ba3f
http://chiselapp.com/user/julcar/reposi ... 97d595ba3f

I want to avoid the mid step and pass the module main procedure from the same file
http://chiselapp.com/user/julcar/reposi ... 97d595ba3f
http://chiselapp.com/user/julcar/reposi ... 97d595ba3f

So instead of doing

Code: Select all

SUB LoadModule (ModuleName AS STRING)
  'Main module procedure trigger
  SELECT CASE ModuleName
    CASE "users"
      LoadUsersInterface()
    CASE "admin"
      LoadAdminPanel()
    CASE ELSE
  END SELECT
END SUB


I could do

Code: Select all

DIM ModuleMainProcedure AS SUB
...
ModuleMainProcedure = ProcPtr(LoadUsersInterface)
...
ModuleMainProcedure()
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Postby MrSwiss » Sep 28, 2020 23:33

I just love iguanas (my *virtual companeros during hurricane patricia striking Mexico in 2015).
*virtual = the hotel rooms walls had lots of metal-iguanas

Well, looks good to me "as-is".
With more different modules you might consider using callback procedures ...
(in thread "Wiki impovements" currently about callbacks in Documentation-Section)
Julcar
Posts: 72
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Postby Julcar » Sep 29, 2020 7:50

Thankyou so much guys, this commit is dedicated to those who gave me a hand yesterday: http://chiselapp.com/user/julcar/reposi ... e5b03821d8
Roland Chastain
Posts: 947
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Execute function stored as string literal

Postby Roland Chastain » Sep 29, 2020 10:26

By curiosity, I downloaded your project, compiled it and ran it, without having any idea of what it is. :)

Here is what I get:

[roland@localhost Iguana CMS-7d1a3fe5b0]$ ./index
Content-Type: text/html; Charset=



[roland@localhost Iguana CMS-7d1a3fe5b0]$


What is the program supposed to do?
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Postby MrSwiss » Sep 29, 2020 14:24

Roland Chastain wrote:What is the program supposed to do?

Well, CMS stands for [C]ontent [M]anagement [S]ystem, used on a Web-Server typically.
This lets you easily update "the content served" without changing the code (HTML/CSS/Scripts).
Roland Chastain
Posts: 947
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Execute function stored as string literal

Postby Roland Chastain » Sep 29, 2020 14:32

MrSwiss wrote:
Roland Chastain wrote:What is the program supposed to do?

Well, CMS stands for [C]ontent [M]anagement [S]ystem, used on a Web-Server typically.
This lets you easily update "the content served" without changing the code (HTML/CSS/Scripts).


I see. Thank you.
Julcar
Posts: 72
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Postby Julcar » Sep 29, 2020 16:53

Roland Chastain wrote:By curiosity, I downloaded your project, compiled it and ran it, without having any idea of what it is. :)

Here is what I get:

[roland@localhost Iguana CMS-7d1a3fe5b0]$ ./index
Content-Type: text/html; Charset=



[roland@localhost Iguana CMS-7d1a3fe5b0]$


What is the program supposed to do?


Well, if you have a webhosting account, yo could upload ./index to your public_html/cgi-bin folder and then execute it from any web browser.

Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests