in reply to Re^3: how to move multiple files
in thread how to move multiple files

However, I imagine you're asking that because...

I'm curious why you used a sub given that it's a script, not a module, and it only gets called once.

Replies are listed 'Best First'.
Re^5: how to move multiple files
by eyepopslikeamosquito (Archbishop) on Sep 09, 2023 at 00:36 UTC

    I'm curious why you used a sub given that it's a script, not a module, and it only gets called once

    Bod, further to kcott's excellent reply (especially "a subroutine abstracts functionality and promotes reuseability; it has nothing to do with script vs. module" ++) a few more thoughts on the tricky topic of how best to test end user scripts.

    My preferred approach is to abstract the work the script does into CPAN-like modules and unit test each module using Test::More and the prove command. Accordingly, I strive to keep my script mainlines as short as is practicable. There are many examples of this approach on the CPAN; the perltidy command, for example, is effectively a one-liner:

    exit Perl::Tidy::perltidy( argv => $arg_string );

    Though I'm not a fan, an alternative approach, concocted by brian_d_foy (who has made 93 fewer posts than you ;-), is modulinos (how a script becomes a module).

Re^5: how to move multiple files
by kcott (Archbishop) on Sep 08, 2023 at 23:25 UTC

    G'day Bod,

    "I'm curious why you used a sub given that it's a script, not a module, and it only gets called once."

    My example code only called it once for demonstration purposes. A subroutine abstracts functionality and promotes reuseability; it has nothing to do with script vs. module. The OP's production code may want to use it multiple times.

    Here's move_to_phone_4.pl which, after adding some files for my explanation to you, shows:

    • Multiple calls to mv_tmp_to_phone().
    • Variable and hard-coded parameters.
    • Handling of filenames containing spaces.
    • The fact that glob("$src/*") works and does not require glob("'$src/*'").
    • An absence of files in the source directory is not a problem.

    [Those last three points are mainly for the OP's benefit.]

    #!/usr/bin/env perl use v5.36; use File::Copy 'move'; my ($source, $destination) = qw{Temp Phone}; mv_tmp_to_phone($source, $destination); mv_tmp_to_phone(qw{For_Bod Phone}); sub mv_tmp_to_phone ($src, $dest) { system ls => '-lR'; my @files = glob("$src/*"); say for @files; move($_, $dest) for @files; system ls => '-lR'; }

    The output's getting lengthy but here's a sample run:

    ken@titan ~/tmp/pm_11154302_move_files $ ./move_to_phone_4.pl .: total 4 drwxr-xr-x 1 ken None 0 Sep 9 08:42 For_Bod -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 -rwxr-xr-x 1 ken None 356 Sep 9 09:02 move_to_phone_4.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 ./For_Bod: total 0 -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 3' -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 4' ./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 .: total 4 drwxr-xr-x 1 ken None 0 Sep 9 08:42 For_Bod -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 -rwxr-xr-x 1 ken None 356 Sep 9 09:02 move_to_phone_4.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 ./For_Bod: total 0 -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 3' -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 4' ./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 .: total 4 drwxr-xr-x 1 ken None 0 Sep 9 08:42 For_Bod -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 -rwxr-xr-x 1 ken None 356 Sep 9 09:02 move_to_phone_4.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 ./For_Bod: total 0 -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 3' -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 4' ./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 For_Bod/file 3 For_Bod/file 4 .: total 4 drwxr-xr-x 1 ken None 0 Sep 9 09:02 For_Bod -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 -rwxr-xr-x 1 ken None 356 Sep 9 09:02 move_to_phone_4.pl drwxr-xr-x 1 ken None 0 Sep 9 09:02 Phone drwxr-xr-x 1 ken None 0 Sep 9 08:15 Temp ./For_Bod: total 0 ./Phone: total 0 -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 3' -rw-r--r-- 1 ken None 0 Sep 9 08:42 'file 4' -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