Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [Perl] $stringa = << "HEREDOC"

    Buona sera a tutti,
    ho comprato un libro su perl e sto iniziando a studiarlo però questo pezzo di codice che mi sta spiegando non l'ho compreso bene dal libro quindi chiedo a voi un aiutino, nel frattempo vedo se ci sono errori di sintassi nel codice.

    codice:
    use strict;
    use warnings;
    my $stringa = << "HEREDOC"
    Nel mezzo di cammin di nostra vita mi ritrovai per una selva oscura che la diritta via era smarrita
    HEREDOC
    ;
    L'esempio di codice del libro è questo:

    codice:
    my $stringa = << "HEREDOC" 
    Nel mezzo di cammin di nostra vita mi ritrovai per una selva oscura che la diritta via era smarrita 
    HEREDOC
     ;
    Potete spiegarmi l'utilizzo di HERE DOC ? perchè io non l'ho capito inoltre i 2 codici non funzionano. Il prompt dei comandi non mi da errori ma non fa spuntare niente.

    Grazie in anticipo.

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    21,188
    Dal manuale perl (perlop)
    codice:
     <<EOF   A line-oriented form of quoting is based on the shell
                   "here-document" syntax.  Following a "<<" you specify a string
                   to terminate the quoted material, and all lines following the
                   current line down to the terminating string are the value of
                   the item.  The terminating string may be either an identifier
                   (a word), or some quoted text.  If quoted, the type of quotes
                   you use determines the treatment of the text, just as in regu-
                   lar quoting.  An unquoted identifier works like double quotes.
                   There must be no space between the "<<" and the identifier,
                   unless the identifier is quoted.  (If you put a space it will
                   be treated as a null identifier, which is valid, and matches
                   the first empty line.)  The terminating string must appear by
                   itself (unquoted and with no surrounding whitespace) on the
                   terminating line.
    
                          print <<EOF;
                       The price is $Price.
                       EOF
    
                          print << "EOF"; # same as above
                       The price is $Price.
                       EOF
    
                          print << ‘EOC‘; # execute commands
                       echo hi there
                       echo lo there
                       EOC
    
                          print <<"foo", <<"bar"; # you can stack them
                       I said foo.
                       foo
                       I said bar.
                       bar
    
                          myfunc(<< "THIS", 23, <<’THAT’);
                       Here’s a line
                       or two.
                       THIS
                       and here’s another.
                       THAT
    
                   Just don’t forget that you have to put a semicolon on the end
                   to finish the statement, as Perl doesn’t know you’re not going
                   to try to do this:
    
                          print <<ABC
                       179231
                       ABC
                          + 20;
    
                   If you want your here-docs to be indented with the rest of the
                   code, you’ll need to remove leading whitespace from each line
                   manually:
    
                       ($quote = <<’FINIS’) =~ s/^\s+//gm;
                          The Road goes ever on and on,
                          down from the door where it began.
                       FINIS
    
                   If you use a here-doc within a delimited construct, such as in
                   "s///eg", the quoted material must come on the lines following
                   the final delimiter.  So instead of
    
                       s/this/<<E . ’that’
                       the other
                       E
                        . ’more ’/eg;
    
                   you have to write
    
                       s/this/<<E . ’that’
                        . ’more ’/eg;
                       the other
                       E
    
                   If the terminating identifier is on the last line of the pro-
                   gram, you must be sure there is a newline after it; otherwise,
                   Perl will give the warning Can’t find string terminator "END"
                   anywhere before EOF....
    
                   Additionally, the quoting rules for the identifier are not
                   related to Perl’s quoting rules -- "q()", "qq()", and the like
                   are not supported in place of '' and "", and the only interpo-
                   lation is for backslashing the quoting character:
    
                       print << "abc\"def";
                       testing...
                       abc"def
    
                   Finally, quoted strings cannot span multiple lines.  The gen-
                   eral rule is that the identifier must be a string literal.
                   Stick with that, and you should be safe.
    Quindi quella espressione significa:
    Carica nella stringa $stringa tutto quanto e` contenuto tra la riga successiva a "HEREDOC" alla riga precedente quella che inizia con HEREDOC.

    Per vedere qualcosa dovresti aggiungere al tuo pezzo di programma:
    print "$stringa\n";
    Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
    No domande tecniche in messaggi privati

  3. #3
    codice:
    use strict;
     use warnings; 
    my $stringa = << "HEREDOC" 
    Nel mezzo di cammin di nostra vita mi ritrovai per una selva oscura che la diritta via era smarrita 
    HEREDOC 
    print "$stringa\n";
    Così? Non funziona .

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    21,188
    Ti sei mangiato unpuntoevirgola:
    codice:
    use strict;
     use warnings; 
    my $stringa = << "HEREDOC" 
    Nel mezzo di cammin di nostra vita mi ritrovai per una selva oscura che la diritta via era smarrita 
    HEREDOC 
    ;
    print "$stringa\n";
    oppure:
    codice:
    use strict;
     use warnings; 
    my $stringa = << "HEREDOC" ;
    Nel mezzo di cammin di nostra vita mi ritrovai per una selva oscura che la diritta via era smarrita 
    HEREDOC 
    print "$stringa\n";
    Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
    No domande tecniche in messaggi privati

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.