I don't know about you guys, but to me, the less typing the better. That's why i have put together this simple module to automate slurp. I'm sure there is already something like this out there, but I'm new to Perl and through you guys might enjoy it!

#!/usr/bin/perl package Slurp; use strict; use warnings; use Carp; { no strict 'refs'; *main::slurp = \&Slurp::slurp; } sub slurp { { local $/; open(my $fh,'<',$_[0]) || croak " Cant open [ $_[0] ] -- ($!) "; $_ = <$fh>; close($fh); return($_); } } 1;

Replies are listed 'Best First'.
Re: Automated Slurp
by FunkyMonk (Bishop) on Jan 02, 2010 at 17:07 UTC
    I'm sure there is already something like this out there
    There sure is! File::Slurp
Re: Automated Slurp
by ruzam (Curate) on Jan 02, 2010 at 20:33 UTC

    For argument's sake,

    { no strict 'refs'; *main::slurp = \&Slurp::slurp; }

    could also be written as an export:

    use Exporter; our @ISA = qw( Exporter ); our @EXPORT = qw( slurp );

    or better, an export on request:

    use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( slurp );

      Not "also be written", but "better be written":

      Not every code that would use this module wants the slurp routine aliased as main::slurp. Most times, calling code wants the slurp routine either in its own namespace or nowhere except in the Slurp:: namespace. Using Exporter (or one of the alternatives) would allow that, the original code doesn't. It should at least call the caller method to find out into which namespace the alias should be exported instead of blindly assuming main::. And it should create the alias inside the import routine, allowing use Slurp () to load the module without creating any alias.

      Exporter is a huge load of code, and sometimes just overkill. The "tricks" from the Manual Exporting section of the Teflon Tape article shows how to export with less code. On the other hand, since so many modules use the Exporter, it is already in memory without extra costs most of the times, so writing own exporting code costs more than just using the Exporter and may create new bugs.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      I havnt had a thorough look at Exporter yet, but it does seem like the more elegant solution, thanks!
        seem like the more elegant solution

        For what you are doing in your code, it isn't. Why substitute 500 lines of code for 4?

        Errors compound geometrically with the number of lines of code.

        Exporter is fine if you need its facilities. It's overhead, complexity and a drag on maintenance if you do not.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Automated Slurp
by ikegami (Patriarch) on Jan 02, 2010 at 17:15 UTC
    cpan File::Slurp
    would have been less typing. File::Slurp

    ( oops! someone posted a similar reply since I opened to the OP )

      I prefer
      use Path::Class qw{file}; my $data=file($filename)->slurp;
Re: Automated Slurp
by eyepopslikeamosquito (Archbishop) on Jan 03, 2010 at 06:32 UTC
      In what way is it an alternative?