sad723 has asked for the wisdom of the Perl Monks concerning the following question:

hello every body i'm a beginner in perl; can you help me to solve this problem:

#!C:/Perl/bin/perl.exe -w #!/usr/bin/perl print "listes associatives \n"; %url=("fd"="www.forumdz.com", "ok"="www.ouedkniss.com", "se"="www.sec4ever.com"); #----------------------------- print "liste des URL \n"; foreach $val (%url){ print $val."\t"; }

Unquoted string "p" may clash with future reserved word at C:\Perl\bin\hash.pl ine 4. Bareword found where operator expected at C:\Perl\bin\hash.pl line 4, near "" (Missing operator before r?) Unquoted string "r" may clash with future reserved word at C:\Perl\bin\hash.pl ine 4. Bareword found where operator expected at C:\Perl\bin\hash.pl line 4, near "" (Missing operator before i?) Unquoted string "i" may clash with future reserved word at C:\Perl\bin\hash.pl ine 4. Bareword found where operator expected at C:\Perl\bin\hash.pl line 4, near "" (Missing operator before n?) Unquoted string "n" may clash with future reserved word at C:\Perl\bin\hash.pl ine 4. Bareword found where operator expected at C:\Perl\bin\hash.pl line 4, near "" (Missing operator before t?)

i found some thing like this on this forum but can't understand ; ms-dos batch file !!!?? thank's

Replies are listed 'Best First'.
Re: begin with perl
by CountZero (Bishop) on Oct 30, 2011 at 10:49 UTC
    Bonjour et bienvenue dans notre Monastère!

    These are my comments on your little script.

    I gather you run this on some Windows version, so the first two lines are not really necessary. Windows applies another mechanism to link your script to the Perl interpreter. It is far better to replace these by the following:

    use strict; use warnings;
    or, if you use a recent version of Perl:
    use Modern::Perl;
    It has the same effect, but also activates some "modern" functions and commands, such as say.

    Please note that once you have activated the strict mode, you have to use lexical "my" variables throughout your script or you have to fully qualify your global variables.

    Next, the separator between the key and the value of your hash elements is not "=", but either a simple comma "," or the "fat comma" "=>". The fat comma automatically quotes your key value (provided it is only one word long).

    To get the values of your hash, you use the values keyword, to get the keys of the hash, you use the keys keyword.

    Applying the above, we get:

    use Modern::Perl; say 'listes associatives'; my %url = ( fd => "www.forumdz.com", ok => "www.ouedkniss.com", se => "www.sec4ever.com" ); #----------------------------- say 'liste des URL'; foreach my $val (values %url) { print "$val\t"; }
    If you wish to extract both keys AND values in one operation, you use the each keyword in a while-loop.
    say 'liste des abbreviations et URL'; while (my ($key, $ val) = each %url) { print "$key: $val\t"; }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      when i test your code

      use Modern::Perl; say 'listes associatives'; my %url = ( fd => "www.forumdz.com", ok => "www.ouedkniss.com", se => "www.sec4ever.com" );

      i get this error with cygwin :

      sad723@sad723-PC ~/perl $ ./hash.pl ./hash.pl: ./hash.pl: cannot execute binary file

      and this error when i use cmd;

      C:\Perl\bin>hash.pl Unrecognized character \xE3 at C:\Perl\bin\hash.pl line 2.

        Unicode character \xE3 is 'Latin small letter a with tilde'.

        When I run your posted code, I don't get any error message. Are you sure there's no tilde on the 'a' in 'listes associatives' in your original?

        What Perl are you using?

        I have tried it on Strawberry Perl 5, version 12, subversion 1 (v5.12.1) and I have saved my script as ASCII, UTF-8, UTF-16 (both Big-endian and Little-endian), with and without a BOM (Byte Order Mark) and it all works without error.

        Perhaps there is something wrong with your set-up.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: begin with perl
by ikegami (Patriarch) on Oct 30, 2011 at 10:31 UTC

    I'm guessing the file is encoded using UTF-16, and that your Perl doesn't support UTF-16 source files. (Mine seems to, but maybe that's something ActiveState adds to their Perl???) Save it as UTF-8 instead, then tell Perl it's UTF-8 using use utf8;.

    By the way, it doesn't make sense to have two #! lines. Furthermore, the #! sequence has to be the first two characters of the file, and it's not in your case.

      encoding - allows you to write your script in non-ascii or non-utf8

        Unless you load it via perl -Mencoding=UTF-16le, Perl can't read the encoding pragma to process it.

        Furthermore, "encoding" has the nasty side-effect of breaking \x## sequences in string literals.

Re: begin with perl
by ramprasad27 (Sexton) on Oct 30, 2011 at 10:34 UTC

    You need to declare hash properly. Please study basic tutorial on hashes. Hash declaration is incorrect