Questions tagged [subroutine]

A subroutine (e.g. procedure or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming languages includes support for creating self contained subroutines, and for calling and returning from them. They are in many ways similar to functions, but usually have side-effects outside of the simple "return value" that functions return.

1255 questions
23
votes
4 answers

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

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); …
Ivan Salazar
  • 296
  • 2
  • 12
23
votes
4 answers

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?
Nikita
  • 837
  • 1
  • 12
  • 23
22
votes
9 answers

How can I export all subs in a Perl package?

I would like to expose all subs into my namespace without having to list them one at a time: @EXPORT = qw( firstsub secondsub third sub etc ); Using fully qualified names would require bunch of change to existing code so I'd rather not do that. Is…
Ville M
  • 2,009
  • 7
  • 30
  • 45
21
votes
3 answers

How to pass optional parameters to a Perl function?

I want to pass several parameters, one of which is optional, to a function. The only way to do it that I know is using a list (@) as a parameter. Thus, it contents nothing or 1 element (will never be undef), so that I can use the following code: sub…
evgeny9
  • 1,381
  • 3
  • 17
  • 31
21
votes
1 answer

How do you access function parameters in Perl?

In C++ I would do something like this: void some_func(const char *str, ...); some_func("hi %s u r %d", "n00b", 420); In PHP I would do like this: function some_func() { $args = func_get_args(); } some_func($holy, $moly, $guacomole); How do I…
JOSHUA
  • 223
  • 1
  • 2
  • 4
19
votes
7 answers

Why would I use Perl anonymous subroutines instead of a named one?

I'm just curious why one would choose to use an anonymous subroutine, versus a named one, in Perl. Thanks.
user102881
  • 191
  • 1
  • 1
  • 3
19
votes
4 answers

What are the uses of lvalue subroutines in Perl?

I don't understand what could be the uses of lvalue subroutines? What is it that I can't accomplish with normal subroutines? Could you please post some examples? Thanks
Geo
  • 93,257
  • 117
  • 344
  • 520
18
votes
3 answers

How can I smoke out undefined subroutines?

I want to scan a code base to identify all instances of undefined subroutines that are not presently reachable. As an example: use strict; use warnings; my $flag = 0; if ( $flag ) { undefined_sub(); } Observations When $flag evaluates to true,…
Zaid
  • 36,680
  • 16
  • 86
  • 155
18
votes
1 answer

What are the differences between functions and subroutines in Fortran?

I was under the impression that the main differences between subroutines and functions in Fortran was that functions returned values, while subroutines change some or all of the values passed as arguments. But then I learned that you could modify…
naught101
  • 18,687
  • 19
  • 90
  • 138
18
votes
4 answers

What is the reason to use parenthesis-less subroutine calls in Perl?

As per perldoc perlsub: The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared. I notice that a lot of times, people use the fact that you can omit parenthesis when calling Perl subroutines (e.g., randomly…
DVK
  • 126,886
  • 32
  • 213
  • 327
18
votes
4 answers

Nested subroutines and Scoping in Perl

I'm writing Perl for quite some time now and always discovering new things, and I just ran into something interesting that I don't have the explanation to it, nor found it over the web. sub a { sub b { print "In B\n"; } } b(); how come I…
snoofkin
  • 8,725
  • 14
  • 49
  • 86
17
votes
6 answers

Why am I getting "called too early to check prototype" warnings in my Perl code?

I have a Perl file like this: use strict; f1(); sub f3() { f2(); } sub f1() {} sub f2() {} In short, f1 is called before it is defined. So, Perl throws a warning: "f1 called too early to check prototype". But same is the case with f2, the only…
user65457
  • 237
  • 1
  • 3
  • 10
17
votes
7 answers

in perl, is it bad practice to call multiple subroutines with default arguments?

I am learning perl and understand that it is a common and accepted practice to unpack subroutine arguments using shift. I also understand that it is common and acceptable practice to omit function arguments to use the default @_ array. Considering…
Mythoranium
  • 195
  • 1
  • 7
15
votes
4 answers

Printing out the code of an anonymous subroutine

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…
Andrei Krotkov
  • 5,556
  • 3
  • 33
  • 36
15
votes
9 answers

Is there a difference between Perl's shift versus assignment from @_ for subroutine parameters?

Let us ignore for a moment Damian Conway's best practice of no more than three positional parameters for any given subroutine. Is there any difference between the two examples below in regards to performance or functionality? Using shift: sub…
cowgod
  • 8,626
  • 5
  • 40
  • 57
1
2
3
83 84