in reply to Re^2: how to move multiple files
in thread how to move multiple files
If you don't mind Ken, can you very briefly tell me what is the purpose of the array in the first line of mv_tmp_to_phone?
my ($src, $dest) = @_;
The short (and simplistic) answer is that is how to capture the parameters sent to a subroutine.
However, I imagine you're asking that because $src and $dest already exist in the code and could be used directly in mv_tmp_to_phone().
Whilst that is true, it is a poor coding habit to get into because those variables in the subroutine would be reliant upon variables declared and used outside of the subroutine. This has the problems that you may have encountered with using global variables in other scripts.
The $src and $dest used in mv_tmp_to_phone() are lexically scoped to mv_tmp_to_phone() and are not reliant on what those variables are called elsewhere in the code. To demonstrate, here's another version (move_to_phone_2.pl) where I move those files back again but, outside of mv_tmp_to_phone(), they have different names (i.e. $s and $d).
#!/usr/bin/env perl use strict; use warnings; use File::Copy 'move'; my ($s, $d) = reverse qw{Temp Phone}; mv_tmp_to_phone($s, $d); sub mv_tmp_to_phone { my ($src, $dest) = @_; system ls => '-lR'; my @files = glob("$src/*"); print "$_\n" for @files; move($_, $dest) for @files; system ls => '-lR'; }
Here's a run of that code:
ken@titan ~/tmp/pm_11154302_move_files $ ./move_to_phone_2.pl .: total 2 -rwxr-xr-x 1 ken None 337 Sep 8 18:05 move_to_phone.pl -rwxr-xr-x 1 ken None 335 Sep 9 07:07 move_to_phone_2.pl drwxr-xr-x 1 ken None 0 Sep 8 18:05 Phone drwxr-xr-x 1 ken None 0 Sep 8 18:05 Temp ./Phone: total 0 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file1 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file2 ./Temp: total 0 Phone/file1 Phone/file2 .: total 2 -rwxr-xr-x 1 ken None 337 Sep 8 18:05 move_to_phone.pl -rwxr-xr-x 1 ken None 335 Sep 9 07:07 move_to_phone_2.pl drwxr-xr-x 1 ken None 0 Sep 9 07:07 Phone drwxr-xr-x 1 ken None 0 Sep 9 07:07 Temp ./Phone: total 0 ./Temp: total 0 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file1 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file2
For completeness on a discussion of passing variables to subroutines, a more modern and improved method is to use Signatures. These were introduced as an experimental feature in v5.20.0 (see "perl5200delta: Experimental Subroutine signatures") and became a stable feature in v5.36.0 (see "perl5360delta: Core Enhancements: use v5.36").
As a side note, I do not recommend the use of experimental features in production code as they can often change before becoming stable features. From that last link (with my emphasis added): "Introduced in Perl version 5.20.0, and modified several times since, the subroutine signatures feature is now no longer considered experimental."
The original version of the code I posted should work under any Perl5 version. Here's another version, move_to_phone_3.pl, for version v5.36.0 and later.
#!/usr/bin/env perl use v5.36; use File::Copy 'move'; my ($source, $destination) = qw{Temp Phone}; mv_tmp_to_phone($source, $destination); sub mv_tmp_to_phone ($src, $dest) { system ls => '-lR'; my @files = glob("$src/*"); say for @files; move($_, $dest) for @files; system ls => '-lR'; }
And here's a run of that code:
ken@titan ~/tmp/pm_11154302_move_files $ ./move_to_phone_3.pl .: total 3 -rwxr-xr-x 1 ken None 337 Sep 8 18:05 move_to_phone.pl -rwxr-xr-x 1 ken None 335 Sep 9 07:07 move_to_phone_2.pl -rwxr-xr-x 1 ken None 319 Sep 9 08:14 move_to_phone_3.pl drwxr-xr-x 1 ken None 0 Sep 9 07:07 Phone drwxr-xr-x 1 ken None 0 Sep 9 07:07 Temp ./Phone: total 0 ./Temp: total 0 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file1 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file2 Temp/file1 Temp/file2 .: total 3 -rwxr-xr-x 1 ken None 337 Sep 8 18:05 move_to_phone.pl -rwxr-xr-x 1 ken None 335 Sep 9 07:07 move_to_phone_2.pl -rwxr-xr-x 1 ken None 319 Sep 9 08:14 move_to_phone_3.pl drwxr-xr-x 1 ken None 0 Sep 9 08:15 Phone drwxr-xr-x 1 ken None 0 Sep 9 08:15 Temp ./Phone: total 0 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file1 -rw-r--r-- 1 ken None 0 Sep 8 17:50 file2 ./Temp: total 0
— Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: how to move multiple files
by ObiPanda (Acolyte) on Sep 09, 2023 at 00:55 UTC | |
Re^4: how to move multiple files
by Bod (Parson) on Sep 08, 2023 at 22:37 UTC | |
by eyepopslikeamosquito (Archbishop) on Sep 09, 2023 at 00:36 UTC | |
by kcott (Archbishop) on Sep 08, 2023 at 23:25 UTC |