| | 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 | |
| | 88 | This is a simple slurp implementation, provided as a convenience for |
| | 89 | internal Padre use when loading trivial unimportant files for which |
| | 90 | we don't need anything more robust. |
| | 91 | |
| | 92 | All file reading is done with binmode enabled, and data is returned by |
| | 93 | reference to avoid needless copying. |
| | 94 | |
| | 95 | Returns the content of the file as a SCALAR reference if the file exists |
| | 96 | and can be read. |
| | 97 | |
| | 98 | Returns false if loading of the file failed. |
| | 99 | |
| | 100 | This function is only expected to be used in situations where the file |
| | 101 | should almost always exist, and thus the reason why reading the file |
| | 102 | failed isn't really important. |
| | 103 | |
| | 104 | =cut |
| | 105 | |
| | 106 | sub 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 | } |