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

Oh wise ones, I humbly beseech you...
I have a subroutine that conatins many unless statements (Seven to be exact), so with the left and right curly brackets for those and the ones for the begining and end of the sub that makes eight of each in total. They are all there, I have counted several times. : )
Despite the apparent matching of brackets, I still get the unmatched right curly bracket... error, on the line right at the end of the sub routine. I am running ActiveState Perl 5.61 on a win32 box, I have also tried it on a Linux box running the same version to the same result. What am I missing? Has anyone else experienced something similar?
I would post the offending sub routine but it is 200 odd lines long. It also contains a lot of HERE documents, as I have heard them called. For the unless commands, I have not included else {}, because I just want it to continue processing the sub. Perhaps this is my problem?
Would I be better off to reverse the logic and go with if.... else{} statements instead?
Any suggestions / comments much appreciated. If you require more information, just ask, I should be sitting here for a while yet...
Much appreciated,

Gerard

Replies are listed 'Best First'.
Re: Unmatched right curly bracket ...
by Biker (Priest) on Feb 27, 2002 at 11:04 UTC

    Are you building complex strings?

    You may have an unmatched string, like starting with a double quote and ending what you think is the same string with a single quote.


    Everything will go worng!

      Possibly, but usually an unmatched single quote gives rise to the confusing:

      Bad name after %s::
      message with 5.6.1

      /J\

Re: Unmatched right curly bracket ...
by simon.proctor (Vicar) on Feb 27, 2002 at 11:10 UTC
    Perhaps if you put your code on your scratchpad? Then we could take a look at it.

    I'll take a peek if you message me (or reply here) that you have put it there.


    UPDATE
    Heres my code which appears to work. Note that I have quoted the ENDHTML bits. Take a look at perlfaq4:
    Check for these three things: There must be no space after the << part. There (probably) should be a semicolon at the end. You can't (easily) have any space in front of the tag


    And the code:
    use strict; use warnings; use vars qw($home_fax $Company $position $company_address1 $company_po +stal1 $company_phone $company_fax); $home_fax = "a"; $Company = "b"; $position = "c"; $company_address1 = "d"; $company_postal1 = "e"; $company_phone = "f"; $company_fax = "g"; section2(); sub section2 { print <<"ENDHTML"; one ENDHTML unless ($home_fax eq "") { print <<"ENDHTML"; two ENDHTML } print <<"ENDHTML"; three ENDHTML unless ($Company eq "") { print <<"ENDHTML"; four ENDHTML } unless ($position eq "") { print <<"ENDHTML"; five ENDHTML } unless ($company_address1 eq "") { print <<"ENDHTML"; six ENDHTML } unless ($company_postal1 eq "") { print <<"ENDHTML"; seven ENDHTML } unless ($company_phone eq "") { print <<"ENDHTML"; eight ENDHTML } unless ($company_fax eq "") { print <<"ENDHTML"; nine ENDHTML } print <<"ENDHTML"; ten ENDHTML }
    Which prints:
    C:\WINNT\PROFILES\simonp\DESKTOP>perl test.pl one two three four five six seven eight nine ten C:\WINNT\PROFILES\simonp\DESKTOP>
    I hope that helps :).
      Here is the code, I decided to take the Html out to make it less unwieldly...
      sub section2 { print <<ENDHTML; # Html goes in here ENDHTML unless ($home_fax eq ""){ print <<ENDHTML; # Html goes in here ENDHTML } print <<ENDHTML; # Html goes in here ENDHTML unless ($Company eq "") { print <<ENDHTML; # Html goes in here ENDHTML } unless ($position eq "") { print <<ENDHTML; # Html goes in here ENDHTML } unless ($company_address1 eq "") { print <<ENDHTML; # Html goes in here ENDHTML } unless ($company_postal1 eq ""){ print <<ENDHTML; # Html goes in here ENDHTML } unless ($company_phone eq "") { print <<ENDHTML; # Html goes in here ENDHTML } unless ($company_fax eq "") { print <<ENDHTML; # Html goes in here ENDHTML } print <<ENDHTML; # Html goes in here ENDHTML }
      Cheers, Gerard.
        Kill the spaces after each ending tag ENDHTML, because after ENDHTML there must immediately follow a \n; otherwise it is not interpreted as the end of a HERE-Document and an error like this occurs...

        Best regards,
        perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

        I know I am not addressing your question, but why use unless when you are checking for truth values?

        I'd rather use if:

        if ($company_fax) { .... }
        instead of:
        unless ($company_fax eq "") .... }
        /prakash
Re: Unmatched right curly bracket ...
by gellyfish (Monsignor) on Feb 27, 2002 at 11:06 UTC

    I know its like shutting the stable door and everything but perhaps you ought to be using an editor like vim that lets you match the brackets

    /J\

Re: Unmatched right curly bracket ...
by erikharrison (Deacon) on Feb 28, 2002 at 02:36 UTC

    I'm not sure from the preceeding replies that you have solved the problem . . .but even if you have this is good to keep in mind.

    I once stared at this subroutine for *5* hours trying to figure out where the missing curlie was . . .and eventually I found it. My problem? A missing curlie several hundred lines before - because my subs are generally at the end of a file, perl managed to parse the whole thing not finding the "missing curlie" error until the last couple of lines. Using the debugger would have helped had I known at the time how to use it. But keep in mind that, like run away lines and strings, missing curlies often creat errors at a distance - far more distance than any other error I've encountered.

    Cheers,
    Erik
      Yikes. 5 hours! In the interest of saving you the hassle next time, might I recommend perltidy? The perltidy documentation shows how it helps find mis-matched braces at: An example of finding a nesting error (here it is in googles cache since the above link isn't working for me at the moment.)

      -Blake

      Thanks all for your comments. I manageed to solve it in the end, by removing my sloppy, trailing spaces. Erik, I did already think of that, I ran the subroutine as seperate file altogether and got the same error, which is how I knew it was in there. Thanks anyway.

      Regards,
      Gerard.