packetstormer has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks
I have a 'best practice' query. Suppose the simple script below. I want to (or maybe I don't!) create a module that might fail under certain circumstances. However, I would like to continue on from the calling script. I realise I could use 'return' but then I'd have to catch that value
So, with that in mind does anyone have any thoughts on how the package below might be rewritten to allow the calling script continue? I could also just print "SPLAT";return but I am unsure what the norm is.
You can see that I could use eval (commented out) but again, I am unsure of the best practice!?
#!/usr/bin/perl use strict; use Data::Dumper; my @r = (1..5); foreach (@r) { print "Start for $_\n"; my $splat = Splat->new(); #eval { $splat->splat() }; #if ($@) {next} if ($_ ==1) { print "Never getting this far\n" } } print "Nor this far\n"; package Splat; use Carp qw(croak); sub new { my $class = shift; my $self = {}; bless $self,$class; return $self; } sub splat { my $self = shift; croak "SPLAT!" if ! $self->{splat} } 1;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Croak, return et al.
by McA (Priest) on Aug 16, 2014 at 16:07 UTC | |
by packetstormer (Monk) on Aug 16, 2014 at 18:22 UTC | |
Re: Croak, return et al.
by AppleFritter (Vicar) on Aug 16, 2014 at 16:04 UTC | |
by Laurent_R (Canon) on Aug 16, 2014 at 17:59 UTC | |
by AppleFritter (Vicar) on Aug 16, 2014 at 19:22 UTC | |
Re: Croak, return et al.
by Anonymous Monk on Aug 16, 2014 at 20:22 UTC |