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

mail question and book, and code

by Raymond (Novice)
on May 06, 2013 at 14:42 UTC ( #1032312=note: print w/ replies, xml ) Need Help??


in reply to Re^3: mail question and book
in thread mail question and book

#!/usr/bin/perl use strict; use warnings; print "How old are you?"; $age = <>; print "What is your favorite color?"; $color = <>; print "You are $age, and your favorite color is $color.";
Global Symbol "$age" requires explicit package name at agecolor.pl lin +e 5 Global Symbol "$color" requires explicit package name at agecolor.pl l +ine 7 Global Symbol "$age" requires explicit package name at agecolor.pl lin +e 8 Global Symbol "$color" requires explicit package name at agecolor.pl l +ine 8 Execution of agecolor.pl aborted due to compilation errors.
#!/usr/bin/perl use strict; use warnings; print "What is the radius of the circle?"; chomp ($r = <>); $diameter = (2 * $r); $area = (3.14 * ($r ** 2)); $cir = ($diameter * 3.14); print "Radius: $r\n Diameter: $diameter\n Circumference: $cir\n Area: +$area";
Global symbol "$r" requires explicit package name at diameter.pl line +5. Global symbol "$diameter" requires explicit package name at diameter.p +l line 6. Global symbol "$r" requires explicit package name at diameter.pl line +6. Global symbol "$area" requires explicit package name at diameter.pl li +ne 7. Global symbol "$r" requires explicit package name at diameter.pl line +7. Global symbol "$cir" requires explicit package name at diameter.pl lin +e 8. Global symbol "$diameter" requires explicit package name at diameter.p +l line 8. Global symbol "$r" requires explicit package name at diameter.pl line +9. Global symbol "$diameter" requires explicit package name at diameter.p +l line 9. Global symbol "$cir" requires explicit package name at diameter.pl lin +e 9. Global symbol "$area" requires explicit package name at diameter.pl li +ne 9. Execution of diameter.pl aborted due to compilation errors.
It took me 2 days to amke this topic: ______________________________

My list from perl cookbook is named:

1.1slowcat.pl

1.2randcap.pl

1.3wrapdemo.pl

this code does not work, and i ask for help

#!/usr/bin/perl -w use strict; use warnings; # slowcat - emulate a slow line printer # usage: slowcat [-DELAY] [files ...] $DELAY = ($ARGV[0] =~ /^-([.\d]+)/) ? (shift, $1) : 1; $| = 1; while (<>) { for (slit(//)) { print; select(undef,undef,undef, 0.005 * $DELAY); } }
errors:
Global symbol "$DELAY" requires explicit package name at 1.1slowcat.pl + line 6. Global symbol "$DELAY" requires explicit package name at 1.1slowcat.pl + line 11. Execution of 1.1slowcat.pl aborted due to compilation errors.
------------------------------
#!/usr/bin/perl -p # randcap: filter to randomly capitalize 20% of the letters # call to srand() is unnecessary in 5.004 BEGIN { srand(time() ^ ($$ + ($$ << 15))) } sub randcase { rand(100) < 20 ? "\u$_[0]" : "\l$_[0]" } s/(\w)/randcase($1)/ge;
% randcap < genesis | head -9
errors:
Semicolon seems to be missing at 1.2randcap.pl line 5. syntax error at 1.2randcap.pl line 6, near "letters # call to srand() is unnecessary in 5.004 BEGIN " syntax error at 1.2randcap.pl line 10, near ";}" Execution of 1.2randcap.pl aborted due to compilation errors.
------------------------------------
#!/usr/bin/perl -w use strict; use warnings; # wrapdemo - show how Text::Wrap works @input = ("Folding and splicing is the work of an editor, ", "not a mere collection of silicon", "and", "mobile electrons!"); use Text::Wrap qw($columns &wrap); $colums = 20; print "0123456789" x 2, "\n"; print wrap(" ", " ", @input), "\n";
errors:
Global symbol "@input" requires explicit package name at 1.3wrapdemo.p +l line 6. BEGIN not safe after errors--compilation aborted at 1.3wrapdemo.pl lin +e 11.

I hope it wasn't too much, but still i count with the help, of perlmonks and friends. Best Regards Ray


Comment on mail question and book, and code
Select or Download Code
Re^5: mail question and book
by marto (Bishop) on May 06, 2013 at 14:52 UTC

    You need to declare variables when you have use strict; see strict. Also when reading from they keyboard you need to use chomp:

    #!/usr/bin/perl use strict; use warnings; print "How old are you?\n"; chomp (my $age = <>); print "What is your favorite color?\n"; chomp (my $color = <>); print "You are $age, and your favorite color is $color.\n";

    Note that the second example is using chomp already, but missing declorations:

    #!/usr/bin/perl use strict; use warnings; print "What is the radius of the circle?\n"; chomp (my $r = <>); my $diameter = (2 * $r); my $area = (3.14 * ($r ** 2)); my $cir = ($diameter * 3.14); print "Radius: $r\n Diameter: $diameter\n Circumference: $cir\nArea: $ +area\n";

    References for learning Perl:

Re: mail question and book, and code
by Laurent_R (Parson) on May 06, 2013 at 17:58 UTC

    Yes, this is what I told you. They provide only short code snippets to show how to solve a specific problem, but usually don't give full programs. Because of that, they suppose that the variables have been declared earlier, it is up to you to do it (usually with the 'my' function) when you use the code snippets to write an actual full program.

    In the case of the randcap program, you need to comment out the first line, because it is a comment, not a line of code.

    Example of possible corrections to be made for the 1st program:

    #!/usr/bin/perl use strict; use warnings; print "How old are you?"; my $age = <>; print "What is your favorite color?"; my $color = <>; print "You are $age, and your favorite color is $color.";

    Just adding the "my" function for the two variables should remove your compile time errors.

      Thanks it's a new begining, and i don't have the Art to debug, as some of you have, in this post the 2 firts programs are solved, there are 3 more pls. Iam trying to get a collection of programs, that a person practise it, and copied those same programs one person can learn how to Perl. If someone knows a site with lots of samples, don't be shy, tell me.

        If you had bothered to click the links I provided you'd have all the resources you need to get started.

        OK, for the third, probably one single little change: declare $DELAY with the my function:

        #!/usr/bin/perl -w use strict; use warnings; # slowcat - emulate a slow line printer # usage: slowcat -DELAY files ... my $DELAY = ($ARGV[0] =~ /^-(.\d+)/) ? (shift, $1) : 1; $| = 1; while (<>) { for (slit(//)) { print; select(undef,undef,undef, 0.005 * $DELAY); } }

        The same correction will apply to most of your other problems.

        And BTW, yes, I was fortunate enough to be born with the art of debugging Perl, I was doing that all the time in my cradle or while drinking milk from the breast of my mother (even though this was 30 years before Larry Wall invented Perl, but I knew it all already), but if it is not your case, too bad, you'll have to learn it the hard way: by doing it yourself again and again. You can get some help, and I am ready to help you again on the programs you have shown, but at one point, you really have to do it yourself.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-12-25 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls