Search code examples
perlhandle

What is a "handle" in Perl?


Wondering what the handle is in Perl.

I can see file handle, directory handle..etc but want to know the meaning of the handle in Perl.

For example, in IO::Pipe, I can see below explain. And want to make clear the meaning of "becomes a handle"?

reader ([ARGS])

The object is re-blessed into a sub-class of IO::Handle, 
and becomes a handle at the reading end of the pipe. If 
ARGS are given then fork is called and ARGS are passed 
to exec.

Also could you please explain the meaning of bless?


Solution

  • A handle is a way to get to something without actually being that thing. A handle has an interface to interact with something managed by the system (or something else).

    Start with the idea of a scalar. Defining a simple scalar stores a value and that value is actually in the memory of your program. In very simplistic terms, you manage that resource directly and wholly within your program. You don't need to ask the system to do increment the variable for you:

     my $n = 5; 
     $n++;
    

    Talking to the outside world

    A handle represents a connection to something managed by something else, typically through "system calls".

    A file handle is your connection to a file (so, managed by the filesystem or OS) but is not the file itself. With that filehandle, you can can read from or write to the file, there is code behind all that to talk to the system to do the actual work.

     open my $filehandle, '<', $filename or die "$!";
    

    Since you are not managing the actual work and since you depend on the system to do the work, you check the $! system error variable to check that the system was able to do what you wanted. If it couldn't, it tells you how it ran into a problem (although the error may not be very specific).

    A directory handle is a way to get a list of the things inside a directory, but is not the directory itself. To get that, you have to ask the system to do things for you. And so on.

    Perl is wonderful though

    But, in Perl, you can make a handle to anything you like (and I write a lot about this in either Effective Perl Programming and Mastering Perl. You can use the interface for a handle even if you wholly control the thing and don't need to ask the system to do something on your behalf.

    For example, you can use the filehandle interface on a string:

     open my $string_filehandle, '>', \my $string;
     print {$string_filehandle} "This goes to the string";
    

    To your code as you read it, it looks like the thing is a file (socket, whatever) because the main use of the handle interface. This is quite handy when you are handcuffed to using a filehandle because someone else wrote some code you can't change. This function is designed to only send $message to some output handle:

    sub print_to_file_only {
        my( $filehandle, $message ) = @_;
        print {$filehandle} $message;
        }
    

    But sometime you don't want that message to go to the terminal, file, socket, or whatever. You want to see it in your program. You can capture the message in your $string_filehandle because it uses the same handle interface even though it's not a static resource:

    print_to_file_only( $string_filehandle );
    

    Now you'll see the message show up in $string, and you can do whatever you like with it.

    There are many more tricks like this, and I'm tempted to talk about all of them. But, this should be a good start.