Last active
April 24, 2021 17:54
-
-
Save matthewpersico/c4d57556483ae85aef4b53af0aeccf4e to your computer and use it in GitHub Desktop.
'eval' as a control structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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