Jue Sep 18, 2008 1:43 pm
|
 |
tetu
Perlero Nuevo

|
Registrado: 18 Sep 2008
Mensajes: 1
|
|
| Uso de Hash |
|
|
Hola, ojalá alguien me pueda ayudar con esta función que fue desarrollada por una persona que no está disponible en este momento y esta "cosa rara" no está funcionando.
Lo que se pretende es que si una persona ($entry que es el parámetro de entrada) es "Trading" (un tipo de empleado), la función devuelva true:
| Perl: | sub userHasVariance ($ )
{
my ($entry) = @_;
my %variances;
my $key;
my (@attrValues, @varValues);
my ($attrValue, $varValue);
my $attrVar;
my $configVar;
my $configValue;
my (@userAttrValues, $userConfigValue, $userAttrValue);
my $thisValueFound = FALSE;
# get all of the variances
%variances = &getConfigFile ('variance');
# look through all of the variances
foreach $configVar (keys %variances) {
$userConfigValue = $entry-> get_value("$configVar");
foreach $configValue (keys % {$variances{$configVar}}) {
# is this user in the group of users we want to check?
if ($userConfigValue =~ / $configValue/i ) {
#which attribute are we basing the variance on
$attrVar = $variances{$configVar}{$configValue}{attr };
$attrValue = $variances{$configVar}{$configValue}{value };
# get the users attributes
@userAttrValues = $entry-> get_value("$attrVar");
# check each of the values to see if it is in the list
foreach $userAttrValue (@userAttrValues) {
if ($attrValue =~ /, $userAttrValue,/i ) {
$thisValueFound = TRUE;
} elsif ($attrValue =~ /^ $userAttrValue,/i ) {
$thisValueFound = TRUE;
} elsif ($attrValue =~ /, $userAttrValue$/i ) {
$thisValueFound = TRUE;
} elsif ($attrValue =~ /^ $userAttrValue$/i ) {
$thisValueFound = TRUE;
} else { # not found user does not have a variance
return FALSE;
}
}
# if user has nono of these attributes return FALSE (default setting)
# or the user has all of them, then return TRUE;
return $thisValueFound;
}
}
# user not in these "Groups"
return FALSE;
}
# no variances found
return FALSE;
} |
El archivo de configuración es el siguiente:
| Código: |
| variance:collectiveParentRDN:Trading:applications:38 |
ColectiveParentRDN es por ejemplo ou=Trading Partners,o=Com.
Este es el caso que la función debería ser cierta y también, cuando el usuario tiene en el atributo applications el valor 38... lo que ahora sucede es que en donde se usa la función, que es un if... nunca se entra... ¡¡y no puedo sacar por qué!! ¡porqué este código es demasiado complejo! =(
Espero que no falten datos...
¡Mil gracias!
Stefania. |
|
|
|

Jue Sep 18, 2008 2:55 pm
|
 |
explorer
Moderador

|
Registrado: 24 Jul 2005
Mensajes: 4239
Ubicación: Valladolid, España
|
|
|
|
|
Bienvenido a los foros de Perl en Español, tetu.
Te recomendaría el uso del depurador: ir paso a paso e ir sacando el valor de las variables para ver qué contienen.
Y desde luego, sí que se puede simplificar un poco...
¿Cómo realizas la lectura del fichero de configuración? Es que no veo cómo puede quedar almacenado en %variances. |
|

Sab Sep 20, 2008 8:24 am
|
 |
Jenda
Perlero Frecuente

|
Registrado: 29 Oct 2007
Mensajes: 108
Ubicación: Praga, Republica Checa
|
|
|
|
|
1) No declares las variables antes de que las necesites. Hay lenguajes en que tienes que declarar todas las variables antes del primer comando. No es así en Perl.
Así que es mejor escribir | Perl: | foreach my $configVar (keys %variances) | que | Perl: | my $configVar;
...
foreach $configVar (keys %variances) |
2) Casi nunca es necesario escribir & cuando llamas a una función. Y cuando es necesario deberías saber por qué.
3) "$variable" es inútil. No pongas las comillas sobre la variable. Si la variable contiene una cadena, solo haces una copia. Si contiene un número, fuerzas a Perl a convertirlo en una cadena de caracteres y haces la copia. Pero si la variable contiene una referencia las comillas la matan. Lo que obtienes no será ninguna referencia sino una cadena de caracteres que parece una referencia. Pero no se puede usar así.
4) Ésto es una tontería | Perl: | if ($attrValue =~ /, $userAttrValue,/i ) {
$thisValueFound = TRUE;
} elsif ($attrValue =~ /^ $userAttrValue,/i ) {
$thisValueFound = TRUE;
} elsif ($attrValue =~ /, $userAttrValue$/i ) {
$thisValueFound = TRUE;
} elsif ($attrValue =~ /^ $userAttrValue$/i ) {
$thisValueFound = TRUE;
} else { # not found user does not have a variance
return FALSE;
} | eso se puede escribir así: | Perl: | if ($attrValue =~ / (?:^|, )$userAttrValue(?:,|$ )/i ) {
$thisValueFound = TRUE;
} else { # not found user does not have a variance
return FALSE;
} |
Y hay un problema con ambas soluciones. La $userAttrValue podría contener un carácter especial para expresiones regulares. Para prevenir esos problemas hay que hacer algo así:
| Perl: | if ($attrValue =~ / (?:^|, )\Q $userAttrValue\E (?:,|$ )/i ) {
$thisValueFound = TRUE;
} else { # not found user does not have a variance
return FALSE;
} |
|
|

Powered by phpBB © 2001, 2005 phpBB Group
|