subroutine

Getting a subroutine to return three seperate arrays of random numbers in C

I currently have code for a subroutine to return a pointer to an array. This array is a list of random numbers for a one dimensional monte-carlo integral. I am now trying to do a multi dimensional equivalent which requires 3 arrays of random numbers and instead of having a separate subroutine for each I'm trying to make one which retur...

Perl - Subroutine redefined

I have asked this question before or searched and seen others ask - why am I getting the warning "Subroutine mySub redefined at ../lib/Common.pm line x"? and you always get the answer you declared the sub twice in the same code. I created this test package: ENTIRE FILE --------------- package Common; use strict; sub thisSubroutineIs...

How can I tell which subroutine I'm in?

Is there a way to get the name of the enclosing subroutine of a piece of perl code? For example: sub foo { print where_am_i(); } will output 'foo'. ...

Does Fortran preserve the value of internal variables through function and subroutine calls?

After much painful debugging, I believe I've found a unique property of Fortran that I'd like to verify here at stackoverflow. What I've been noticing is that, at the very least, the value of internal logical variables are preserved across function or subroutine calls. Here is some example code to illustrate my point: PROGRAM function...

Default argument values in subroutines

I have been working with perl for about two months now; it just occurred to me that I don't know how to set default arguments for subroutines. Here is what I considered: sub hello { print @_ || "Hello world"; } And that works fine for if all you needed was one argument. How would you set default values for multiple arguments? I was ...

Subroutines vs scripts in Perl

I'm fairly new to Perl and was wondering what the best practices regarding subroutines are with Perl. Can a subroutine be too big? I'm working on a script right now, and it might need to call another script. Should I just integrate the old script into the new one in the form of a subroutine? I need to pass one argument to the script and...

How to handle subroutine redefined errors in Perl

So I have a file that in short has this problem... #!/usr/bin/perl -w package Foo; use strict; use POSIX; ... sub remove { ... } ... and I get a get an error saying the subroutine remove has been redefined. I know the problem, there is a subroutine called remove in POSIX. However, I don't know how to handle it. How is this proble...

How to use threads to replace looping a subroutine in perl/pdl

I have a perfectly good perl subroutine written as part of a perl module. Without going into too many details, it takes a string and a short list as arguments (often taken from terminal) and spits out a value (right now, always a floating point, but this may not always be the case.) Right now, the list portion of my argument takes two v...

In Perl, how can I check from which module a given function was imported?

I have a code which calls the function. But I don't know the module this function belongs to. I need it to modify this function. How can I check it? ...

Procedure too large in vba

Getting an error meassage: Procedure too large in vba? what is the reason and way out? ...

When would it be a good idea to supply an anonymous sub as a command-line argument?

I'm toying with the idea of modifying my home-made test suite to supply it anonymous subroutines as command-line arguments. The reason why I want to do this is to enhance the script's flexibility. It doesn't seem worth it to hard-code a subroutine that I am only going to use once or twice to assimilate the data in a particular manner. ...

Should I use nested subroutines in Perl?

I have 5 Perl files that are verification scripts for 5 different states of my environment. Each of them has at least a couple of subroutines. Till now, the number of states was limited to 5 and these worked fine. But now, I have like 20 more states of the environment and hence 20 more Perl scripts according to the current design. I wa...

How can I call a subroutine whose name is a value in a hash, in Perl?

$ cat test.pl use strict; use warnings; sub route { print "hello, world!"; } my %h; $h{'a'} = 'route'; print "1\n"; $h{a}; print "2\n"; $h{a}(); print "3\n"; "$h{a}".(); $ perl test.pl Useless use of hash element in void context at test.pl line 12. Useless use of concatenation (.) or string in void context at test.pl line 18. 1 ...

How can I open a file only if it is not already open, in Perl?

If I have a subroutine that opens a file what is the best way to ensure it opens it only upon the first time the subrountine is called? I have this but not sure if its best practice: { my $count = 0; sub log_msg { my ($msg,$name) = @_; if ($count == 0) { my $log_file_name = "/tmp/" . $name; open my $log_fh,">",...

Why is my Perl loop off by one at the end?

I have this program which does not work as expected. Help me. I want to print a row heading. If input is 4, I want 1|2|3|4 to be output. It does not work as all, if I hard-code $count value it works partially but the last number is missing. sub printC { my $count = @_; # count = 4 # works partially only prints 1|2|3 ...

What is the CORE:match (opcode) subroutine in Perl profiling?

I previously wrote some utilities in Perl, and I am now rewriting them in order to give some new/better features. However, things seem to be going much more slowly than in the original utilities, so I decided to run one with the NYTProf profiler. Great profiler btw, still trying to figure out all its useful features. So anyway, it turns...

What does () accomplish in a subroutine definition in Perl?

The following code is lifted directly from the source of the Tie::File module. What do the empty parentheses accomplish in the definition of O_ACCMODE in this context? I know what subroutine prototypes are used for, but this usage doesn't seem to relate to that. use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX', 'LOCK_SH', 'O_WRONLY', 'O_RDONL...

Why does invoking print in a subroutine append 1 to the string?

I have created the following subroutine gender to randomly print string MALE or FEMALE. When subroutine is invoked, the print command suffixes a "1" at the end of the string. See the sample code and output below: sub gender { if ( (int rand(100)) >50) { print "MALE "; } else { print "FEMALE"; } } fo...