Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Completely Useless

by btrott (Parson)
on Aug 12, 2000 at 04:07 UTC ( #27590=snippet: print w/replies, xml ) Need Help??
Description: So this is probably the most useless module ever, but I thought it was kind of fun, because... I don't know. I like writing things that seem like English but are actually Perl, I guess.

Usage:

use Silly::Die; EXPR or die trying;
Where EXPR is, of course, an expression. For example:
open FILE, "foo" or die trying;

    package Silly::Die;

    sub import {
        my $class  = shift;
        my $caller = caller;
        no strict 'refs';
        *{"${caller}::trying"} = \&{"${class}::trying"};
    }

    sub trying { $! }

    1;

Replies are listed 'Best First'.
RE: Completely Useless
by btrott (Parson) on Aug 12, 2000 at 05:45 UTC
    Hey, you know what'd be neat? To customize the error message. Sure, it's still just as useless, but that's okay. So here's the new version:
    package Silly::Die; use strict; use vars qw/$OUTPUT/; sub import { my $class = shift; my $caller = caller; no strict 'refs'; *{"${caller}::trying"} = \&{"${class}::trying"}; $OUTPUT = shift if @_ && ref $_[0] eq "CODE"; } sub trying { $OUTPUT ? $OUTPUT->($!) : $! } 1;
    Usage is the same as before, but if you want an extra special output function, try this:
    use Silly::Die sub { qq(I died with: "@_") }; open FILE, "foo" or die trying;
    Prints:
    I died with: "No such file or directory" at ./test.pl line 9.
RE: Completely Useless
by mwp (Hermit) on Aug 12, 2000 at 04:30 UTC
    bond@russia:~$ perl -I/usr/local/lib/m4 \ -MSecret::Agent -e 'save->world()' died trying at line 007 (eval 1).

    I guess he forgot the -w flag.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://27590]
help
Chatterbox?
[Corion]: Yaerox: That's a somewhat hard problem. Encode solves the conversion part, but for guessing what encoding a file is in, that's the hard part
[Corion]: Yaerox: There is Encode::Guess, but that needs a limited set of inputs, and it also cannot handle multiple single-byte encodings
[Corion]: If you have a BOM, that's a really easy way to recognize UTF-8. Otherwise, you can try to decode a file from UTF-8, and if that works OK and doesn't crash, most likely the file was valid UTF-8
[Corion]: But as "ansi" (Latin-1?) is a single-byte encoding, any file is a valid ANSI file

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-03-28 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (332 votes). Check out past polls.