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


In reply to Re^3: how to move multiple files by kcott
in thread how to move multiple files by ObiPanda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.