Skip to content

Commit

Permalink
use state instead of static closures
Browse files Browse the repository at this point in the history
  • Loading branch information
vaeth committed Jun 18, 2017
1 parent 86b09eb commit e933458
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for schedule

*schedule-v7.5.4:
Martin Väth <martin at mvath.de>:
- Use state instead of static closures

*schedule-v7.5.3_p1:
Martin Väth <martin at mvath.de>:
- schedule-tmux: Treat 0 and - as false only if no symbol follows
Expand Down
9 changes: 5 additions & 4 deletions share/schedule/Schedule/Client/Clientfuncs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# This is part of the schedule project.

BEGIN { require 5.012 }
package Schedule::Client::Clientfuncs v7.5.0;
package Schedule::Client::Clientfuncs v7.5.4;

use strict;
use warnings;
use integer;
use feature 'state';

use Exporter qw(import);
use IO::Socket 1.19 (); # INET or UNIX, depending on user's choice

Expand Down Expand Up @@ -56,8 +58,6 @@ sub client_globals {
$s
}

{ # A static variable:
my $checked = '';
sub openclient {
$socket = $s->timeout($s->tcp() ? sub { IO::Socket::INET->new(
PeerAddr => $s->addr(),
Expand All @@ -79,6 +79,7 @@ sub openclient {
unless($silence);
return ''
}
state $checked = '';
return 1 if($checked);
my $ver;
if(&client_send('version') && &client_recv($ver) &&
Expand All @@ -89,7 +90,7 @@ sub openclient {
return ''
}
$checked = 1
}}
}

sub check_server_version {
my ($v) = @_;
Expand Down
20 changes: 12 additions & 8 deletions share/schedule/Schedule/Client/Cmd/Queue.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# This is part of the schedule project.

BEGIN { require 5.012 }
package Schedule::Client::Cmd::Queue v7.5.0;
package Schedule::Client::Cmd::Queue v7.5.4;

use strict;
use warnings;
use integer;
use feature 'state';

use Cwd ();

use Schedule::Client::Clientfuncs qw(:FUNCS);
Expand All @@ -35,6 +37,7 @@ sub queue_init {
}

{
# Global "args" for subsequent helper functions
my ($status, $title, $text);
my ($user, $host, $hosttext, $cwd, $job);
my $jobtext;
Expand Down Expand Up @@ -198,10 +201,12 @@ sub statusbar {
print("\033]0;$t\007") if($status);
print("\033k$t\033\\") if($title)
}

# End of the helper functions (global "args")
}

{ my $hostname = undef; # a static closure
sub my_hostname {
state $hostname;
return $hostname if(defined($hostname));
$hostname = $ENV{'HOSTNAME'};
unless(&is_nonempty($hostname)) {
Expand All @@ -214,11 +219,10 @@ sub my_hostname {
$hostname =~ s{\c@.*$}{}
}
$hostname
}}
}

{ my $hosttext = undef; # a static closure
sub my_hosttext {
$hosttext //= $ENV{'HOSTTEXT'};
state $hosttext = $ENV{'HOSTTEXT'};
return $hosttext if(defined($hosttext));
my $olderr = undef;
$olderr = undef unless(open($olderr, '>&', \*STDERR)
Expand All @@ -228,10 +232,10 @@ sub my_hosttext {
chomp($hosttext);
$hosttext =~ s{\c@.*$}{};
$hosttext
}}
}

{ my $cwd = undef; # a static closure
sub my_cwd {
state $cwd;
return $cwd if(defined($cwd));
$cwd = (Cwd::getcwd() // '');
unless($_[0]) {
Expand All @@ -246,6 +250,6 @@ sub my_cwd {
}
$cwd =~ s{\c@.*$}{};
$cwd
}}
}

1;
22 changes: 13 additions & 9 deletions share/schedule/Schedule/Connect.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
# common parts of schedule and schedule-server which are always needed

BEGIN { require 5.012 }
package Schedule::Connect v7.5.3;
package Schedule::Connect v7.5.4;

use strict;
use warnings;
use integer;
use feature 'state';

use Getopt::Long 2.24 ();
use File::Spec ();
use IO::Select ();
Expand Down Expand Up @@ -48,11 +50,16 @@ my $serversupallowed = '';
# For modules not appearing here, the above defaults apply.
# If "undefined", no corresponding restriction is required.

my $version753 = $VERSION; # version->declare('v7.5.3')
my $version753 = version->declare('v7.5.3');
my $version754 = $VERSION;
my %minversion = (
# temporary:
'Schedule' => $version753,
'ScheduleServer' => $version753,
'Schedule::Client::Clientfuncs' => $version754,
'Schedule::Client::Cmd::Queue' => $version754,
'Schedule::Helpers' => $version754,
'Schedule::Log' => $version754,

# Keep the following always:
'Schedule::Connect' => undef
Expand Down Expand Up @@ -250,21 +257,18 @@ sub warning {
join("\n" . (' ' x $len), @_), "\n")
}

{ # some static closures
my $reset_col = undef;
my $name_col = undef;
my $warn_col = undef;
my $err_col = undef;
sub incolor {
my $s = shift();
my $mode = shift();
$reset_col //= &my_color('reset');
state $reset_col = &my_color('reset');
state $name_col;
return (($name_col //= &my_color('bold')) . $_[0] . $reset_col)
unless($mode);
state ($warn_col, $err_col);
($mode == 1) ?
(($warn_col //= &my_color('bold cyan')) . $_[0] . $reset_col) :
(($err_col //= &my_color('bold red')) . $_[0] . $reset_col)
}}
}

sub check_version {
my $s = shift();
Expand Down
14 changes: 8 additions & 6 deletions share/schedule/Schedule/Helpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# This is part of the schedule project.

BEGIN { require 5.012 }
package Schedule::Helpers v7.5.0;
package Schedule::Helpers v7.5.4;

use strict;
use warnings;
use integer;
use feature 'state';

use Exporter qw(import);

my @export_is = qw(
Expand Down Expand Up @@ -49,22 +51,22 @@ sub signals {
$SIG{INT} = $SIG{TERM} = ((@_) ? $_[0] : 'IGNORE')
}

{ my $user = undef; # static closure
sub my_user {
state $user;
return $user if(defined($user));
my $user = getpwuid($<);
$user = getpwuid($<);
$user = $< unless(&is_nonempty($user));
$user
}}
}

{ my $ansicolor = undef; # A closure static variable
sub use_ansicolor {
state $ansicolor;
return $ansicolor if(defined($ansicolor));
eval {
require Term::ANSIColor
};
$ansicolor = !$@
}}
}

sub my_color {
&use_ansicolor() ? Term::ANSIColor::color(@_) : ''
Expand Down
8 changes: 5 additions & 3 deletions share/schedule/Schedule/Log.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# This is part of the schedule project.

BEGIN { require 5.012 }
package Schedule::Log v7.5.0;
package Schedule::Log v7.5.4;

use strict;
use warnings;
use integer;
use feature 'state';

#use Sys::Syslog (); # needed if --syslog is selected

use Schedule::Helpers qw(join_quoted);
Expand Down Expand Up @@ -86,9 +88,9 @@ sub count {
@_ ? ($s->{count} = shift()) : $s->{count}
}

{ my $syslog = undef; # A static closure
sub open_internal {
my $s = shift();
state $syslog;
if(defined($syslog) || $s->syslog()) {
eval {
require Sys::Syslog
Expand All @@ -108,7 +110,7 @@ sub open_internal {
}
}
$s->is_open(1)
}}
}

sub log_internal {
my $s = shift();
Expand Down

0 comments on commit e933458

Please sign in to comment.