Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does assigning 'shift' to a variable mean?

Tags:

variables

perl

Example:

use strict;
my $file = shift;

open(IN, $file) || die "Unable to open $file\n";
open(OUT, ">$file.$$") or die $!;

What is going on with the my $file = shift?

like image 658
sean curran Avatar asked Aug 16 '11 18:08

sean curran


People also ask

What are shift variables?

Variable shifts – also called rotating shifts – are one way employers schedule employees to cover 24 hour a day, 7 days per week operations. Instead of working a traditional eight-hour day, or a four-hour day for part-time workers, employees work longer hours in a day, but fewer days per week.

What does shift means in Perl?

shift() function in Perl returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop.

What does @_ mean in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.

What is the use of shift @_ in Perl subroutine?

Description. This function returns the first value in an array, deleting it and shifting the elements of the array list to the left by one. If ARRAY is not specified, shifts the @_ array within a subroutine, or @ARGV otherwise.


3 Answers

If used in the main program, it will shift (remove and return) the first value from @ARGV, the argument list of your program. If used inside a subroutine, it will shift the first value from @_, the argument list of the sub. See the documentation for more info.

like image 94
TLP Avatar answered Sep 25 '22 15:09

TLP


You can read the documentation on shift. It is very clear. shift accepts an array and extracts the first element (actually removing it from the array). If you don't provide any array, it uses @ARGV (the arguments to the command line of the program) to read the data. If you're inside a function, it uses @_, namely, the parameters of the function.

Example:

my @array = (1, 2, 3); my $value = shift @array; print $value; # will print '1' 
like image 45
Diego Sevilla Avatar answered Sep 23 '22 15:09

Diego Sevilla


shift uses the @_ variable which contains the values passed to a function or shift uses @ARGV for the values passed to the script itself if shift is referenced outside of a function.

ONE WORD OF CAUTION: It is probably wise, especially in Windows where many paths contain spaces, to put the filename and path in single quotes when you pass it to the script or function. This will tell Perl that you are passing it a single scalar value rather than an array of values. You can use double quotes if you want to interpolate part of the name. For example:

my $path = 'C:\Goat Kids';
func("$path\\goat.txt");

Or, from the command line if you are passing the filename directly to the script:

perl goat.pl "C:\Goat Kids\goat.txt"
like image 24
Smartnor Avatar answered Sep 24 '22 15:09

Smartnor