Skip to content

Instantly share code, notes, and snippets.

@matthewpersico
Last active April 24, 2021 17:54
Show Gist options
  • Save matthewpersico/c4d57556483ae85aef4b53af0aeccf4e to your computer and use it in GitHub Desktop.
Save matthewpersico/c4d57556483ae85aef4b53af0aeccf4e to your computer and use it in GitHub Desktop.
'eval' as a control structure
sub some_func {
my $ctx = $_[0];
return undef if ( !$ctx->{wrkdir} or !-d ctx->{wrkdir} );
my $prevdir = getcwd();
chdir $ctx->{wrkdir};
my $status;
$status = f1($c);
if ($status) {
chdir $prevdir;
return $status;
}
$status = f3($c);
if ($status) {
chdir $prevdir;
return $status;
}
$status = f5($c);
if ($status) { # Pednantic, yes
chdir $prevdir;
return $status;
}
# If we are here, we are good
chdir $prevdir;
return $status;
}
sub some_func {
my $ctx = $_[0];
return undef if ( !$ctx->{wrkdir} or !-d ctx->{wrkdir} );
my $prevdir = getcwd();
chdir $ctx->{wrkdir};
my $status;
eval {
$status = f1($c);
die if $status;
$status = f3($c);
die if $status;
$status = f5($c);
die if $status; #Pedantic, yes
};
chdir $prevdir;
return $status;
}
sub some_func {
my $ctx = $_[0];
return undef if ( !$ctx->{wrkdir} or !-d ctx->{wrkdir} );
my $prevdir = getcwd();
chdir $ctx->{wrkdir};
my $status;
$status = f1($c);
goto ONE_EXIT if $status;
$status = f3($c);
goto ONE_EXIT if $status;
$status = f5($c);
goto ONE_EXIT if $status; # Pedantic, yes
ONE_EXIT:
chdir $prevdir;
return $status;
}
sub some_func {
my $ctx = $_[0];
return undef if ( !$ctx->{wrkdir} or !-d ctx->{wrkdir} );
my $prevdir = getcwd();
chdir $ctx->{wrkdir};
my $status;
$status = f1($c);
if ($status==0) {
$status = f3($c);
if ($status==0) {
$status = f5($c);
}
}
chdir $prevdir;
return $status;
}
# If we are here, we are good
chdir $prevdir;
return $status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment