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
9
votes
1 answer

Does Perl v5.18's sort understand lexical subroutines?

This is fixed in Perl v5.22. Does Perl v5.18's lexical subroutines with sort? I finally had a use for them today where I had a complicated sorting routine that depends on the current position in the data structure to look at deeper parts. Here's a…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
9
votes
3 answers

Is using labels in Perl subroutines considered a bad practice?

I find that using labels inside Perl subroutines, to break from multiple loops, or to redo some parts with updated variables, very helpful. How is this coding style seen by the community? Is using labels inside subroutines frowned upon?
Geo
  • 93,257
  • 117
  • 344
  • 520
8
votes
2 answers

How should I pass objects to subroutines?

Is one of these the best or the worst approach? utilize the scope: my $cache = CHI->new( driver => 'File', expires_in => 3600 ); sub one { if ( my $data = $cache->get( 'key_one' ) ) { # ... } sub two { if ( my $data = $cache->get(…
sid_com
  • 24,137
  • 26
  • 96
  • 187
8
votes
1 answer

Why am I not getting a warning from Perl?

Consider these two use cases: sub test1 { my $v = 1; sub test2 { print $v } # ... } and for (0..3) { my $foo = $_; sub test1 { print $foo } # ... } The first one produces a Variable will not stay shared warning, while the…
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
8
votes
2 answers

perl6 - assignment to parameter

Is there a way to do an assignment to a formal parameter? Something like: sub myfunc($n) { $n = $n + 5; return $n; } Or, would I have to create a new variable and assign the value of $n to it?
user6189164
  • 667
  • 3
  • 7
8
votes
1 answer

Why subroutine needs to be written after the declaration of variables used in it?

Let's assume we have this code, why it fails with the explicit package name error since the function is called only after the declaration of the $value? use strict; use warnings; sub print_value{ print "\n$value"; } my $value =…
John Doe
  • 1,058
  • 8
  • 30
8
votes
1 answer

why the syntax `&name arg1 arg2 ...` can't be used to call a Perl subroutine?

for a Perl subroutine, if passing 0 argument, I can use 4 forms to call it. But if passing 1 or more arguments, there is one form that I can't use, please see below: sub name { print "hello\n"; } # 4 forms to…
password636
  • 981
  • 1
  • 7
  • 18
8
votes
5 answers

Ending an iteration of a loop from a subroutine

I have a program with a while loop which has several points where certain conditions require some action to be taken and then the remainder of the iteration to be skipped. As this is always going to be the same code, I wanted to put it in a…
CarrieVS
  • 140
  • 1
  • 7
8
votes
2 answers

How to use my own subroutines (globally) in a Mojolicious::Lite Application

I need to be able to write and call my own subroutines in a Mojolicious::Lite Application. However, the intuitive way to do this doesn't seem to be working. I emailed a colleague who has more Mojolicious experience than I do with this question and…
kingsfoil
  • 3,795
  • 7
  • 32
  • 56
8
votes
2 answers

Is there anything wrong with passing an unallocated array to a routine without an explicit interface?

Consider: program main real, allocatable, dimension(:) :: foo integer n n=10 call dofoo(foo,n,1) allocate(foo(n)) call dofoo(foo,n,0) end program main subroutine dofoo(foo,n,mode) real foo(n) integer i,n,mode if(mode.eq.1)then n=6 …
mgilson
  • 300,191
  • 65
  • 633
  • 696
8
votes
5 answers

Dynamically Create AutoHotkey Hotkey to Function/Subroutine

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,…
Synetech
  • 9,643
  • 9
  • 64
  • 96
8
votes
3 answers

Using a sorting subroutine from another package

I have a script and a package like so: # file: sortscript.pl use strict; use warnings; use SortPackage; my @arrays = ([1,"array1"],[10,"array3"],[4,"array2"]); print "Using sort outside package\n"; foreach (sort SortPackage::simplesort @arrays){ …
MattLBeck
  • 5,701
  • 7
  • 40
  • 56
7
votes
2 answers

Function call instead of self messaging — When to use what?

In Objective-C, when I want to call a subroutine, I send a message to an object, like: [self mySubroutine:myParameter]; There is a (negligible?) performance penalty, so I could just use a C-style function call: mySubroutine(myParameter); The…
Joe Völker
  • 781
  • 1
  • 5
  • 19
7
votes
3 answers

Can I capture the returned value of a routine used in RUN-MAIN?

I want a script to run a subroutine exported from a module, with the exported sub to be run as MAIN in the script. The subroutine does all that I want, except that it returns the result instead of printing it. RUN-MAIN seems to achieve most of what…
Daniel Mita
  • 512
  • 2
  • 9
7
votes
4 answers

Perl: How do you dereference an array without creating a copy of the array?

When I dereference an array using @$arrayRef or @{$arrayRef} it appears to create a copy of the array. Is there a correct way to dereference an array? This code... sub updateArray1 { my $aRef = shift; my @a = @$aRef; my…
DemiImp
  • 968
  • 8
  • 18