Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to copy/cut and paste files without using modules ?

by palkia (Monk)
on Oct 20, 2011 at 04:21 UTC ( [id://932552]=perlquestion: print w/replies, xml ) Need Help??

palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone
I just learned I can use unlink to delete files. Surprisingly I couldn't find how to do other fundamental file actions,
such as copy cu and paste without using any modules.
I did found other function which got he work "link" in them,
but the description of these function in perldoc is very unclear (at least for me),
(example: "link - Creates a new filename linked to the old filename", is that means: a copy ? a shortcut ? my interpretation: a shortcut that the system consider a regular file ???)
and I wouldn't try to use this type of functions until I'm absolutely sure what they do,
especially after reading the description for "rename" that supposedly is a lot less predictable than expected.

Thx

In Addition:
As requested by Marshall I'll further specify my intentions.
1st of all I am familiar with he inner mechanics of file handling (I've done some Assembler a decade ago),
but I've never heard about any modern operating system (I'm a windows user), that would allow multiple references to he same file without calling all but one "shortcuts".
Although it is of course technically possible.

my (well, windows's really) definitions: (sorry for the over specifying..)
* Applying cut & paste on a file, is (as far as the common non-programmer user concerns) moving it from 1 directory to another, so it exists in the new directory, but no longer exists in the old one.
* Applying copy & paste on a file, is (as far as the common non-programmer user concerns), copying it from 1 directory to other(s), so it exists in all the new directories and the old 1.
In his case the files may be (at the time of copy) identical (except for their paths ^^), but they are completely independent of each other,
so 1 (or more) may be renamed/edited/deleted/anything without affecting the others in any way.
* Applying "delete" on a file, is (as far as the common non-programmer user concerns), removing the file from existence so it can not be accessed in any way ever again.
* a shortcut, is a file that only contains one thing: a single path to a non-shortcut file.
a shortcut is referring only to the file itself by path, and so it's use is completely dependent on the file it is targeting.
if the file targeted is edited, the change will be noticeable when accessing the file through it's shortcut just as it would be when accessing it directly.
if the target file is renamed/moved/deleted the shortcut would (when accessed) be unable to find it's file target and will become useless.

Thx again

Replies are listed 'Best First'.
Re: How to copy/cut and paste files without using modules ?
by afresh1 (Hermit) on Oct 20, 2011 at 04:51 UTC

    I'm not quite sure what you mean by copy, cut and paste, but based on the rest of your node, you likely want to use File::Copy which, although a module, is a core module so will exist in any complete install.

    l8rZ,
    --
    andrew
Re: How to copy/cut and paste files without using modules ?
by CountZero (Bishop) on Oct 20, 2011 at 14:06 UTC
    A question which contains something like "How can I do ABC without using XYZ" always makes me wonder why such apparently arbitrary restrictions are imposed?

    What is wrong with using a module? The very existence of CPAN with its unique wealth of modules extending the power of Perl is something which makes all other programming languages envious, so why should you not take advantage of it?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      The apparently arbitrary restrictions, are very far from being arbitrary.
      There's nothing wrong with using modules (I use many myself),
      but as a perl nub, an "I want X done" isn't my entire interest, but a more "how does it work" wondering,
      both for theoretical and practical purposes.

      Also the more fundamental the code is, the greater its implementation potential is,
      the less likely it is to surprise the user (by run/intent errors), and the easier it is to be customize to the exact desired purpose for which it is required.

      A less common reason to avoid modules (I faced) is when planning to convert the pl file to something else (like exe) once it's done,
      but your converter can't handle modules correctly.

      Generally when writing a long and complicated program, a module can be a huge time (& headache) saver,
      but whenever using a module can be avoided (with a reasonable cost), a diy is always my choice.
        Studying the source code of the modules is a great way of learning Perl.

        What "Perl-to-exe" converter are you talking about?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to copy/cut and paste files without using modules ?
by Marshall (Canon) on Oct 20, 2011 at 05:28 UTC
    I just learned I can use unlink to delete files
    I think you do not understand how a directory works.

    A common misperception is that a file "exists within a directory". No!

    A file is a sequence of bits.
    A directory entry is a pointer that relates some alphanumeric text to the start of that sequence of bits.

    There is a way to have different text values point to the same sequence of bits. Its just that simple.

    I am confused as to what you are asking about.

    "unlink X;" means literally to disconnect the pointer that the name X describes to a file.

    There could be another name 'Y' that points to the same file (which is a sequence of bits on hard disk), 'Y' or 'X' is essentially a starting number of the "nth bit" on the hard drive.

    So this does not mean "delete the file" on the disk.

    "Unlink" means "disconnect my text pointer to that sequence of bits on the disk".
    It is possible for another name to point to the same "file bits".
    The storage allocation for the "bits on the disk" will not be freed until there are no more pointers to it in the "directory"

    Anyway, please explain your question in more detail!

      A common misperception is that a file "exists within a directory". No!

      That's a whole lot of BS. Stop pretending that directory entries aren't called files.

      unlink: "Deletes a list of files"
      rm: "Remove files or directories"

      You're trying to correct the OP's terminology, yet he's using the same terminology as Perl and GNU.

        That's a whole lot of BS..... If you are going to say BS then I say nonsense!

        I would prefer that we do not use these terms like BS and nonsense. That is not a civilized discussion.

        A directory entry is a name which points to a "set of bits".
        There can be more than one directory entry that points to the same "set of bits".

        "unlink" means disconnect name "x" to a particular set of bits. In Perl lingo, perhaps you could think of it like deleting a hash key. There could be another hash key which points to the same "value".

        Oh, I'm trying to understand...
        Stop pretending that directory entries aren't called files.
        I said that directory entries are pointers to files.
        Unlink-ing a directory entry does not delete the file.
        It just deletes the pointer to the file.
        In this case, the "file" will be "gone" when all references to it are gone. This is the unix file system link count and similar Perl reference counting.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-03-29 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found