Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Matching first Perl statement.

by talexb (Chancellor)
on Jan 15, 2002 at 01:42 UTC ( [id://138719]=note: print w/replies, xml ) Need Help??


in reply to Matching first Perl statement.

I'd suggest working from the back of the statement, stripping off the trailing comment (if it exists) until you find a semi-colon. Munch the leading white space, then you're done -- for the trivial cases.

For a full-featured, robust solution, you will most likely need to use Parse::RecDescent or something else.

This is, of course, a dangerous question to answer because a problem is posed without much definition of how rigorous the answer should be. Caveat emptor.

--t. alex

"Of course, you realize that this means war." -- Bugs Bunny.

Replies are listed 'Best First'.
Re: Re: Matching first Perl statement.
by $code or die (Deacon) on Jan 15, 2002 at 03:49 UTC

    I'd suggest working from the back of the statement, stripping off the trailing comment (if it exists) until you find a semi-colon. Munch the leading white space, then you're done -- for the trivial cases.
    If you know where the end of the statment it, then it would have been a trivial question ;) The problem is that you can't guess where the statement ends. Even stripping commments is not a trivial task. Consider the following line:

    my $foo=';'; $foo =~ s#;##g; #comments start here

    As you can see this would be tricky. I use '#' as regex delimters quite a lot. There are so many special cases in perl that you can't easily isolate things like "strip comments" or "first statement" into simple parsers.

    For the origial poster, I would suggest looking at perlTidy for some ideas.



    Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://138719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found