Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Solved! I would have expected a syntax error here

by roboticus (Chancellor)
on Oct 19, 2018 at 16:05 UTC ( #1224342=perlquestion: print w/replies, xml ) Need Help??

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

Thanks, guys! That was a quick resolution. Indirect Object syntax was what tripped me up. As haukex suggests, I'll have to start using the Yadda, Yadda operator ... to ensure that I don't miss that in the future.

Using that operator and a comment, I can either let the program run and execute and die only if I reach unfinished code:

sub find_symbol { ... # got to here today! (20180520) }
$ perl xyzzy.pl Foo! Unimplemented at xyzzy.pl line 13.

Or if I'd rather have the program just fail without running, I can prefix my original comment with the operator (so long as I don't use a semicolon as the first character in my comment:

sub find_symbol { ... got to here today! (20180520) }
$ perl xyzzy.pl syntax error at xyzzy.pl line 12, near "... got to " Execution of xyzzy.pl aborted due to compilation errors.

Really, though, not one Monty Python reference? 8^)

Original post follows:


Hello, gang:

When I write experimental bits of code and don't get a chance to finish, I'll frequently mark my position with a few words and no comment marker with the intent that if I run the program, perl will tell me that bit(s) of my code are unfinished, and let me know where the unfinished bits are. This morning I decided to adapt a working program I've been using all week to add some new functionality, and when I looked at the code, I saw this bit:

sub find_symbol { got to here today! (20180520) }

What the heck? The code is working, but that subroutine just can't be right. So I looked over the program--It doesn't look like the code is commented out anywhere (I normally use "=h1 foo" and "=cut" to comment out blocks of code I don't want to use). I ran:

$ perl -c LTSpice_to_graph.pl LTSpice_to_graph.pl syntax OK

OK, then, *something* has to be masking this syntax error, right? So I copied the code to xyzzy.pl and started removing chunk after chunk, eventually coming up with:

$ cat xyzzy.pl #!env perl use strict; use warnings; find_symbol(); sub find_symbol { got to here today } $ perl -c xyzzy.pl xyzzy.pl syntax OK $ perl -MO=Concise xyzzy.pl 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 5 xyzzy.pl:5) v:*,&,{,x*,x&,x$,$ ->3 5 <1> entersub[t2] vKS/TARG,STRICT ->6 - <1> ex-list K ->5 3 <0> pushmark s ->4 - <1> ex-rv2cv sK/STRICT,1 ->- 4 <#> gv[*find_symbol] s/EARLYCV ->5 - <;> ex-nextstate(main 7 xyzzy.pl:10) v:*,&,{,x*,x&,x$,$ ->6 xyzzy.pl syntax OK

So consider me baffled, I don't know what's going on here. Can anyone tell me what I'm not seeing?

For the record:

$ perl -version This is perl 5, version 26, subversion 2 (v5.26.2) built for x86_64-cy +gwin-threads-multi (with 7 registered patches, see perl -V for more detail)

Interesting ... one more bit of information before I hit the preview button. I changed the contents of the subroutine to:

sub find_symbol { I got to here today }

and now I get what I expected to see:

$ perl -c xyzzy.pl Bareword "today" not allowed while "strict subs" in use at xyzzy.pl li +ne 8. xyzzy.pl had compilation errors.

Does anyone know what's happening here? My only (ill-informed) guess would be something to do with parsing the GOTO statement.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re: I would have expected a syntax error here
by haukex (Bishop) on Oct 19, 2018 at 16:11 UTC

    B::Deparse can help:

    $ perl -wMstrict -MO=Deparse sub find_symbol { got to here today } __END__ BEGIN { $^W = 1; } use strict; sub find_symbol { 'to'->got('today'->here); } - syntax OK $ perl -wMstrict -MO=Deparse sub find_symbol { I got to here today } __END__ Bareword "today" not allowed while "strict subs" in use at - line 2. - had compilation errors. BEGIN { $^W = 1; } use strict; sub find_symbol { 'got'->I('here'->to('today')); }

    So the first one is just the Indirect Object Syntax, twice, while the second one assumes an extra parameter, and when that's a number it's fine; when it's a bareword it's not (under strict, at least). It just depends on whether the number of barewords is even or odd :-)

    $ perl -wMstrict -MO=Deparse -e 'sub x { foo bar }' BEGIN { $^W = 1; } use strict; sub x { 'bar'->foo; } -e syntax OK $ perl -wMstrict -MO=Deparse -e 'sub x { foo bar quz }' Bareword "quz" not allowed while "strict subs" in use at -e line 1. -e had compilation errors. BEGIN { $^W = 1; } use strict; sub x { 'bar'->foo('quz'); } $ perl -wMstrict -MO=Deparse -e 'sub x { foo bar quz baz }' BEGIN { $^W = 1; } use strict; sub x { 'bar'->foo('baz'->quz); } -e syntax OK $ perl -wMstrict -MO=Deparse -e 'sub x { foo bar quz baz bleep }' Bareword "bleep" not allowed while "strict subs" in use at -e line 1. -e had compilation errors. BEGIN { $^W = 1; } use strict; sub x { 'bar'->foo('baz'->quz('bleep')); }

    None of the code actually runs, of course :-)

    BTW, maybe you want to use the "Yada Yada operator" (...) for unfinished code? (Perl v5.12 and up)

      haukex:

      Yeah, I'll have to start using that operator to ensure that I don't run into this again!

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re: I would have expected a syntax error here
by choroba (Archbishop) on Oct 19, 2018 at 16:09 UTC
    Indirect object syntax:
    perl -MO=Deparse 1.pl use warnings; use strict; find_symbol(); sub find_symbol { use warnings; use strict; 'to'->got('today'->here(!1)); } 1.pl syntax OK

    No argument to here if you remove the !(20180520).

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      To elabotate further:

      the odd words to today are considered packages, the even ones got here methods.

      Both are not required to exist at compile time.

      It's a variation of new Class arg-list just twice.

      Hence got To (here Today () ) (capitalizing classes for clarity)

      Nb: If the bare words where already pre defined subs, Perl would parse them differently.

      I see two solutions:

      • a possible pragma to forbid indirect method calls (becoming standard in Perl 7)
      • a module which is parsing the op-tree at the CHECK phase warning about undefined packages. (dynamic package creation at runtime is rare)

      the latter could be implemented right away.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        indirect already exists on CPAN:

        >perl -M-indirect -wle "got to here today! (20180520)" Indirect call of method "here" on object "today" at -e line 1. Indirect call of method "got" on object "to" at -e line 1. Can't locate object method "here" via package "today" (perhaps you for +got to load "today"?) at -e line 1.
        >perl -wle "got to here today! (20180520)" Can't locate object method "here" via package "today" (perhaps you for +got to load "today"?) at -e line 1.

        You can put it either into PERL5OPT or just at the top of your code:

        no indirect;
Re: I would have expected a syntax error here
by Corion (Pope) on Oct 19, 2018 at 16:10 UTC

    I wouldn't have expected that, but B::Deparse tells me this is valid Perl:

    perl -MO=Deparse -e "sub find_symbol { got to here today! (20180520)}" sub find_symbol { 'to'->got('today'->here(!1)); }

    The !(20180520) deparses to a constant...

Re: I would have expected a syntax error here
by tybalt89 (Parson) on Oct 19, 2018 at 16:17 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1224342 use strict; use warnings; sub find_symbol { got to here today! (20180520) } find_symbol(); print "it ran!!\n"; # even runs with a little extra code :) sub today::here {} sub to::got {}
Re: Solved! I would have expected a syntax error here
by ikegami (Pope) on Oct 20, 2018 at 02:01 UTC
Re: Solved! I would have expected a syntax error here
by perldigious (Priest) on Oct 19, 2018 at 17:45 UTC

    It's a bit off topic, but as an Electronics Engineer who often uses LTSpice, I saw...

    LTSpice_to_graph.pl

    ... and was intrigued... Watcha' workin' on over there roboticus? :-)

    Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.

      perldigious:

      At the moment, I'm just working on a tool that lets me interoperate between LTSpice and OmniGlyph. Since both use text file formats they ought to be easy enough to swap between them. That way, I won't have to re-enter schematics twice. Since there are a lot of KiCAD users out there, I'd probably target that next, and make the tool be able to migrate schematic symbols and PCB footprints between them.

      I don't really have an ultimate goal, though, as I'm just puttering around with my hobbies at the moment. I'm currently decompressing from my last job and trying to figure out what I'd like to do next. It's kind of funny that I studied Electrical Engineering at school but have always worked as a programmer. Though knowing my way around an oscilloscope has been helpful in quite a few jobs, as I really enjoy working on embedded systems.

      (I realize that OmniGlyph is a niche, and not particularly well known, but I find it more convenient to use than other packages. It not only uses the same file format for schematics and PCBs, but it's very keyboard friendly. I tend to think of it as the VI of electronics. It also happens to be the tool of choice for my electronics-oriented friends.)

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Nice, Perl is pretty awesome for building tools like that, and yes KiCAD has picked up a lot of popularity lately. I've been considering puttering around with KiCAD tools as well along with some component distributor APIs for part info/ordering integration, but a lot of people who are a lot better programmers than me have been doing that already and making some very cool stuff.

        Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-07-06 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?