IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

problème fonction syntaxe


Sujet :

Langage Perl

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2009
    Messages : 236
    Par défaut problème fonction syntaxe
    Bonjour,

    J'ai un problème de syntaxe avec ma fonction !

    La voici :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    sub boucle(){	
    	while ( my $ligne = <FICHIER>) {
    		$cpt++;
    		if (( $ligne =~ m/^^@@\s*(.+)/ ) || 
    			( $ligne =~ m/^^@\s*(.+)/ ) ||
    			( $ligne =~ m/^^sta\s*(.+)/ )){
    				my ($NomFichier, $niv) = @_; # paramètres de la fonction boucle()
    				$tab_nb++;
    				$NomFichier =  $1;
    				$tab[$tab_nb][0]=$NomFichier;
    				$tab[$tab_nb][1]=$niv;
    			boucle();
    			open FICHIER, '<', $NomFichier or die("$NomFichier non existant : $!\n");
    		}		
    	}	
    }
    boucle();
    J'ai cette erreur :

    main::boucle() called too early to check prototype at verif.pl line 44.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.

    J'ai aussi essayé comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    sub boucle(){	
    	while ( my $ligne = <FICHIER>) {
    		$cpt++;
    		if (( $ligne =~ m/^^@@\s*(.+)/ ) || 
    			( $ligne =~ m/^^@\s*(.+)/ ) ||
    			( $ligne =~ m/^^sta\s*(.+)/ )){
    				$tab_nb++;
    				$tab[$tab_nb][0]=$_[0];
    				$tab[$tab_nb][1]=$_[1];
    			boucle($1,$_[1]+1);
    			open FICHIER, '<', $_[0] or die("$_[0] non existant : $!\n");
    		}		
    	}	
    }
    boucle("Init.sql",0);
    J'ai cette erreur :

    main::boucle() called too early to check prototype at verif.pl line 44.
    Too many arguments for main::boucle at verif.pl line 50, near "0)"
    Execution of verif.pl aborted due to compilation errors.

    Une idée ?????

  2. #2
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Par défaut
    Quand tu crées une fonction récursive, celle-ci s'appelant elle même doit être pré-déclarée (c'est le seul cas, à ma connaissance, où il est indispensable de prédéclarer une fonction).

    pour cela, tu écris;

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    sub boucle;
    sub boucle {
      ...
      boucle();
    }

  3. #3
    Membre éclairé
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2009
    Messages : 236
    Par défaut
    Merci ça a résolu ma première erreur mais j'ai encore

    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.
    Use of uninitialized value in print at verif.pl line 57, <FICHIER> line 728.

    c'est a cause de mon $niv je pense.
    je veux juste que cette varible s'incremente $niv++ mais j'arrive pas a le faire ... pourtant c'est simple !

    une idée?

  4. #4
    Membre éclairé
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2009
    Messages
    236
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2009
    Messages : 236
    Par défaut
    Dimitry.e le problème ne vient pas de la,
    voici mon code entier :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    # !usr/bin/perl -w
    use strict;
    use warnings;
     
    my $cpt = 0; # Compteur
    my @tab =([0]); # Tableau 
    # my $niv = 0;
    # my $NomFichier ="Init.sql" ; # Récupération nom de fichier
    my $tab_nb = 0; # Compteur
    my $j = 0; # Variable compteur pour écriture du fichier	
    my $y = 0; # Variable compteur pour écriture du fichier
     
    #######################################################################################################
    #                         Début du traitement et ouverture fichiers                                   #
    #######################################################################################################
     
        print "\n-------------------------------------------------------\n";
        print "|---------------  Debut du traitement  ---------------|\n";
        print "-------------------------------------------------------\n";
     
    open FICHIER, '<', "Init.sql" or die("E/S : $!\n");
    open ECRIRE,  '>', "verif.txt" or die("Erreur de création de verif.txt");
     
    #######################################################################################################
    #                         		           Tableau                                                    #
    #######################################################################################################	
     
    $tab[0][0]="Fichier : ";
    $tab[0][1]="Niveau : ";
     
    #tant qu'il y a des lignes dans mon fichiers
    sub boucle();
    sub boucle(){	
    	while ( my $ligne = <FICHIER>) {
    		$cpt++;
    		if (( $ligne =~ m/^^@@\s*(.+)/ ) || 
    			( $ligne =~ m/^^@\s*(.+)/ ) ||
    			( $ligne =~ m/^^sta\s*(.+)/ )){
    				my ($NomFichier, $niv) = @_; # paramètres de la fonction boucle()
    				$tab_nb++;
    				$NomFichier =  $1;
    				$tab[$tab_nb][0]=$NomFichier;
    				$tab[$tab_nb][1]=$niv;
    			boucle();
    			open FICHIER, '<', $NomFichier or die("$NomFichier non existant : $!\n");
    		}		
    	}	
    }
    boucle();
     
    #######################################################################################################
    #                                           Ecriture                                                  #
    ####################################################################################################### 
     
    for ($y=0; $y<=$tab_nb; $y++){
    	for ($j=0; $j<=1; $j++){
    		print ECRIRE $tab[$y][$j], "\			";
    	}
    	print ECRIRE "\n";
    }
     
    #######################################################################################################
    #                                         Compteur                                                    #
    #######################################################################################################
     
        print "-------------------------------------------------------\n";
        print "|---- parserAction.pl a parcouru ", $cpt, " lignes ----|\n";
        print "-------------------------------------------------------\n";
     
    #######################################################################################################
    #                            Fin du traitement et Fermeture Fichiers                                  #
    #######################################################################################################
     
        print "-------------------------------------------------------\n";
        print "|----------------  Fin du traitement  ----------------|\n";
        print "-------------------------------------------------------\n";
     
    close FICHIER;
    close ECRIRE;

  5. #5
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Par défaut
    Quand je t'ai dis plus haut, je laisser tomber les prototypes, il faut laisser tomber TOUT le prototype. Le fait d'ajouter () après la déclaration (ou la définition d'une fonction) crée implicitement un prototype correspondant à une fonction SANS paramètre.

    Tu dois donc supprimer ces parenthèses !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    sub boucle;
    sub boucle {
      ...
    }
    comme je l'avais indiqué dans mon message précédent.

  6. #6
    Expert confirmé

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    3 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Avril 2009
    Messages : 3 577
    Par défaut
    Par ailleurs, tu ré-utilises un descripteur de fichier FICHIER dans ta boucle alors qu'il est cours d'utilisation. Tu ne pourras donc pas revenir au descripteur antérieur au retour de la fonction.
    Comme dit Dimitry.e, tu dois ouvrir le fichier juste avant while, et tu dois aussi impérativement utiliser un descripteur de fichier de portée limitée (un typeglob étant toujours global, il ne convient pas d'utiliser ce type de descripteur de fichier, mais d'utiliser à la place une variable scalaire définie par my) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    sub boucle {
      open my $FICHIER, "<", "fichier.txt" or die "Can't open fichier.txt: $!\n";
      while (my $ligne = <$FICHIER>) {
        ...
        boucle(...);
      }
    }
    (ou quelque chose dans le genre)

  7. #7
    Membre expérimenté

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2011
    Messages
    184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 184
    Par défaut
    Tu essaies de lire un descripteur qui n'existe pas. Il faut que tu places la ligne avec open FICHIER avant ta boucle while.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de syntaxe avec les fonctions CALLBACK
    Par nonozor dans le forum C++
    Réponses: 4
    Dernier message: 21/07/2008, 18h06
  2. Réponses: 2
    Dernier message: 11/04/2008, 22h58
  3. [MySQL] Problème de syntaxe avec variable utilisée dans fonction PHP et MySQL
    Par redvivi dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 19/03/2008, 21h07
  4. Réponses: 2
    Dernier message: 03/10/2007, 22h29
  5. problème de syntaxe pour une fonction
    Par gero123 dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 27/07/2007, 12h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo