Changeset 9580


Ignore:
Timestamp:
12/09/09 17:18:04 (2 years ago)
Author:
adamk
Message:

I'm sick and tired of not having a simple slurp function available for Padre internals, so I've added one

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Padre/lib/Padre/Util.pm

    r9579 r9580  
    7474##################################################################### 
    7575# Miscellaneous Functions 
     76 
     77=pod 
     78 
     79=head2 C<slurp> 
     80 
     81    my $content = Padre::Util::slurp( $file ); 
     82    if ( $content ) { 
     83        print $$content; 
     84    } else { 
     85        # Handle errors appropriately 
     86    } 
     87 
     88This is a simple slurp implementation, provided as a convenience for 
     89internal Padre use when loading trivial unimportant files for which 
     90we don't need anything more robust. 
     91 
     92All file reading is done with binmode enabled, and data is returned by 
     93reference to avoid needless copying. 
     94 
     95Returns the content of the file as a SCALAR reference if the file exists 
     96and can be read. 
     97 
     98Returns false if loading of the file failed. 
     99 
     100This function is only expected to be used in situations where the file 
     101should almost always exist, and thus the reason why reading the file 
     102failed isn't really important. 
     103 
     104=cut 
     105 
     106sub slurp { 
     107    my $file = shift; 
     108    open my $fh, '<', $file or return ''; 
     109    binmode $fh; 
     110    local $/ = undef; 
     111    my $content = <$fh>; 
     112    close $fh; 
     113    return \$content; 
     114} 
    76115 
    77116=pod 
Note: See TracChangeset for help on using the changeset viewer.