subroutine

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 do_something_fantastical { my $foo = shift; my $bar = shift; my $baz = shif...

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 ...

What's the best way to discover all subroutines a Perl module has?

What's the best way to programatically discover all of the subroutines a perl module has? This could be a module, a class (no @EXPORT), or anything in-between. Edit: All of the methods below look like they will work. I'd probably use the Class::Sniff or Class::Inspector in production. However, Leon's answer is marked as 'accepted' ...

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 there @EXPORT_ALL? I think documentation says it's a bad idea, but I'd like to do it a...

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. ...

Syntax Question: "Exit Sub" or "Return" in VB.Net Sub Routines

Both seem to accomplish the same thing--exit a subroutine. Is there any difference in how they work under the covers? I.e. Private Sub exitNow() Exit Sub End Sub or Private Sub exitNow() Return End Sub ...

How can I validate enum types as Perl subroutine arguments?

Building off Does Perl have an enumeration type?, how can I perform dynamic type checking (or static type checking if use strict is able to do so) that my subroutine argument is getting the right type of enum? package Phone::Type; use constant { HOME => 'Home', WORK => 'Work', }; package main; sub fun { my ($my_phone_type...

How can I link a subroutine to Autodyn?

I want to model reinforced concrete structures in Autodyn v6.1 under blast loading. So I am writting an user subroutine to model concrete with modified properties but I cann't link writed subroutine to Autodyn. So I am trying to find the solution or find some references/websites that can help me. ...

How can I use hashes as arguments to subroutines in Perl?

I was asked to modify some existing code to add some additional functionality. I have searched on Google and cannot seem to find the answer. I have something to this effect... %first_hash = gen_first_hash(); %second_hash = gen_second_hash(); do_stuff_with_hashes(%first_hash, %second_hash); sub do_stuff_with_hashes { my %first_hash ...

Installers with shared subinstallers?

We build a bunch of products that all have a similar structure, and in fact share installed subdirectories, documentation and various system configuration variables (environment variables, start menu items, ...) We've been using InstallShield. It works, but it is incredibly painful to configure a product installer one mouse click at a ...

When should I use the & to call a Perl subroutine?

I have heard that people shouldn't be using '&' to call Perl subs, i.e: function($a,$b,...); # opposed to &function($a,$b,...); I know for one the argument list becomes optional, but what are some cases where it is appropriate to use the '&' and the cases where you should absolutely not be using it? Also how does the performace incre...

How can I ask the user to re-enter input when they enter invalid input, in Perl?

Hi, I've a Perl subroutine which asks input from User. I perform a check inside that subroutine itself whether the input entered is a valid input. If it's not, I want to call the subroutine again to let the user enter a valid input this time. My subroutine is as follows: sub some_routine { print "Enter a number to select (1...

How do I properly invoke a subroutine that takes 2 subroutine references?

Imagine this subroutine: sub test(&&) { my $cr1 = shift; my $cr2 = shift; $cr1->(); $cr2->(); } I know I can call it like: test(\&sub1,\&sub2), but how can I call it like: test { print 1 },{ print 2 }; If I say that the subroutine takes only one &, than sending a block will work. I don't know how to make it work wit...

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? ...

Windows Shell Script: Can't make string substitution working in subroutine...

Greetings dear Experts! Could you please advice me on how to cope with the problem: @echo off cls setlocal enabledelayedexpansion path=%CD%;%path% set NumberOfPages=553 rem set /A MaxFileIndex=%Counter% - 1 set MaxFileIndex=1 del Output.txt for /l %%i in (0,1,%MaxFileIndex%) do call :GenerateFileList %%i goto :eof :::::::::::::...

perl subroutine call

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 diff being that it is called from inside another subroutine. It doesn't throw a warning fo...

Perl - passing arguments to a subroutine as hash key-value pairs problem

I have to pass two references as arguments to a subroutine (buildRanges) as hash key-value pairs as show below Example: @array = (“0A0”, “005”, “001”, “004”, “0BC”, “004”, “002”, “001”); @ranges = (); $numRanges = buildRanges(VALUES => \@array, REF_RANGES=>\@ranges); My question is 1. is the syntax for the sub-routine call above is...

Why am I unable to load a Perl library when using the `do` function?

I'm new to Perl, and I'm updating an old Perl website. Every .pl file seems to have this line at the top: do "func.inc"; So I figured I could use this file to tag on a subroutine for global use. func.inc #!/usr/bin/perl sub foobar { return "Hello world"; } index.pl #!/usr/bin/perl do "func.inc"; print "Content-type: text/html...

Should I call Perl subroutines with no arguments as marine() or marine?

Hi, My sample code as below, there are two styles subname and subnam() #!C:\Perl\bin\perl.exe use strict; use warnings; use 5.010; &marine(); # style 1 &marine; # style 2 sub marine { state $n = 0; # private, persistent variable $n $n += 1; print "Hello, sailor number $n!\n"; } Which one(&marine(); and &marine;) is more...

How can I pass parameters to Perl subroutines defined using eval?

I'm using a config file (in YAML) to define types that are used later on to validate other config values required for my app: --- action: > use List::MoreUtils; my $value = $_; any { $value eq $_ } qw(fatal keep merge non-fatal replace); dir : return defined $_ ? -d $_ : -1; file : return defined $_ ? -f $_ : ...