1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/env perl use strict; use warnings; sub _validate_arguments { my ( $first_arg, $second_arg ) = @_; croak "Called without arguments." if @_ < 1; croak "Called with " . scalar @_ . " arguments. Expected arguments: 1 or 2." if @_ > 2; croak "The first argument is not defined." if ! defined $first_arg; ... ... carp "The first argument refers to an empty list!" if ! @$first_arg; return $first_arg, $second_arg; } sub my_function { my ( $first_arg, $second_arg ) = _validate_arguments( @_ ); return if ! @$first_arg; ... ... }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env perl use strict; use warnings; sub _validate_arguments { my ( $first_arg, $second_arg, $arg_count ) = @_; croak "Called without arguments." if $arg_count < 1; croak "Called with $arg_count arguments. Expected arguments: 1 or 2." if $arg_count > 2; croak "The first argument is not defined." if ! defined $first_arg; ... ... carp "The first argument refers to an empty list!" if ! @$first_arg; return; } sub my_function { my ( $first_arg, $second_arg ) = @_; _validate_arguments( $first_arg, $second_arg, scalar @_ ); return if ! @$first_arg; ... ... }
2013-08-27T08:00:39 KuerbisIch dachte mir, dass das, was croak macht, Exceptions genannt werden?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/perl use strict; use warnings; use Carp; sub foo{ my %in = ( name => undef, vname => undef, ort => undef, suburb => '', @_); my @requires = qw(name vname ort); return eval{ foreach my $r(@requires){ croak qq('$r' not set) unless defined $in{$r}; } "@in{@requires}"; # das ist der return }; } my $li = foo( xname => 'Henne', vname => 'Horst', ort => 'Henneberg' ) or die $@; print "Angekommen: $li";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/env perl use strict; use warnings; sub _validate_arguments { my ( $first_arg, $second_arg ) = @_; croak "Called without arguments." if @_ < 1; croak "Called with " . scalar @_ . " arguments. Expected arguments: 1 or 2." if @_ > 2; croak "The first argument is not defined." if ! defined $first_arg; ... ... carp "The first argument refers to an empty list!" if ! @$first_arg; return ( $first_arg, $second_arg ); } sub my_function { my ( $first_arg, $second_arg ) = &_validate_arguments; return if ! @$first_arg; ... ... }