Question

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?

Was it helpful?

Solution

OTHER TIPS

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

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; <> };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top