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
?
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.
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.
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.
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.
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.
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'
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With