Is the “callback†concept of programming existent in Bash?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
A few times when I read about programming I came across the "callback" concept.
Funnily, I never found an explanation I can call "didactic" or "clear" for this term "callback function" (almost any explanation I read seemed to me enough different from another and I felt confused).
Is the "callback" aspect of programming existent in Bash? If so, please also give an example.
The language I know the best is Bash, hence I seek an explanation in Bash.
bash function
add a comment |Â
up vote
1
down vote
favorite
A few times when I read about programming I came across the "callback" concept.
Funnily, I never found an explanation I can call "didactic" or "clear" for this term "callback function" (almost any explanation I read seemed to me enough different from another and I felt confused).
Is the "callback" aspect of programming existent in Bash? If so, please also give an example.
The language I know the best is Bash, hence I seek an explanation in Bash.
bash function
See stackoverflow.com/questions/5672289 for a similar question.
– dsstorefile1
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
A few times when I read about programming I came across the "callback" concept.
Funnily, I never found an explanation I can call "didactic" or "clear" for this term "callback function" (almost any explanation I read seemed to me enough different from another and I felt confused).
Is the "callback" aspect of programming existent in Bash? If so, please also give an example.
The language I know the best is Bash, hence I seek an explanation in Bash.
bash function
A few times when I read about programming I came across the "callback" concept.
Funnily, I never found an explanation I can call "didactic" or "clear" for this term "callback function" (almost any explanation I read seemed to me enough different from another and I felt confused).
Is the "callback" aspect of programming existent in Bash? If so, please also give an example.
The language I know the best is Bash, hence I seek an explanation in Bash.
bash function
bash function
asked 4 hours ago
JohnDoea
171523
171523
See stackoverflow.com/questions/5672289 for a similar question.
– dsstorefile1
2 hours ago
add a comment |Â
See stackoverflow.com/questions/5672289 for a similar question.
– dsstorefile1
2 hours ago
See stackoverflow.com/questions/5672289 for a similar question.
– dsstorefile1
2 hours ago
See stackoverflow.com/questions/5672289 for a similar question.
– dsstorefile1
2 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
In typical imperative programming, you write sequences of instructions and they are executed one after the other, with explicit control flow:
touch file
if [ -f otherfile ]; then
cp otherfile otherfile2
fi
etc.
You can follow the execution flow quite easily, always working your way up from any given line of code to determine its execution context, knowing that any instructions you give will be executed as a result of their location in the flow (or their call sites’ locations, if you’re writing functions).
When you use callbacks, instead of placing the use of a set of instructions “geographicallyâ€Â, you describe when it should be called. Typical examples in other programming environments are cases such as “download this resource, and when the download is complete, call this callbackâ€Â. Bash doesn’t have a generic callback construct of this kind, but it does have callbacks, for error-handling and a few other situations; for example:
#!/bin/bash
cleanup()
echo This could delete temporary files etc
trap cleanup EXIT
echo Doing lots of stuff...
sleep 2
My code here never explicitly calls the cleanup
function; it tells Bash when to call it, using trap cleanup EXIT
, i.e. “dear Bash, please run the cleanup
command when you exit†(and cleanup
happens to be a function I defined earlier, but it could be anything Bash understands). Bash supports this for all non-fatal signals, exits, command failures, and general debugging (you can specify a callback which is run before every command).
You can use Bash’s ability to evaluate shell parameters as commands, to build a callback-oriented framework; that’s somewhat beyond the scope of this answer, and would perhaps cause more confusion by suggesting that passing functions around always involves callbacks. See Bash: pass a function as parameter for some examples of the underlying functionality.
add a comment |Â
up vote
2
down vote
First it's important to note that what makes a function a callback function is how it's used, not what it does. A callback is when code that you write is called from code that you didn't write. You're asking the system to call you back when some particular event happens.
An example of a callback in shell programming is traps. A trap is a callback that isn't expressed as a function, but as a piece of code to evaluate. You're asking the shell to call your code when the shell receives a particular signal.
Another example of a callback is the -exec
action of the find
command. The job of the find
command is to traverse directories recursively and process each file in turn. By default, the processing is to print the file name (implicit -print
), but with -exec
the processing is to run a command that you specifies. This fits the definition of a callback, although at callbacks go, it is not very flexible since the callback runs in a separate process.
If you implemented a find-like function, you could make it use a callback function to call on each file. Here's an ultra-simplified example that takes a function name (or external command name) as argument and calls it on all regular files in the current directory and its subdirectories. The function is used as a callback which is called every time call_on_regular_files
finds a regular file.
shopt -s globstar
call_on_regular_files ()
declare callback="$1"
declare file
for file in **/*; do
if [[ -f $file ]]; then
"$callback" "$file"
fi
done
Callbacks aren't as common in shell programming as in some other environments because shells are primarily designed for simple programs. Callbacks are more common in environments where data and control flow are more likely to move back and forth between parts of the code that are written and distributed independently: the base system, various libraries, the application code.
+1, just a note: I think the "ultra-simplified" term isn't right, becauseshopt -s
or"$1"
(I don't recall exactly --- first argument that should be given?) and**/*
isn't very simple for people quite starting with Bash.
– JohnDoea
45 mins ago
add a comment |Â
up vote
0
down vote
Kind of.
One simple way to implement a callback in bash, is to accept the name of a program as a parameter, which acts as "callback function".
# This is script worker.sh accepts a callback in $1
cb="$1"
....
# Execute the call back, passing 3 parameters
$cb foo bar baz
This would be used like this:
# Invokes mycb.sh as a callback
worker.sh mycb.sh
Of course you don't have closures in bash. Hence, the callback function doesn't have access to the variables on the caller side. You can, however, store data the callback needs in environment variables. Passing information back from the callback to the invoker script is trickier. Data could be placed into a file.
If your design allows that everything is handled in a single process, you could use a shell function for the callback, and in this case the callback function has of course access to the variables on the invoker side.
add a comment |Â
up vote
-1
down vote
Only Perl-example available:
In the Perl example, we create a for_each
function that takes a reference to an array and an anonymous function. Next, we go through the array, and perform an anonymous function (closure), passing to it as a parameter the next element of the array.
When using the for_each function, we first define the $ serminal variable $ initialized by zero. Then we pass a reference to the array and a closure function to the for_each function, in which we add each element of the array to the $ sum variable. After executing the for_each function, $ sum will contain the sum of the array.
sub for_each
my($arr, $cb) = @_;
for my $item (@$arr)
$cb->($item);
my $sum = 0;
for_each [3, 4, 2, 21, 13, 666], sub
$sum += $_[0];
;
ptint $sum;
As the for_each
example shows, the callback function is the transfer of executable code as one of the parameters of another code. Often, the transmitted function works as a closure, i.e. has access to lexical variables and can be defined in other program code contexts and not available for direct calling from the parent function.
In fact, callback is an analog of function polymorphism, namely, it allows you to create functions of a more general purpose rather than creating a series of functions that are the same in structure but differ only in individual places with executable subtasks.
2
The fact that you don't give an example inbash
is worth mentioning and explaining... (Also, test run your code).
– Kusalananda
2 hours ago
@Kusalanandabash
is very complicated for me.) Sorry.
– misdeed
2 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
In typical imperative programming, you write sequences of instructions and they are executed one after the other, with explicit control flow:
touch file
if [ -f otherfile ]; then
cp otherfile otherfile2
fi
etc.
You can follow the execution flow quite easily, always working your way up from any given line of code to determine its execution context, knowing that any instructions you give will be executed as a result of their location in the flow (or their call sites’ locations, if you’re writing functions).
When you use callbacks, instead of placing the use of a set of instructions “geographicallyâ€Â, you describe when it should be called. Typical examples in other programming environments are cases such as “download this resource, and when the download is complete, call this callbackâ€Â. Bash doesn’t have a generic callback construct of this kind, but it does have callbacks, for error-handling and a few other situations; for example:
#!/bin/bash
cleanup()
echo This could delete temporary files etc
trap cleanup EXIT
echo Doing lots of stuff...
sleep 2
My code here never explicitly calls the cleanup
function; it tells Bash when to call it, using trap cleanup EXIT
, i.e. “dear Bash, please run the cleanup
command when you exit†(and cleanup
happens to be a function I defined earlier, but it could be anything Bash understands). Bash supports this for all non-fatal signals, exits, command failures, and general debugging (you can specify a callback which is run before every command).
You can use Bash’s ability to evaluate shell parameters as commands, to build a callback-oriented framework; that’s somewhat beyond the scope of this answer, and would perhaps cause more confusion by suggesting that passing functions around always involves callbacks. See Bash: pass a function as parameter for some examples of the underlying functionality.
add a comment |Â
up vote
3
down vote
In typical imperative programming, you write sequences of instructions and they are executed one after the other, with explicit control flow:
touch file
if [ -f otherfile ]; then
cp otherfile otherfile2
fi
etc.
You can follow the execution flow quite easily, always working your way up from any given line of code to determine its execution context, knowing that any instructions you give will be executed as a result of their location in the flow (or their call sites’ locations, if you’re writing functions).
When you use callbacks, instead of placing the use of a set of instructions “geographicallyâ€Â, you describe when it should be called. Typical examples in other programming environments are cases such as “download this resource, and when the download is complete, call this callbackâ€Â. Bash doesn’t have a generic callback construct of this kind, but it does have callbacks, for error-handling and a few other situations; for example:
#!/bin/bash
cleanup()
echo This could delete temporary files etc
trap cleanup EXIT
echo Doing lots of stuff...
sleep 2
My code here never explicitly calls the cleanup
function; it tells Bash when to call it, using trap cleanup EXIT
, i.e. “dear Bash, please run the cleanup
command when you exit†(and cleanup
happens to be a function I defined earlier, but it could be anything Bash understands). Bash supports this for all non-fatal signals, exits, command failures, and general debugging (you can specify a callback which is run before every command).
You can use Bash’s ability to evaluate shell parameters as commands, to build a callback-oriented framework; that’s somewhat beyond the scope of this answer, and would perhaps cause more confusion by suggesting that passing functions around always involves callbacks. See Bash: pass a function as parameter for some examples of the underlying functionality.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
In typical imperative programming, you write sequences of instructions and they are executed one after the other, with explicit control flow:
touch file
if [ -f otherfile ]; then
cp otherfile otherfile2
fi
etc.
You can follow the execution flow quite easily, always working your way up from any given line of code to determine its execution context, knowing that any instructions you give will be executed as a result of their location in the flow (or their call sites’ locations, if you’re writing functions).
When you use callbacks, instead of placing the use of a set of instructions “geographicallyâ€Â, you describe when it should be called. Typical examples in other programming environments are cases such as “download this resource, and when the download is complete, call this callbackâ€Â. Bash doesn’t have a generic callback construct of this kind, but it does have callbacks, for error-handling and a few other situations; for example:
#!/bin/bash
cleanup()
echo This could delete temporary files etc
trap cleanup EXIT
echo Doing lots of stuff...
sleep 2
My code here never explicitly calls the cleanup
function; it tells Bash when to call it, using trap cleanup EXIT
, i.e. “dear Bash, please run the cleanup
command when you exit†(and cleanup
happens to be a function I defined earlier, but it could be anything Bash understands). Bash supports this for all non-fatal signals, exits, command failures, and general debugging (you can specify a callback which is run before every command).
You can use Bash’s ability to evaluate shell parameters as commands, to build a callback-oriented framework; that’s somewhat beyond the scope of this answer, and would perhaps cause more confusion by suggesting that passing functions around always involves callbacks. See Bash: pass a function as parameter for some examples of the underlying functionality.
In typical imperative programming, you write sequences of instructions and they are executed one after the other, with explicit control flow:
touch file
if [ -f otherfile ]; then
cp otherfile otherfile2
fi
etc.
You can follow the execution flow quite easily, always working your way up from any given line of code to determine its execution context, knowing that any instructions you give will be executed as a result of their location in the flow (or their call sites’ locations, if you’re writing functions).
When you use callbacks, instead of placing the use of a set of instructions “geographicallyâ€Â, you describe when it should be called. Typical examples in other programming environments are cases such as “download this resource, and when the download is complete, call this callbackâ€Â. Bash doesn’t have a generic callback construct of this kind, but it does have callbacks, for error-handling and a few other situations; for example:
#!/bin/bash
cleanup()
echo This could delete temporary files etc
trap cleanup EXIT
echo Doing lots of stuff...
sleep 2
My code here never explicitly calls the cleanup
function; it tells Bash when to call it, using trap cleanup EXIT
, i.e. “dear Bash, please run the cleanup
command when you exit†(and cleanup
happens to be a function I defined earlier, but it could be anything Bash understands). Bash supports this for all non-fatal signals, exits, command failures, and general debugging (you can specify a callback which is run before every command).
You can use Bash’s ability to evaluate shell parameters as commands, to build a callback-oriented framework; that’s somewhat beyond the scope of this answer, and would perhaps cause more confusion by suggesting that passing functions around always involves callbacks. See Bash: pass a function as parameter for some examples of the underlying functionality.
answered 1 hour ago
Stephen Kitt
146k22319385
146k22319385
add a comment |Â
add a comment |Â
up vote
2
down vote
First it's important to note that what makes a function a callback function is how it's used, not what it does. A callback is when code that you write is called from code that you didn't write. You're asking the system to call you back when some particular event happens.
An example of a callback in shell programming is traps. A trap is a callback that isn't expressed as a function, but as a piece of code to evaluate. You're asking the shell to call your code when the shell receives a particular signal.
Another example of a callback is the -exec
action of the find
command. The job of the find
command is to traverse directories recursively and process each file in turn. By default, the processing is to print the file name (implicit -print
), but with -exec
the processing is to run a command that you specifies. This fits the definition of a callback, although at callbacks go, it is not very flexible since the callback runs in a separate process.
If you implemented a find-like function, you could make it use a callback function to call on each file. Here's an ultra-simplified example that takes a function name (or external command name) as argument and calls it on all regular files in the current directory and its subdirectories. The function is used as a callback which is called every time call_on_regular_files
finds a regular file.
shopt -s globstar
call_on_regular_files ()
declare callback="$1"
declare file
for file in **/*; do
if [[ -f $file ]]; then
"$callback" "$file"
fi
done
Callbacks aren't as common in shell programming as in some other environments because shells are primarily designed for simple programs. Callbacks are more common in environments where data and control flow are more likely to move back and forth between parts of the code that are written and distributed independently: the base system, various libraries, the application code.
+1, just a note: I think the "ultra-simplified" term isn't right, becauseshopt -s
or"$1"
(I don't recall exactly --- first argument that should be given?) and**/*
isn't very simple for people quite starting with Bash.
– JohnDoea
45 mins ago
add a comment |Â
up vote
2
down vote
First it's important to note that what makes a function a callback function is how it's used, not what it does. A callback is when code that you write is called from code that you didn't write. You're asking the system to call you back when some particular event happens.
An example of a callback in shell programming is traps. A trap is a callback that isn't expressed as a function, but as a piece of code to evaluate. You're asking the shell to call your code when the shell receives a particular signal.
Another example of a callback is the -exec
action of the find
command. The job of the find
command is to traverse directories recursively and process each file in turn. By default, the processing is to print the file name (implicit -print
), but with -exec
the processing is to run a command that you specifies. This fits the definition of a callback, although at callbacks go, it is not very flexible since the callback runs in a separate process.
If you implemented a find-like function, you could make it use a callback function to call on each file. Here's an ultra-simplified example that takes a function name (or external command name) as argument and calls it on all regular files in the current directory and its subdirectories. The function is used as a callback which is called every time call_on_regular_files
finds a regular file.
shopt -s globstar
call_on_regular_files ()
declare callback="$1"
declare file
for file in **/*; do
if [[ -f $file ]]; then
"$callback" "$file"
fi
done
Callbacks aren't as common in shell programming as in some other environments because shells are primarily designed for simple programs. Callbacks are more common in environments where data and control flow are more likely to move back and forth between parts of the code that are written and distributed independently: the base system, various libraries, the application code.
+1, just a note: I think the "ultra-simplified" term isn't right, becauseshopt -s
or"$1"
(I don't recall exactly --- first argument that should be given?) and**/*
isn't very simple for people quite starting with Bash.
– JohnDoea
45 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
First it's important to note that what makes a function a callback function is how it's used, not what it does. A callback is when code that you write is called from code that you didn't write. You're asking the system to call you back when some particular event happens.
An example of a callback in shell programming is traps. A trap is a callback that isn't expressed as a function, but as a piece of code to evaluate. You're asking the shell to call your code when the shell receives a particular signal.
Another example of a callback is the -exec
action of the find
command. The job of the find
command is to traverse directories recursively and process each file in turn. By default, the processing is to print the file name (implicit -print
), but with -exec
the processing is to run a command that you specifies. This fits the definition of a callback, although at callbacks go, it is not very flexible since the callback runs in a separate process.
If you implemented a find-like function, you could make it use a callback function to call on each file. Here's an ultra-simplified example that takes a function name (or external command name) as argument and calls it on all regular files in the current directory and its subdirectories. The function is used as a callback which is called every time call_on_regular_files
finds a regular file.
shopt -s globstar
call_on_regular_files ()
declare callback="$1"
declare file
for file in **/*; do
if [[ -f $file ]]; then
"$callback" "$file"
fi
done
Callbacks aren't as common in shell programming as in some other environments because shells are primarily designed for simple programs. Callbacks are more common in environments where data and control flow are more likely to move back and forth between parts of the code that are written and distributed independently: the base system, various libraries, the application code.
First it's important to note that what makes a function a callback function is how it's used, not what it does. A callback is when code that you write is called from code that you didn't write. You're asking the system to call you back when some particular event happens.
An example of a callback in shell programming is traps. A trap is a callback that isn't expressed as a function, but as a piece of code to evaluate. You're asking the shell to call your code when the shell receives a particular signal.
Another example of a callback is the -exec
action of the find
command. The job of the find
command is to traverse directories recursively and process each file in turn. By default, the processing is to print the file name (implicit -print
), but with -exec
the processing is to run a command that you specifies. This fits the definition of a callback, although at callbacks go, it is not very flexible since the callback runs in a separate process.
If you implemented a find-like function, you could make it use a callback function to call on each file. Here's an ultra-simplified example that takes a function name (or external command name) as argument and calls it on all regular files in the current directory and its subdirectories. The function is used as a callback which is called every time call_on_regular_files
finds a regular file.
shopt -s globstar
call_on_regular_files ()
declare callback="$1"
declare file
for file in **/*; do
if [[ -f $file ]]; then
"$callback" "$file"
fi
done
Callbacks aren't as common in shell programming as in some other environments because shells are primarily designed for simple programs. Callbacks are more common in environments where data and control flow are more likely to move back and forth between parts of the code that are written and distributed independently: the base system, various libraries, the application code.
answered 1 hour ago


Gilles
510k12010081537
510k12010081537
+1, just a note: I think the "ultra-simplified" term isn't right, becauseshopt -s
or"$1"
(I don't recall exactly --- first argument that should be given?) and**/*
isn't very simple for people quite starting with Bash.
– JohnDoea
45 mins ago
add a comment |Â
+1, just a note: I think the "ultra-simplified" term isn't right, becauseshopt -s
or"$1"
(I don't recall exactly --- first argument that should be given?) and**/*
isn't very simple for people quite starting with Bash.
– JohnDoea
45 mins ago
+1, just a note: I think the "ultra-simplified" term isn't right, because
shopt -s
or "$1"
(I don't recall exactly --- first argument that should be given?) and **/*
isn't very simple for people quite starting with Bash.– JohnDoea
45 mins ago
+1, just a note: I think the "ultra-simplified" term isn't right, because
shopt -s
or "$1"
(I don't recall exactly --- first argument that should be given?) and **/*
isn't very simple for people quite starting with Bash.– JohnDoea
45 mins ago
add a comment |Â
up vote
0
down vote
Kind of.
One simple way to implement a callback in bash, is to accept the name of a program as a parameter, which acts as "callback function".
# This is script worker.sh accepts a callback in $1
cb="$1"
....
# Execute the call back, passing 3 parameters
$cb foo bar baz
This would be used like this:
# Invokes mycb.sh as a callback
worker.sh mycb.sh
Of course you don't have closures in bash. Hence, the callback function doesn't have access to the variables on the caller side. You can, however, store data the callback needs in environment variables. Passing information back from the callback to the invoker script is trickier. Data could be placed into a file.
If your design allows that everything is handled in a single process, you could use a shell function for the callback, and in this case the callback function has of course access to the variables on the invoker side.
add a comment |Â
up vote
0
down vote
Kind of.
One simple way to implement a callback in bash, is to accept the name of a program as a parameter, which acts as "callback function".
# This is script worker.sh accepts a callback in $1
cb="$1"
....
# Execute the call back, passing 3 parameters
$cb foo bar baz
This would be used like this:
# Invokes mycb.sh as a callback
worker.sh mycb.sh
Of course you don't have closures in bash. Hence, the callback function doesn't have access to the variables on the caller side. You can, however, store data the callback needs in environment variables. Passing information back from the callback to the invoker script is trickier. Data could be placed into a file.
If your design allows that everything is handled in a single process, you could use a shell function for the callback, and in this case the callback function has of course access to the variables on the invoker side.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Kind of.
One simple way to implement a callback in bash, is to accept the name of a program as a parameter, which acts as "callback function".
# This is script worker.sh accepts a callback in $1
cb="$1"
....
# Execute the call back, passing 3 parameters
$cb foo bar baz
This would be used like this:
# Invokes mycb.sh as a callback
worker.sh mycb.sh
Of course you don't have closures in bash. Hence, the callback function doesn't have access to the variables on the caller side. You can, however, store data the callback needs in environment variables. Passing information back from the callback to the invoker script is trickier. Data could be placed into a file.
If your design allows that everything is handled in a single process, you could use a shell function for the callback, and in this case the callback function has of course access to the variables on the invoker side.
Kind of.
One simple way to implement a callback in bash, is to accept the name of a program as a parameter, which acts as "callback function".
# This is script worker.sh accepts a callback in $1
cb="$1"
....
# Execute the call back, passing 3 parameters
$cb foo bar baz
This would be used like this:
# Invokes mycb.sh as a callback
worker.sh mycb.sh
Of course you don't have closures in bash. Hence, the callback function doesn't have access to the variables on the caller side. You can, however, store data the callback needs in environment variables. Passing information back from the callback to the invoker script is trickier. Data could be placed into a file.
If your design allows that everything is handled in a single process, you could use a shell function for the callback, and in this case the callback function has of course access to the variables on the invoker side.
answered 23 mins ago
user1934428
36119
36119
add a comment |Â
add a comment |Â
up vote
-1
down vote
Only Perl-example available:
In the Perl example, we create a for_each
function that takes a reference to an array and an anonymous function. Next, we go through the array, and perform an anonymous function (closure), passing to it as a parameter the next element of the array.
When using the for_each function, we first define the $ serminal variable $ initialized by zero. Then we pass a reference to the array and a closure function to the for_each function, in which we add each element of the array to the $ sum variable. After executing the for_each function, $ sum will contain the sum of the array.
sub for_each
my($arr, $cb) = @_;
for my $item (@$arr)
$cb->($item);
my $sum = 0;
for_each [3, 4, 2, 21, 13, 666], sub
$sum += $_[0];
;
ptint $sum;
As the for_each
example shows, the callback function is the transfer of executable code as one of the parameters of another code. Often, the transmitted function works as a closure, i.e. has access to lexical variables and can be defined in other program code contexts and not available for direct calling from the parent function.
In fact, callback is an analog of function polymorphism, namely, it allows you to create functions of a more general purpose rather than creating a series of functions that are the same in structure but differ only in individual places with executable subtasks.
2
The fact that you don't give an example inbash
is worth mentioning and explaining... (Also, test run your code).
– Kusalananda
2 hours ago
@Kusalanandabash
is very complicated for me.) Sorry.
– misdeed
2 hours ago
add a comment |Â
up vote
-1
down vote
Only Perl-example available:
In the Perl example, we create a for_each
function that takes a reference to an array and an anonymous function. Next, we go through the array, and perform an anonymous function (closure), passing to it as a parameter the next element of the array.
When using the for_each function, we first define the $ serminal variable $ initialized by zero. Then we pass a reference to the array and a closure function to the for_each function, in which we add each element of the array to the $ sum variable. After executing the for_each function, $ sum will contain the sum of the array.
sub for_each
my($arr, $cb) = @_;
for my $item (@$arr)
$cb->($item);
my $sum = 0;
for_each [3, 4, 2, 21, 13, 666], sub
$sum += $_[0];
;
ptint $sum;
As the for_each
example shows, the callback function is the transfer of executable code as one of the parameters of another code. Often, the transmitted function works as a closure, i.e. has access to lexical variables and can be defined in other program code contexts and not available for direct calling from the parent function.
In fact, callback is an analog of function polymorphism, namely, it allows you to create functions of a more general purpose rather than creating a series of functions that are the same in structure but differ only in individual places with executable subtasks.
2
The fact that you don't give an example inbash
is worth mentioning and explaining... (Also, test run your code).
– Kusalananda
2 hours ago
@Kusalanandabash
is very complicated for me.) Sorry.
– misdeed
2 hours ago
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Only Perl-example available:
In the Perl example, we create a for_each
function that takes a reference to an array and an anonymous function. Next, we go through the array, and perform an anonymous function (closure), passing to it as a parameter the next element of the array.
When using the for_each function, we first define the $ serminal variable $ initialized by zero. Then we pass a reference to the array and a closure function to the for_each function, in which we add each element of the array to the $ sum variable. After executing the for_each function, $ sum will contain the sum of the array.
sub for_each
my($arr, $cb) = @_;
for my $item (@$arr)
$cb->($item);
my $sum = 0;
for_each [3, 4, 2, 21, 13, 666], sub
$sum += $_[0];
;
ptint $sum;
As the for_each
example shows, the callback function is the transfer of executable code as one of the parameters of another code. Often, the transmitted function works as a closure, i.e. has access to lexical variables and can be defined in other program code contexts and not available for direct calling from the parent function.
In fact, callback is an analog of function polymorphism, namely, it allows you to create functions of a more general purpose rather than creating a series of functions that are the same in structure but differ only in individual places with executable subtasks.
Only Perl-example available:
In the Perl example, we create a for_each
function that takes a reference to an array and an anonymous function. Next, we go through the array, and perform an anonymous function (closure), passing to it as a parameter the next element of the array.
When using the for_each function, we first define the $ serminal variable $ initialized by zero. Then we pass a reference to the array and a closure function to the for_each function, in which we add each element of the array to the $ sum variable. After executing the for_each function, $ sum will contain the sum of the array.
sub for_each
my($arr, $cb) = @_;
for my $item (@$arr)
$cb->($item);
my $sum = 0;
for_each [3, 4, 2, 21, 13, 666], sub
$sum += $_[0];
;
ptint $sum;
As the for_each
example shows, the callback function is the transfer of executable code as one of the parameters of another code. Often, the transmitted function works as a closure, i.e. has access to lexical variables and can be defined in other program code contexts and not available for direct calling from the parent function.
In fact, callback is an analog of function polymorphism, namely, it allows you to create functions of a more general purpose rather than creating a series of functions that are the same in structure but differ only in individual places with executable subtasks.
answered 3 hours ago


misdeed
20711
20711
2
The fact that you don't give an example inbash
is worth mentioning and explaining... (Also, test run your code).
– Kusalananda
2 hours ago
@Kusalanandabash
is very complicated for me.) Sorry.
– misdeed
2 hours ago
add a comment |Â
2
The fact that you don't give an example inbash
is worth mentioning and explaining... (Also, test run your code).
– Kusalananda
2 hours ago
@Kusalanandabash
is very complicated for me.) Sorry.
– misdeed
2 hours ago
2
2
The fact that you don't give an example in
bash
is worth mentioning and explaining... (Also, test run your code).– Kusalananda
2 hours ago
The fact that you don't give an example in
bash
is worth mentioning and explaining... (Also, test run your code).– Kusalananda
2 hours ago
@Kusalananda
bash
is very complicated for me.) Sorry.– misdeed
2 hours ago
@Kusalananda
bash
is very complicated for me.) Sorry.– misdeed
2 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470440%2fis-the-callback-concept-of-programming-existent-in-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
See stackoverflow.com/questions/5672289 for a similar question.
– dsstorefile1
2 hours ago