subroutine

How to create a Perl subroutine that accepts a block of code

懵懂的女人 提交于 2019-12-03 05:03:01
问题 I have a set of subroutines that look like this: sub foo_1($) { my $name = shift; my $f; run_something(); open($f, $name) or die ("Couldn't open $name"); while (<$f>) { //Something for foo_1() } close($f); do_something_else(); } And I have like four or more that look the same, the only thing that changes is the body of the while block. I'd like to abstract this and stop copy-pasting the code around. Is there a way to code a subroutine that accepts a block of code and executes it? To give more

Basic Structure of a Haskell Program

一曲冷凌霜 提交于 2019-12-03 04:46:19
问题 Many of the Haskell tutorials I've looked through focus almost entirely on syntax with very little coverage on how to structure a program. For example... Here's a bare-bones outline of a C++ application: #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; } When I first started learning C++, examples like these helped me immensely in learning how to assemble

Dynamically Create AutoHotkey Hotkey to Function/Subroutine

若如初见. 提交于 2019-12-03 03:18:48
The AutoHotkey command Hotkey allows for the creation of dynamic hotkeys at runtime, but its syntax and documentation seems to limit it to built-in or existing labels/subroutines, which makes it much less useful: Hotkey, KeyName [, Label, Options] Is there a way to get it to work like regular, hard-coded hotkeys? For example: #z::MsgBox foobar ; Typical, hard-coded hotkey pops up a message-box Hotkey, z, MsgBox foobar ; Nope; complains about missing label “MsgBox foobar” It looks like it might be possible due to the following line from the manual, however it is not clear how it would work:

Assembly 'call' vs 'jmp'

寵の児 提交于 2019-12-03 02:04:21
I got told to try and use 'jmp rather than 'call', but 'jmp' is not liking me .. when I jump it doesn't return (so it never exits and not happy days ), but calling returns and exits as normal. I am happy using 'call' but is there actually a reason I should try and overcome 'jmp' ? This simple code just shows if when I jmp it never returns and exits. _start: jmp _Print jmp _Exit ret _Exit: ; normal exit ret _Print ; print something ret also .. I'm running this all in a Linux terminal if that changes anything. Well, first of all, jmp simply 'jumps' to the label that you give to it (which is a

Printing out the code of an anonymous subroutine

大憨熊 提交于 2019-12-03 02:00:36
I'm currently working in a very complex Perl architecture, and I want to create some debugging tools. Since a lot of the behavior involves anonymous subroutines, I'd like to analyze some of the behavior, and all I have to work with is a reference to the subroutine. In short, is there a way to print the code (since Perl is interpreted it may still be available?) of a subroutine reference? The core module B::Deparse provides this functionality. use B::Deparse (); my $deparse = B::Deparse->new; my $code = sub {print "hello, world!"}; print 'sub ', $deparse->coderef2text($code), "\n"; which prints

Perl special variable “@_” in a subroutine not working

杀马特。学长 韩版系。学妹 提交于 2019-12-02 19:49:19
问题 This script rips out the urls from a downloaded webpage. I had some trouble with this script - when I use the "my $csv_html_line = @_ ;" and then print out the "@html_LineArray" - it just prints out "1's" . When I replace the "my $csv_html_line = @_ ;" with "my $csv_html_line = shift ;" the script works fine. I do not know what the difference is betweeh the "= @_" and shift - becuase I thought that without specifying something, in a subroutine, shift shift froms "@_". #!/usr/bin/perl use

What does a void subroutine return?

眉间皱痕 提交于 2019-12-02 19:40:00
问题 I was just doing an assessment on pluralsight and was given the following question. "What does a void subroutine return?" I was under the impression that a void subroutine did not return anything, but this was not one of the answers provided (Multiple choice question). Does .Net return a value type in the background or is this question incorrect? The choices I was given were: Integer Boolean String Datetime 回答1: System.Void is not nothing , it's a struct hence a value type. However, a method

Basic Structure of a Haskell Program

风格不统一 提交于 2019-12-02 19:06:19
Many of the Haskell tutorials I've looked through focus almost entirely on syntax with very little coverage on how to structure a program. For example... Here's a bare-bones outline of a C++ application: #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; } When I first started learning C++, examples like these helped me immensely in learning how to assemble individual pieces into working programs. Maybe I'm looking in the wrong places, but I haven't been able to

How to create a Perl subroutine that accepts a block of code

我是研究僧i 提交于 2019-12-02 18:16:54
I have a set of subroutines that look like this: sub foo_1($) { my $name = shift; my $f; run_something(); open($f, $name) or die ("Couldn't open $name"); while (<$f>) { //Something for foo_1() } close($f); do_something_else(); } And I have like four or more that look the same, the only thing that changes is the body of the while block. I'd like to abstract this and stop copy-pasting the code around. Is there a way to code a subroutine that accepts a block of code and executes it? To give more context, the different foo subroutines are a different Finite State Machine (FSM) that read the

What does a void subroutine return?

ⅰ亾dé卋堺 提交于 2019-12-02 10:29:12
I was just doing an assessment on pluralsight and was given the following question. "What does a void subroutine return?" I was under the impression that a void subroutine did not return anything, but this was not one of the answers provided (Multiple choice question). Does .Net return a value type in the background or is this question incorrect? The choices I was given were: Integer Boolean String Datetime Tim Schmelter System.Void is not nothing , it's a struct hence a value type. However, a method that is a Sub (VB.NET) or "returns" void (C#) does not really return a value. So you cannot