File::Glob::Slurp - Turns <> into a slurp operator


File-Glob-Slurp documentation Contained in the File-Glob-Slurp distribution.

Index


Code Index:

NAME

Top

File::Glob::Slurp - Turns <> into a slurp operator

VERSION

Top

$Id: Slurp.pm,v 0.2 2009/06/10 05:51:19 dankogai Exp dankogai $

SYNOPSIS

Top

Quick summary of what the module does.

Perhaps a little code snippet.

  use File::Glob::Slurp;
  # slurps path/to/filename.ext
  my $home = <path/to/filename.ext>; 
  # you can do this if you have LWP::Simple
  my $away = <http://example.com/>;

EXPORT

Top

tweaks CORE::GLOBAL::glob

DESCRIPTION

Top

HACK #90 of PERL HACK proved that <*glob*> operator is a pretty good place to implement micro-DSL. This module turns ancient *glob* operator into modern slurp operator!

As shown in SYNOPSIS, The overridden <> slurps not only local files but also URL if you have LWP::Simple installed.

CAVEAT

Unfortunately <> also acts as readline(). Therefore

  my $content = <$path>;

Does not work. In such cases simply add whitespece like:

  my $content = < $path >;

AUTHOR

Top

Dan Kogai, <dankogai at dan.co.jp>

BUGS

Top

Please report any bugs or feature requests to bug-file-glob-slurp at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Glob-Slurp. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc File::Glob::Slurp

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Glob-Slurp

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/File-Glob-Slurp

* CPAN Ratings

http://cpanratings.perl.org/d/File-Glob-Slurp

* Search CPAN

http://search.cpan.org/dist/File-Glob-Slurp/

ACKNOWLEDGEMENTS

Top

Hack #90 of Perl Hacks http://oreilly.com/catalog/9780596526740/

Perl6::Slurp

COPYRIGHT & LICENSE

Top


File-Glob-Slurp documentation Contained in the File-Glob-Slurp distribution.

package File::Glob::Slurp;
use 5.008001;
use warnings;
use strict;
our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g;
use Carp;

no warnings 'redefine';

*CORE::GLOBAL::glob = sub {
    my $path = shift;
    $path =~ s/\A\s+//;
    $path =~ s/\s+\z//;
    my $slurp;
    if ( $path =~ m{\w+://} ) {
        eval { require LWP::Simple; };
        croak $@ if $@;
        $slurp = LWP::Simple::get($path);
    }
    else {
        local $/;
        open my $fh, '<', $path or croak "$path:$!";
        $slurp = CORE::readline($fh);
        close $fh;
    }
    return $slurp;
};

if ($0 eq __FILE__){
    my $content = <http://www.dan.co.jp/>;
    print $content;
}

1; # End of File::Glob::Slurp