in reply to Package variables and Parse::RecDescent

Two niggly comments to add to the existing correct explanations of your problem:

my $parser = new Parse::RecDescent($grammar);
Is a bad meme in Perl. I know its in the docs (it shouldnt be, even TheDamian has commented on why its a bad idea) but use the unambiguous and easier to read (IMO)
my $parser = Parse::RecDescent->new($grammar) or die "Failed to create + parser.\n";
and you'll one day save yourself some bizarre debugging. Note that checking the return of a constructor for an object you didnt write is usually a good idea.

The other is that while q§ ... §; sure is funky looking, it is not as suitable as using HereDoc quoting when using PRD and embedded code:

my $grammar=<<'END_OF_GRAMMAR'; ... END_OF_GRAMMAR my $parser = Parse::RecDescent->new($grammar) or die "Failed to create + parser.\n";
Personally I tend to roll them into one:
my $parser = Parse::RecDescent->new(<<'END_OF_GRAMMAR') or die "Failed + to create parser.\n"; ... END_OF_GRAMMAR
The main reason that <<'END_OF_GRAMMAR' is better is that it is the only normal Perl quoting construct that does not require backslashes or quotes to be escaped. Which in turn can greatly simplify the embedded code that you have in your grammar.

HTH

--- demerphq
my friends call me, usually because I'm late....

Replies are listed 'Best First'.
Re: Re: Package variables and Parse::RecDescent
by castaway (Parson) on Jan 07, 2003 at 22:03 UTC
    You're right about the 'new Parse::RecDescent' stuff, I've read somewhere it's better the other way around. Though personally I don't like ' .. or die .. ' since usually I want to continue afterwards with some sort of warning. (My program works without the parser, if its not available). So I usually do 'my $parser = ...' and then check the result afterwards.

    The HereDioc stuff sounds like a better idea than searching for funky characters that won't appear in the text, I'll give it a whirl.
    Thanks,
    C.

Re: Re: Package variables and Parse::RecDescent
by castaway (Parson) on Jan 08, 2003 at 13:52 UTC
    Just another small niggle. According to my camel (page 63), quoting as I'm doing using q§ .. §; (or any other character), doesn't interpolate. In my embedded code I'm using quotes, double quotes, variables etc. without problems.
    C.
      Er. Not sure this ones valid. :-)

      My point was mostly about escaping back slahes or a potential § somewhere in your code. For instance

      E:\blead> perl -e "print q§/\\\./§" /\\./
      So the regex that starts off being "match a backslash followed by a period" becomes "match a backslash followed by any non newline character". Not exactly the same thing. Whereas
      #!perl print <<'ENDOFHERE'; /\\\./ ENDOFHERE
      Outputs the expected /\\\./

      For this reason, I tend to do my best to avoid using the q// operator when Im trying to quote code. After all you never know when you'll stick a double backslash into the code, and it might save you hours of debugging trying to figure what the problem was, wheras the HEREDOC notation is only slightly more characters to write, with virtually no chance of this happening.

      --- demerphq
      my friends call me, usually because I'm late....