in reply to howto parse (or determining end) of a line of perl

My solution for my REPL is very simple; I use two consecutive semicolons to indicate the end of input; and I don't evaluate the input until I see it.

I just accumulate input lines, removing any newlines until I see my ad-hoc end-of-input indicator and then evaluate the entire expression. It is far simpler than trying to determine whether what you've got so far constitutes a complete expression.


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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: howto parse (or determining end) of a line of perl

Replies are listed 'Best First'.
Re^2: howto parse (or determining end) of a line of perl
by perl-diddler (Chaplain) on Aug 25, 2016 at 01:48 UTC
    How would that be different than simply:
    home> perl ... ... ^d
    I.e. perl reads from stdin until you press the end-of-file char (on *nix, ^d), then it would eval everything at once.

    Remember, by default, I'm just doing 1 line at a time (i.e. "calculator mode")...

      Maybe this clarifies things?

      C:\test>p1 [0]{} Perl> print 123 * 346;; 42558 [0]{} Perl> print 123 * 456; print 'pqr';; 56088 pqr [0]{} Perl> sub xyz{ my( $a, $b ) = @_; my $c = $a + $b; $c += $a * $b; $c += $a - $b; $c += $a / $b; return $c ** 2; };; [0]{} Perl> print xyz( 123, 456 );; 3173549946.78328 [0]{} Perl> Terminating on signal SIGINT(2) C:\test>

      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". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Wasn't unclear. Just that it's similar to the invoking an instance of perl to do the same except that it saves context, while up, more like my calc does (which is limited to 1 line).

        It would be closer to what I want if you "assumed" 1 semi -- see if that parses. If so, goto next. If not, then this is where that PPI module could be useful -- run the parse w & w/o the semi and see if it only can work the 2nd way -- if so, ask for another line. If it can work w/a semi- default to closing the statement unless a backslash is on the end.

        Of course if it works with or w/o a semi (like after a closing '}'), then you goback to ask for a new line.

        Yours is more of a REPL (gee, wonder why!;-)), while mine was intended to print results like a calculator -- used to do a better job until I 'fixed' it some time back -- and haven't fixed it again -- printed the hex, string and fractional formats if they made sense.

        BTW -- when I say 'calc', I really mean evaluate a perl expression and print the result, which was sufficient for my purposes.

        It also stopped working right when my distro updated one of the Term::readchar/line routines and now it thinks it is talking to a dumb tty... real annoying, but those are 'transitory issues'... It's not something I use too much -- just now and then and now and then make changes in it -- and the "itch" of it not handling multiple lines like bash would, got "itchier", sorta, if that makes any sense.

        Maybe going through the extra pain of the trying dual-parsing w/PPI and "with", and "with/out" semi's to determine whether to close input and return a result might be the shortest path to get where I might want to go, but that's too much work for a short scratching session and got bigger fish to fry before I can play in my playground...*sigh*.

Re^2: howto parse (or determining end) of a line of perl
by Discipulus (Canon) on Aug 25, 2016 at 07:43 UTC
    is BrowserUk's REPL available somewhere? Let's say for testing... ;=)

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      No.


      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". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

      If you search CPAN for REPL and you will find many REPLs that people are willing to share with you.