in reply to Re: how to move multiple files
in thread how to move multiple files
Thank you to both posters.
I was able to get a test case of code working based on my script, with the help of this code. (I'm not a professional coder.)
I think it's solved.
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?
btw. The single quotes were because the files and file paths at times include spaces.my ($src, $dest) = @_;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: how to move multiple files
by kcott (Archbishop) on Sep 08, 2023 at 22:24 UTC | |
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).
Here's a run of that code:
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.
And here's a run of that code:
— Ken | [reply] [d/l] [select] |
by ObiPanda (Acolyte) on Sep 09, 2023 at 00:55 UTC | |
| [reply] |
by Bod (Parson) on Sep 08, 2023 at 22:37 UTC | |
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. | [reply] [d/l] |
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:
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). | [reply] [d/l] [select] |
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: [Those last three points are mainly for the OP's benefit.]
The output's getting lengthy but here's a sample run:
— Ken | [reply] [d/l] [select] |
Re^3: how to move multiple files
by AnomalousMonk (Archbishop) on Sep 08, 2023 at 22:23 UTC | |
... what is the purpose of the array in the first line of mv_tmp_to_phone?
Taking the liberty of answering for kcott... Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
Re^3: how to move multiple files
by kcott (Archbishop) on Sep 08, 2023 at 23:38 UTC | |
"btw. The single quotes were because the files and file paths at times include spaces." Take a look at my later response to Bod. It contains a number of additional points which may be relevant to you. — Ken | [reply] |