tags:

views:

78

answers:

3

I had a look at the source of Slurp and I would love to understand how does slurp() work:

sub slurp { 
    local( $/, @ARGV ) = ( wantarray ? $/ : undef, @_ ); 
    return <ARGV>;
}

Where is the file even opened?

+5  A: 

See ARGV and $/ in perldoc perlvar.

See also File::Slurp.

Sinan Ünür
+5  A: 

ARGV is a handle, the file has been opened implicitly.

daxim
A: 

This snippet puts the filename in @ARGV. The ARGV filehandle implicitly opens the files it sees in @ARGV. This is the same filehandle that we don't see in the diamond operator <> since it's the default filehandle for that operator.

This is the same Perl idiom as:

 my $data = do { local( @ARGV, $/ ) = $file; <> };
brian d foy