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

Error with require LABEL;

by slinky773 (Sexton)
on Dec 19, 2013 at 22:45 UTC ( #1067893=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks. Currently, I am making this program that pretty much serves as a study guide for my math class. Unit 6 has to do with quadrilaterals, so I made another script aside from Unit6.pl called CMQIA.pl that runs the "Casio-Maxim Quadrilateral Identification Algorithm" (don't ask). I made it so that if you type "quit" into the script, it'll go back to Unit6.pl. I used:  if($choice !~ /ready/i) {require "Unit6.pl";} Since require LABEL; runs the program, I thought it would work. However, since Unit6.pl has  if($Choice =~ /crazy maxim/i) {require "CMQIA.pl";} something really weird happens now. If I run Unit6.pl and I type crazy maxim, it goes to CMQIA.pl. Then, if I type quit in CMQIA.pl, it goes back to Unit6.pl. Then, if I go to CMQIA.pl from there again, it gives me:  Can't exec "Unit6.pl": No such file or directory at CMQIA.pl line 12, <STDIN> line 3. I've tried a bunch of other things in conjunction with the original code from CMQIA.pl, but nothing ever really works. Any solutions? This is the full code. Unit6.pl
#!/usr/bin/perl -w use strict; CHOICE: print " This is Unit 6 - Quadrilaterals and Polygons.\n For the Final Exam, you will have to know the\n properties of all quadrilaterals and how to\n prove that a given shape is any quadrilateral.\n To access the Quadrilateral Information Database,\n type 'Quads.' To access the Casio-Maxim Quadrilateral\n Identification Algorithm, type 'crazy Maxim.'\n Type 'quit' to exit Unit 6.\n Type your choice.\n\n"; my $Choice = <STDIN>; chomp $Choice; if($Choice =~ /quit/i) {print "\nBye.\n\n"; exit;} if($Choice =~ /quads/i) {require "Quadrilaterals.pl";} if($Choice =~ /crazy maxim/i) {require "CMQIA.pl";}
CMQIA.pl
#!/usr/bin/perl -w use strict; ORIGINAL: print " Welcome to the Casio-Maxim Quadrilateral Identification\n Algorithm. In order to identify said quadrilateral,\n you must have the coordinates of all 4 vertices.\n"; PROMPT: print " When you are ready to input your coordines, type\n 'ready.' Type anything else to exit the CMQIA.\n\n"; my $choice = <STDIN>; if($choice =~ /ready/i) {goto MAXIM;} if($choice !~ /ready/i) {require "Unit6.pl"; system("Unit6.pl"); requi +re "Unit6.pl"; goto ORIGINAL;} MAXIM: print "\nFor shape ABCD, in which point A has coordinates (X1, +Y1), B has coordinates\n\n(X2, Y2), C has coordinates (X3, Y3), and D + has coordinates (X4, Y4),\n\nX1 = "; my $AX = <STDIN>; chomp $AX;

Replies are listed 'Best First'.
Re: Error with require LABEL;
by tangent (Vicar) on Dec 20, 2013 at 00:11 UTC
    I don't have any experience writing command line scripts but I don't think you can switch between one script and the other like the way you are trying. If I were writing this I would use modules, something like this:

    File unit6.pl

    #!/usr/bin/perl use strict; use warnings; require Unit6; require CMQIA; # require Quadrilaterals; etc Unit6::run();

    File Unit6.pm

    package Unit6; use strict; use warnings; sub run { print " This is Unit 6 - Quadrilaterals and Polygons.\n For the Final Exam, you will have to know the\n properties of all quadrilaterals and how to\n prove that a given shape is any quadrilateral.\n To access the Quadrilateral Information Database,\n type 'Quads.' To access the Casio-Maxim Quadrilateral\n Identification Algorithm, type 'crazy Maxim.'\n Type 'quit' to exit Unit 6.\n Type your choice.\n\n"; my $Choice = <STDIN>; chomp $Choice; if ($Choice =~ /quit/i) { print "\nBye.\n\n"; exit;} elsif ($Choice =~ /quads/i) { Quadrilaterals::run();} elsif ($Choice =~ /crazy maxim/i) { CMQIA::run();} } 1;

    File CMQIA.pm

    package CMQIA; use strict; use warnings; sub run { print " Welcome to the Casio-Maxim Quadrilateral Identification\n Algorithm. In order to identify said quadrilateral,\n you must have the coordinates of all 4 vertices.\n"; print " When you are ready to input your coordines, type\n 'ready.' Type anything else to exit the CMQIA.\n\n"; my $Choice = <STDIN>; chomp $Choice; if ($Choice =~ /ready/i) { maxim();} else { Unit6::run();} } sub maxim { print "\nFor shape ABCD, in which point A has coordinates (X1, Y1) +, B has coordinates\n\n(X2, Y2), C has coordinates (X3, Y3), and D ha +s coordinates (X4, Y4),\n\nX1 = "; my $AX = <STDIN>; chomp $AX; print "Y1 = \n"; my $AY = <STDIN>; chomp $AY; print "X2 = \n"; # etc } 1;
      Ohhh, I see. yeah, I looked around and whenever someone asked about require LABEL; or do LABEL; they always said to just use modules. I didn't really get what they meant, but I do now. Thanks.

        Just curious: Is  require LABEL; valid statement syntax? I don't see any mention of it in 5.8, 5.14 or 5.18 require documentation. Where do you see this syntax?

Re: Error with require LABEL;
by tangent (Vicar) on Dec 19, 2013 at 23:15 UTC
    This line in CMQIA.pl:
    if($choice !~ /ready/i) {require "Unit6.pl"; system("Unit6.pl"); requi +re "Unit6.pl"; goto ORIGINAL;}
    Should that not be:
    if($choice !~ /ready/i) {require "Unit6.pl";}
    Also, it might be safer to use if ... elsif

    Update: see revised comment below

      That skips  require "Unit6.pl"; entirely. I have no idea why. It just goes straight onto the rest of the script.

        require() skips loading the second time because it records internally which file it already has loaded and therefore doesn't need to load again. This is because require was used to load libraries in early perl versions. Later use() replaced require() because it does much more.

        But using require to start other scripts is definitely not as intended. As you have experienced.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2021-04-22 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?