Pass values to a subroutine in Perl @_

Home > Search
  by

The @_ character is a special build in command in Perl that lets you pass values into a subroutine. For example, let's say you have a subroutine named foo.

sub foo {
}

 

The @_ character can be used in the subroutine to get values. In this example, the $a variable will obtain the first value passed into the subroutine, and then the $a variable is printed.

sub foo {
  my ($a) = @_;
  print $a;
}

 

You will pass values into the subroutine by specifying the values when you call the subroutine. In this example, a value of "bar" is passed into the "foo" subroutine.

foo("bar");

 

Here is the full script.

#!/usr/bin/perl

sub foo {
  my ($a) = @_;
  print $a;
}

foo("bar");

 

When the script is run, the text "bar" will be printed to the console.

~]# perl example.pl
bar

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments