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

I am working with a script for a perl mud located at Boutell.com. I have added the following script to the main file that is called mudlib. When I call the function within the telnet game shell it only echos the function name and does not run the function.
subdice100 { my($me,)=@_; #A random number between 1 and 100, inclusive. $value = int(rand(100 +1); #Announce result to room. &tellRoom($objects[$me]{"location:},$me,$objects[$me]{"name"} "rolle +d $value.");
This script added without any errors, it recompiled fine. So to call this I just typed dice100 in my telnet client. Then I get dice100 that is all. Some info about the script: $me identifies the user, it is used to call subroutines. To print to a room you have to use the &tellRoom line. If you need anymore info on this script to aid me please let me know. Thank you.
Novice to Programming and Perl be kind :)

Replies are listed 'Best First'.
Re: Adding script to this base script?
by Corion (Patriarch) on Jan 05, 2008 at 16:29 UTC

    Your script cannot compile as you posted/pasted it. In the fifth line, you are missing a closing parenthesis:

    $value = int(rand(100 ) +1);

    Also, without knowing the mud environment you use, the seventh line is really quoted weird. I'm sure that the unbalanced quotes there are an error too:

    &tellRoom($objects[$me]{"location:},$me,$objects[$me]{"name"} "rolled +$value."); # ^ Quote missing? # ^ Quote missing? or ^ Comma +missing?

    Please consider whether your target environment really tells you compilation errors and how you can find out about them if not.

    If your target environment silently swallows errors, consider developing for a different target environment or consider testing your code on a local Perl installation. You will have to fake some stuff like tellRoom then, but that's actually not too hard:

    #!/usr/bin/perl -w use strict; my @objects = (); my $me = 1; $objects[$me] = { name = 'merrie' }; sub tellRoom { my ($room,$user,@msg) = @_; print "In $room: @msg\n"; }; sub hello { my ($me,) = @_; tellRoom("Here",$me,"$objects[$me]{name} says 'hello'." ); }; hello();

    But you should learn much about Perl's syntax. Counter to your expectations, whitespace is important to Perl, for example, it makes little sense in Perl to write subdice100 and to expect Perl to know that you meant sub dice100. Get a good book on Perl (via http://books.perl.org) and/or read the Perl syntax description (perlsyn).

Re: Adding script to this base script?
by Cyrnus (Monk) on Jan 05, 2008 at 17:50 UTC
    in the declaration as you listed it here there is no space between 'sub' and 'dice100'. If in your compiled code the space is also missing it may cause problems.

    If however, the space is there in the compiled code the subroutine may not be registered properly with the mud code. From my experience playing muds when you enter something not recognised as a command it simply echoes it pack to you.


    John
Re: Adding script to this base script?
by spx2 (Deacon) on Jan 05, 2008 at 20:12 UTC
    from what I remember, calling a sub with "&" in front of the
    name of the sub passes the current @_ to the arguments the
    sub called gets
    so it is unclear to me why you use & and also pass $me as parameter ...

      The leading ampersand is (almost entirely) superfluous if you pass parameters. It's safe to leave it off.