Execute function stored as string literal

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

Execute function stored as string literal

Post by Julcar »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Execute function stored as string literal

Post by fxm »

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: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Execute function stored as string literal

Post by badidea »

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: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Post by Julcar »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Execute function stored as string literal

Post by fxm »

Julcar wrote:Another question: can the pointer variable be Dimmed as Shared or stored in an array?
Yes.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Post by MrSwiss »

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: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Post by Julcar »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Post by MrSwiss »

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: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Post by Julcar »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Post by MrSwiss »

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: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Post by Julcar »

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: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Execute function stored as string literal

Post by Roland Chastain »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Execute function stored as string literal

Post by MrSwiss »

Roland Chastain wrote:What is the program supposed to do?
Well, CMS stands for [C]ontent [M]anagement 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: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Execute function stored as string literal

Post by Roland Chastain »

MrSwiss wrote:
Roland Chastain wrote:What is the program supposed to do?
Well, CMS stands for [C]ontent [M]anagement 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: 141
Joined: Oct 19, 2010 18:52
Contact:

Re: Execute function stored as string literal

Post by Julcar »

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