Skip to content

Instantly share code, notes, and snippets.

@eiro
Created March 20, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiro/dc2e8d620290ab4757b112b6c87f02cd to your computer and use it in GitHub Desktop.
Save eiro/dc2e8d620290ab4757b112b6c87f02cd to your computer and use it in GitHub Desktop.
lecture
sub { @{shift->{TrafficChecker}{Proxy}}{qw(Protocol Host Port)} = qw(https hello.com 1212) }
sub {
map { @$_{qw(Protocol Host Port)} = qw(https hello.com 1212) };
shift>{TrafficChecker}{Proxy}
}
sub ($self) {
map { @$_{qw(Protocol Host Port)} = qw(https hello.com 1212) };
$self->{TrafficChecker}{Proxy}
}
@arnaudcordier
Copy link

C'est dur de ne pas voir explicitement ce qu'on reçoit du premier coup d'œil.

Avec la dernière je rajouterais

sub ($self) {
   my ($config) = @_;
   map { @$config{qw(Protocol Host Port)} = qw(https hello.com 1212) };
   $self->{TrafficChecker}{Proxy}
}

@eiro
Copy link
Author

eiro commented Mar 21, 2019

ta version

sub ($self) {
   my ($config) = @_;
   map { @$config{qw(Protocol Host Port)} = qw(https hello.com 1212) };
   $self->{TrafficChecker}{Proxy}
}

est ... étrange: en plus d'être fausse

  • soit tu utilises les signatures, soit tu déroule @_ mais pas les deux
  • dans le map, c'est $_ qui doit être traitée

j'en conclus que le map n'était pas une bonne solution non plus ... est-ce que

sub ($config) {
    my $proxy = $config->{TrafficChecker}{Proxy};
    @$proxy{ qw(Protocol Host Port) } = qw(https hello.com 1212);
}

te semble plus lisible ? la version la plus lourdingue serait presque du php:

sub ($config) {
    my $proxy = $config->{TrafficChecker}{Proxy};
    $$proxy{Protocol} = 'https';
    $$proxy{Host} = 'hello.com';
    $$proxy{Port} = 1212;
    return @$proxy{qw( Protocol Host Port )};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment