in reply to Subroutine references inside of a hash with arguments.
Warnings has a runtime impact, but normally I would "use warnings" except in the case of very well debugged, high performance code and even then I would think twice about leaving warnings out!#!/usr/bin/perl use warnings; # can be simplified to just #!/usr/bin/perl -w # this will work even on Windows!! Windows Perl doesn't look # at the path, but it does look at the -w trailing thing. You will see this often at the start of a Perl program: #!/usr/bin/perl -w use strict;
I would add "use strict;". Strict is a compile time thing and has no impact upon code execution time. It enforces scoping rules as one of the main things. Your code doesn't compile under "strict".
The syntax: &sub_name is deprecated. You can just say "sub_name;" or "sub_name();, or sub_name($parm1, $parm2)".
sub menu_system($bs_index){....} is wrong in Perl.
Perl does have a prototype mechanism but is not
strongly typed (to say the least!). In any event the Perl prototype
mechanism could say something like: I require at least 3 and
possibly 4 scalar values. Calling the sub with 2 or 5 values would be
rejected during compile. Most Perl programs don't even worry about this
and for your first code, I would wouldn't either and I'm not even going
to demonstrate this syntax because I don't think you need it.
A Perl sub gets a "stack" of stuff. The Perl subroutine does what it
wants with this "stack of stuff".
Update: I forgot to add this sub param stuff:
I am a bit confused with your code, but I will present the Perl way of a standard command loop that uses text as a command:sub A { my $parm1 = shift; #effect: literally shift off the stack passed } sub B { my ($p1,$p2,$p3) = @_; #effect: make copy from stack passed }
There are of course many variations on the above. But it is short and to the point.while ( (print "Enter Command: "), (my $line = <STDIN>) !~ /^\s*q(uit)?\s*$/i ) { next if $line =~ /^\s*$/; #skip new lines next if $line =~/^\s*skip/i; #skip is a no op command insert if $line =~/^\s*insert/i; #do insert command delete if $line =~/^\s*delete/i; #do delete command #...etc.. } sub insert() {...} sub delete() {...}
I recommend not trying to be overly complex. I have written fancy hash driven dispatch tables, but I don't think that is what you need.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Subroutine references inside of a hash with arguments.
by chromatic (Archbishop) on Jul 25, 2009 at 07:27 UTC | |
by Marshall (Canon) on Jul 25, 2009 at 09:01 UTC | |
by chromatic (Archbishop) on Jul 25, 2009 at 17:55 UTC | |
by Marshall (Canon) on Jul 27, 2009 at 18:41 UTC | |
by Anonymous Monk on Jul 25, 2009 at 09:17 UTC | |
by Marshall (Canon) on Jul 25, 2009 at 09:28 UTC |