Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to write the perl script for the following linux command?

by davido (Cardinal)
on Mar 14, 2017 at 14:25 UTC ( [id://1184554]=note: print w/replies, xml ) Need Help??


in reply to How to write the perl script for the following linux command?

Are find and cpio unavailable to you? If you have something that works, but simply need to incorporate it into a larger script, you can use the qx{} operator or `backticks`, or system to execute the external calls.

While it's a good practice to invoke external calls as infrequently as practical, it's also a good practice to have a clear goal in refactoring, and to leave things that work as they are unless the clear goal necessitates the refactor.

Writing more code to re-implement the existing solution is just likely to introduce new bugs. On the other hand, if you've decided that the need to re-implement is justified for clearly defined reasons, by all means, File::Find is your first step.


Dave

Replies are listed 'Best First'.
Re^2: How to write the perl script for the following linux command?
by haukex (Archbishop) on Mar 14, 2017 at 18:35 UTC

    This is good advice overall!

    For the OP, I just wanted to add that there are "better" alternatives to system and backticks. In this particular case, if there are no variables being interpolated into the command string, meaning it's safe enough to call the shell, IPC::System::Simple's run would probably be easiest, since it's just a replacement for system with better error handling.

    use IPC::System::Simple qw/run/; run q{ find /path/to/files -name '*.csv' | cpio -pdm /target };

    Otherwise, e.g. if variables are being interpolated into the command, things can get more complicated - I wrote about the whole topic at length here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184554]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found