Forum OpenACS Q&A: How to get a value from a tcl file to other tcl file

In my file x.tcl I am calling my pl/sql

set count_num [db_exec_plsql naveen {
begin
:1 := QUEUE_INSURANCE(
START_DATE => :start_date
);
end;
}]

puts $count_num

Now how do I get count_num in y.tcl file?

Thanks

The preferred style is to write a function
ad_proc get_start_date {args} {
  This is a function returning ....
} {
   set count_num ...
   ...
   return $count_num
}
and load this function as a library file (in OpenACS under mypackage/tcl/myfunctions-procs.tcl ... assuming you have a package named mypackage). This function (here get_start_date) can be called from you web-pages or other library files.

Help this helps
-gn

Thanks @Gustaf Neumann for the quick reply.

I created an ad_proc as suggested by you.

My proc gets input parameters(args) from x.tcl, so I am calling my proc in x.tcl file, the return value of my proc(return $count_num) should be passed to y.tcl.

I dont know how to pass return value to y.tcl

I cannot call my proc in y.tcl, because y.tcl don't have my input arguments.

Please suggest me

Thanks

It's not clear what you are trying to achieve.

1. One way is to store the value in a file (or back in the database) and then read it from y.tcl.

# 1.a (working with files) add this to the end of x.tcl file
set fp [open somefile.txt w]
puts $fp $count_num
close $fp

# 1.a (working with files) add this to the beginning of the y.tcl file
set fp [open somefile.txt]
set count_num [read $fp]
close $fp

# 1.b (working with db) add this to the end of x.tcl
# there are countless ways to do this, this is just a very basic example
# of what you can do. obviously, you would have to creat the table first:
# create table sometable (count_num integer);
db_dml delete_previous_count "delete from sometable"
db_dml save_count "insert into sometable (count_num) values (:count_num)"

# 1.b (working with db) add this to the beginning of y.tcl
set count_num [db_string get_count "select count_num from sometable"]

2. Another way is to source x.tcl in y.tcl but this would not work as you don't have the arguments of x.tcl in y.tcl. In any case, this is what you would need to do if you did have them:

# add this to the beginning of the y.tcl file
source x.tcl

3. Is this done in the context of a web app? Then, you have a form with those values and an action=y.tcl, which will then read the submitted query params with an ad_page_contract, i.e.:

# add this to the beginning of y.tcl
ad_page_contract {
@author yourname
} {
count_num:integer
}

PS. If you were to clarify what "getting count_num in y.tcl file" really means, it would be easier for us to answer.

Thanks  @Neophytos Demetriou

I am doing this for web app. I got solution for this, I did similar to your 3rd point.
I added the following code in my x.tcl which added count_num to "y.tcl url"

www.XXX/y?count_num=0

ad_returnredirect y?[ad_export_vars {count_num rel_type return_url}]

in y.tcl I added
ad_page_contract {
@author yourname
} {
count_num:optional
}

set count_num_p "f"
if {[exists_and_not_null count_num]} {
set count_num_p "t"
}

in y.adp

<if @count_num_p@ eq "t">
@count_num@
</if>