Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

syntax error - WHERE?

by davidfilmer (Novice)
on Feb 12, 2014 at 01:19 UTC ( #1074566=perlquestion: print w/ replies, xml ) Need Help??
davidfilmer has asked for the wisdom of the Perl Monks concerning the following question:

Here is a bit of code, every character of which was cut-and-pasted straight from the Data::Dumper manpage (this is a filter to reverse-sort the keys of a hash):

sub my_filter { my ($hash) = @_; return [ sort {$b <=> $a} keys %{$hash}; #Reverse sort ]; }

perl complains of a syntax error near "};" but I don't see it! My only change was tr/%$hash/%{$hash}/ (the Damian way), but it fails either way.

I seek enlightenment

Comment on syntax error - WHERE?
Select or Download Code
Re: syntax error - WHERE?
by BrowserUk (Pope) on Feb 12, 2014 at 01:22 UTC

    You have [ sort {$b <=> $a} keys %{$hash}; ];.

    Note the first semicolon (;) inside an anonymous array, which is a syntax error.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: syntax error - WHERE?
by LanX (Canon) on Feb 12, 2014 at 01:27 UTC
    you can't insert a code-block within the array-literal, hence keep the semicolon out.

    DB<106> @x= map {int rand 10 } 1..10 => (5, 5, 7, 6, 1, 5, 3, 5, 7, 7) DB<107> [ sort @x ] => [1, 3, 5, 5, 5, 5, 6, 7, 7, 7] DB<108> [ sort @x; ] syntax error at (eval 59)[multi_perl5db.pl:644] line 2, at EOF

    if you need more than a simple statement use do { BLOCK }.

    update

    NB: the comment alone is no problem, cause it's ignored while parsing and not part of any statement

    lanx@nc10-ubuntu:~$ perl -c sub my_filter { my ($hash) = @_; return [ sort {$b <=> $a} keys %{$hash} #Reverse sort ]; } - syntax OK

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: syntax error - WHERE?
by toolic (Bishop) on Feb 12, 2014 at 01:29 UTC
    every character of which was cut-and-pasted straight from the Data::Dumper manpage
    Apparently, the Data::Dumper POD has been fixed (that link is currently 5.18.0, but I don't see that code in 5.14.2 either). What version of perl do you have? Regardless, you have an answer, and the bug is no longer in the docs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1074566]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2015-07-07 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls