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...
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...
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'.
...
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...
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 ...
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...
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...
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...
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?
...
Getting an error meassage: Procedure too large in vba? what is the reason and way out?
...
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.
...
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...
$ 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
...
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,">",...
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
...
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...
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...
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...