In Perl, how can I pass a variable as an argument to a subroutine?

Jason Husbands

I am passing a scalar variable to subroutine in which I want to test if the argument matches a regex:

use strict;
use warnings;

use Data::Dumper;

print "Please enter your first name followed by your last name. For example: John Smith.\n";

chomp(my $name = <STDIN>);

&analyze_name($name);

sub analyze_name {
    if ($_ =~ /^\w+\s+\w+$/) {
            print "You entered $_.";
                    } else {
            print "Incorrect convention";
    }
}

I am getting output with the following error

Please enter your first name followed by your last name. For example: John Smith.
firstname lastname
Incorrect convention
Use of uninitialized value $_ in pattern match (m//) at /home/jhusbands/scripts/examples/test.pl line 13, <STDIN> line 1.

My thinking is it would pass $name to the "magic variable" $_. I need to reuse this subroutine on multiple <STDIN> variables. How do I initialize $_?

simbabque

The argument to your subroutine is not in $_. It's in the array @_, and you need to get it out.

sub analyze_name {
    my $name = shift; # implicitly shifts @_
    if ($name =~ m/.../) { ... }
}

Or alternatively as a list assignment

sub analyze_name {
    my ($name) = @_;
    ....
}

Or, if you wanted to operate on the @_ array directly

sub analyze_name {
    if ($_[0] =~ m/.../) { ... }
}

Now we are accessing the first element in the array @_. The sign in front of the variable name (called a sigil) in Perl changes with the type of value that that expression returns. So for the array it's @_, but when you access an element, it becomes $_[0] because the value inside is scalar, which means it is only one value. This is different from $_, which is a scalar variable. The same thing is true about @array, $array[4] and the scalar variable $array (which is a badly chosen name).

In most cases you want to do either the shift or the list assignment. If you have two or three arguments, the list is often easier to read. If there are a lot of args, having them one per line makes sense. Operating on @_ directly usually only makes sense if speed is an issue because your sub is called millions of times in your program. My advice is to take readable code over brevity and go with properly named arguments.


The $name in the above code is lexical to that subroutine. That means it exists only there, and every time you call that sub, you get a new $name. The variable name of the variable you pass in when you call it doesn't matter.

analyze_name($foo);
analyze_name($bar);
analyze_name('foobar');

All of those pass a value to the sub. The sub does not care about the variable name. In the sub, it's always $name. Outside of the sub, $name does not exist.


Note that you should not call subs with & in Perl. That used to be the syntax in Perl 4 about 20 years ago. In Perl 5 this &foo() has a special meaning that you probably do not want. Drop the &, and look for subroutine prototypes if you want to know more about that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to pass a variable to a subroutine to send email in perl

From Dev

How can I pass argument stored on a variable to WGET

From Dev

How can I pass argument stored on a variable to WGET

From Dev

How to get the content of an argument to a Perl subroutine

From Dev

How to pass file names to a subroutine in perl?

From Dev

Pass regex into perl subroutine

From Dev

How do I pass a variable argument to instanceof?

From Java

How can I pass an argument to a PowerShell script?

From Dev

How can I pass argument with requestAnimationFrame?

From Dev

How can I pass argument with parameter to gulp?

From Dev

How to pass a variable as an argument

From Dev

How can I pass a quoted string as a single argument via bash variable

From Dev

How can I create a Perl subroutine that accepts more than one block?

From Java

can a variable be defined in perl whose value cannot be changed in a subroutine?

From Dev

How can I get perl to correctly pass a command line argument with multiple arguments and complex file paths (spaces and symbols)?

From Dev

Can I pass variable from Perl to PHP and backwards?

From Dev

How can I print to a variable in Perl?

From Dev

Why I can't do "shift subroutine_name()" in Perl?

From Dev

Variable change inside Perl subroutine

From Dev

Perl special variable "@_" in a subroutine not working

From Dev

How can I allow lexical variable declarations in my subroutine arguments the way opendir does?

From Dev

how can i pass variable in parameter as a function?

From Dev

How can I pass a variable to spreadsheet function?

From Dev

How can I pass a variable to an interpreted file?

From Dev

How do I pass $SHELL variable into a perl search and replace

From Dev

How can I pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

From Dev

Perl, How do I create a map/grep like subroutine?

From Dev

How to create a unit test for a simple I/O subroutine in Perl

From Dev

perl send internal array as argument to subroutine

Related Related

  1. 1

    how to pass a variable to a subroutine to send email in perl

  2. 2

    How can I pass argument stored on a variable to WGET

  3. 3

    How can I pass argument stored on a variable to WGET

  4. 4

    How to get the content of an argument to a Perl subroutine

  5. 5

    How to pass file names to a subroutine in perl?

  6. 6

    Pass regex into perl subroutine

  7. 7

    How do I pass a variable argument to instanceof?

  8. 8

    How can I pass an argument to a PowerShell script?

  9. 9

    How can I pass argument with requestAnimationFrame?

  10. 10

    How can I pass argument with parameter to gulp?

  11. 11

    How to pass a variable as an argument

  12. 12

    How can I pass a quoted string as a single argument via bash variable

  13. 13

    How can I create a Perl subroutine that accepts more than one block?

  14. 14

    can a variable be defined in perl whose value cannot be changed in a subroutine?

  15. 15

    How can I get perl to correctly pass a command line argument with multiple arguments and complex file paths (spaces and symbols)?

  16. 16

    Can I pass variable from Perl to PHP and backwards?

  17. 17

    How can I print to a variable in Perl?

  18. 18

    Why I can't do "shift subroutine_name()" in Perl?

  19. 19

    Variable change inside Perl subroutine

  20. 20

    Perl special variable "@_" in a subroutine not working

  21. 21

    How can I allow lexical variable declarations in my subroutine arguments the way opendir does?

  22. 22

    how can i pass variable in parameter as a function?

  23. 23

    How can I pass a variable to spreadsheet function?

  24. 24

    How can I pass a variable to an interpreted file?

  25. 25

    How do I pass $SHELL variable into a perl search and replace

  26. 26

    How can I pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

  27. 27

    Perl, How do I create a map/grep like subroutine?

  28. 28

    How to create a unit test for a simple I/O subroutine in Perl

  29. 29

    perl send internal array as argument to subroutine

HotTag

Archive