• Publicidad

Exiting subroutine via next

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

Exiting subroutine via next

Notapor natxo » 2010-02-02 06:00 @291

Hola:

tengo un nuevo plugin para Nagios que casi funciona como pretendo ;)

Aquí va el código:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!perl
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5.  
  6. # hash with the nagios errorlevels
  7. my %ERRORS = (
  8.     'OK'        => 0,
  9.     'WARNING'   => 1,
  10.     'CRITICAL'  => 2,
  11.     'UNKNOWN'   => 3,
  12. );
  13.  
  14. # here we save the number of missed shortcuts in the subroutine _check_path
  15. my $count = 0;
  16. # here I keep a list of missing shortcuts in the subroutine _check_path
  17. my @wrongshortcut ;
  18.  
  19. # before running we need a mapping to \\server\share or we will miss some apps
  20. system("net use M: \\\\server\\share") == 0 or warn "mapping M: drive failed\n" ;
  21.  
  22. # get the desktop folder
  23. my $desktop = "D:\\path\\to\\folder with shortcuts\\" ;
  24.  
  25. # find all *.lnk files (case insensitive) in $desktop
  26. # the find function calls the get_shortcut subroutine in this case
  27. find(\&get_shortcut, $desktop);
  28.  
  29. # subroutine to get all the shortcuts in the folder
  30. sub get_shortcut {
  31.     #the explorer.lnk shortcut has a strange target, skip it
  32.     next if $_ eq "explorer.lnk" ;
  33.  
  34.     # process the rest of the shortcuts
  35.     if ($_ =~ /.*\.(lnk)/i) {
  36.         parse_lnk($_);
  37.     }
  38. }
  39.        
  40. # subroutine to get the target path of the *.lnk files
  41. # gets only one argument, the shortcut location.
  42. sub parse_lnk {
  43.     my ( $file ) = @_;
  44.     use Win32::Shortcut;
  45.  
  46.     # create a new shortcut object and load it
  47.     my $link = Win32::Shortcut->new();
  48.     $link->Load($file) or print "$file shortcut not found\n" ;
  49.  
  50.     # if you need to debug the links and targets, uncomment next line
  51.     #print "$link->{File}\t$link->{Path}\n" ;
  52.  
  53.     # check the target path of the shortcut
  54.     _check_path( $link->{Path}, $link->{File} );
  55.  
  56.     # close the shortcut object
  57.     $link->Close();
  58. }
  59.  
  60. # this checks the existance of the target program in the parse_link
  61. sub _check_path {
  62.     my ( $target ) = @_;
  63.     if ( ! -e $target ) {
  64.         # add 1 to $count
  65.         $count++ ;
  66.         # fill @wrongshortcut with missing target
  67.         push ( @wrongshortcut, $target);
  68.     }
  69. }
  70.  
  71. if ($count == 0) {
  72.     print "$ERRORS{'OK'}\t all shortcuts have valid targets\n";
  73. }
  74. else {
  75.     print "$ERRORS{'WARNING'}\t @wrongshortcut is not a valid target\n" ;
  76. }
  77.  
  78. # when we're done, remove the mapping
  79. system("net use M: /delete") == 0 or warn "could not unmap M: drive\n" ;
  80.  
Coloreado en 0.005 segundos, usando GeSHi 1.0.8.4


Funciona bien, pero no quiero ver ese mensaje de 'Exiting subroutine via next ... (script line)'

¿Alguna idea?

TIA
saludos,
Natxo Asenjo
natxo
Perlero nuevo
Perlero nuevo
 
Mensajes: 76
Registrado: 2007-08-09 16:22 @723
Ubicación: Países Bajos

Publicidad

Re: Exiting subroutine via next

Notapor explorer » 2010-02-02 06:03 @293

Cambia el next() de la línea 32 por return().

O quita la línea de use warnings;
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Exiting subroutine via next

Notapor natxo » 2010-02-02 06:13 @300

explorer escribiste:Cambia el next() de la línea 32 por return().

¿Esto es porque está dentro de una subrutina? Ok, funciona perfecto.
saludos,
Natxo Asenjo
natxo
Perlero nuevo
Perlero nuevo
 
Mensajes: 76
Registrado: 2007-08-09 16:22 @723
Ubicación: Países Bajos


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 12 invitados

cron