| 1 | package File::Glob::Slurp; | ||||||||||
| 2 | 2 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; | |||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 0 | ▶ | *CORE::GLOBAL::glob = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 11 | 0 | my $path = shift; | |||||||||
| 12 | 0 | $path =~ s/\A\s+//; | |||||||||
| 13 | 0 | $path =~ s/\s+\z//; | |||||||||
| 14 | 0 | my $slurp; | |||||||||
| 15 | 0 | ▶ | if ( $path =~ m{\w+://} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | eval { require LWP::Simple; }; | |||||||||
| 17 | 0 | ▶ | croak $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | $slurp = LWP::Simple::get($path); | |||||||||
| 19 | } | ||||||||||
| 20 | else { | ||||||||||
| 21 | 0 | local $/; | |||||||||
| 22 | 0 | ▶ | open my $fh, '<', $path or croak "$path:$!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | $slurp = CORE::readline($fh); | |||||||||
| 24 | 0 | close $fh; | |||||||||
| 25 | } | ||||||||||
| 26 | 0 | return $slurp; | |||||||||
| 27 | 2 | }; | |||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | if ($0 eq __FILE__){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | ▶ | my $content = <http://www.dan.co.jp/>; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||
| 31 | 0 | print $content; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 2 | 1; # End of File::Glob::Slurp | |||||||||
| 35 | |||||||||||
| 36 | =head1 NAME | ||||||||||
| 37 | |||||||||||
| 38 | File::Glob::Slurp - Turns <> into a slurp operator | ||||||||||
| 39 | |||||||||||
| 40 | =head1 VERSION | ||||||||||
| 41 | |||||||||||
| 42 | $Id: Slurp.pm,v 0.2 2009/06/10 05:51:19 dankogai Exp dankogai $ | ||||||||||
| 43 | |||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||
| 45 | |||||||||||
| 46 | Quick summary of what the module does. | ||||||||||
| 47 | |||||||||||
| 48 | Perhaps a little code snippet. | ||||||||||
| 49 | |||||||||||
| 50 | use File::Glob::Slurp; | ||||||||||
| 51 | # slurps path/to/filename.ext | ||||||||||
| 52 | my $home = <path/to/filename.ext>; | ||||||||||
| 53 | # you can do this if you have LWP::Simple | ||||||||||
| 54 | my $away = <http://example.com/>; | ||||||||||
| 55 | |||||||||||
| 56 | =head1 EXPORT | ||||||||||
| 57 | |||||||||||
| 58 | tweaks C<CORE::GLOBAL::glob> | ||||||||||
| 59 | |||||||||||
| 60 | =head1 DESCRIPTION | ||||||||||
| 61 | |||||||||||
| 62 | HACK #90 of PERL HACK proved that C<< <*glob*> >> operator is a pretty | ||||||||||
| 63 | good place to implement micro-DSL. This module turns ancient | ||||||||||
| 64 | C<< *glob* >> operator into modern C<< slurp >> operator! | ||||||||||
| 65 | |||||||||||
| 66 | As shown in L</SYNOPSIS>, The overridden C<< <> >> slurps not only | ||||||||||
| 67 | local files but also URL if you have L<LWP::Simple> installed. | ||||||||||
| 68 | |||||||||||
| 69 | =head2 CAVEAT | ||||||||||
| 70 | |||||||||||
| 71 | Unfortunately C<< <> >> also acts as C<readline()>. Therefore | ||||||||||
| 72 | |||||||||||
| 73 | my $content = <$path>; | ||||||||||
| 74 | |||||||||||
| 75 | Does not work. In such cases simply add whitespece like: | ||||||||||
| 76 | |||||||||||
| 77 | my $content = < $path >; | ||||||||||
| 78 | |||||||||||
| 79 | =head1 AUTHOR | ||||||||||
| 80 | |||||||||||
| 81 | Dan Kogai, C<< <dankogai at dan.co.jp> >> | ||||||||||
| 82 | |||||||||||
| 83 | =head1 BUGS | ||||||||||
| 84 | |||||||||||
| 85 | Please report any bugs or feature requests to C<bug-file-glob-slurp at rt.cpan.org>, or through | ||||||||||
| 86 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Glob-Slurp>. I will be notified, and then you'll | ||||||||||
| 87 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 SUPPORT | ||||||||||
| 90 | |||||||||||
| 91 | You can find documentation for this module with the perldoc command. | ||||||||||
| 92 | |||||||||||
| 93 | perldoc File::Glob::Slurp | ||||||||||
| 94 | |||||||||||
| 95 | You can also look for information at: | ||||||||||
| 96 | |||||||||||
| 97 | =over 4 | ||||||||||
| 98 | |||||||||||
| 99 | =item * RT: CPAN's request tracker | ||||||||||
| 100 | |||||||||||
| 101 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Glob-Slurp> | ||||||||||
| 102 | |||||||||||
| 103 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 104 | |||||||||||
| 105 | L<http://annocpan.org/dist/File-Glob-Slurp> | ||||||||||
| 106 | |||||||||||
| 107 | =item * CPAN Ratings | ||||||||||
| 108 | |||||||||||
| 109 | L<http://cpanratings.perl.org/d/File-Glob-Slurp> | ||||||||||
| 110 | |||||||||||
| 111 | =item * Search CPAN | ||||||||||
| 112 | |||||||||||
| 113 | L<http://search.cpan.org/dist/File-Glob-Slurp/> | ||||||||||
| 114 | |||||||||||
| 115 | =back | ||||||||||
| 116 | |||||||||||
| 117 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 118 | |||||||||||
| 119 | Hack #90 of Perl Hacks L<http://oreilly.com/catalog/9780596526740/> | ||||||||||
| 120 | |||||||||||
| 121 | L<Perl6::Slurp> | ||||||||||
| 122 | |||||||||||
| 123 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 124 | |||||||||||
| 125 | Copyright 2009 Dan Kogai, all rights reserved. | ||||||||||
| 126 | |||||||||||
| 127 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 128 | under the same terms as Perl itself. | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||