Hi Martin,

here are some comments on your code in addition to what Uri said.

On Fri, 27 Feb 2015 22:24:52 -0600
"Martin G. McCormick" <mar...@server1.shellworld.net> wrote:

> Brock Wilcox writes:
> > I'm afraid a bit more context is needed to identify the problem. Could you
> > post your entire bit of code into a gist or pastebin or something for us 
> > to
> > see?
> 
>       I'll do better than that. This is a script which is
> stripped of everything but the problem code. It is 20 lines long
> and here it is.
> 
> #!/usr/bin/perl -w
> use strict;

"use strict;" is goot, but the "-w" flag should be replaced by "use warnings;":

http://perl-begin.org/tutorials/bad-elements/#the-dash-w-flag

(Note: perl-begin.org is a domain I originated and maintain).

> 
> #Declare main variables.
> 
> #main locals
> my @tasks;
> my $task;
> my $report_static;
> $report_static = sub {
>     print "$task\n";
> };
> 

It's a good idea to avoid predeclarations:

http://perl-begin.org/tutorials/bad-elements/#declaring_all_vars_at_top

Usually "my $var" should be immediately followed by an assignment using "=".

> #MAIN_CODE START
> $tasks[0] = "red";
> $tasks[1] = "blue";
> $tasks[2] = "green";

You can either do «my @tasks = ("red", "blue", "green");»
(or «my @tasks = qw(red blue green);» or use push, see:

http://perl-begin.org/tutorials/bad-elements/#appending_using_arr_increment

Don't use indexes in succession.

> foreach $task (@tasks) {
>     &$report_static;

The problem with «&$sub_ref;» (without parentheses) is that it also passes @_
to $sub_ref. Either write:

        &$sub_ref(); 

Or better yet:

        $sub_ref->();

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
UNIX Fortune Cookies - http://www.shlomifish.org/humour/fortunes/

If you have the same ideas as everybody else, but have them one week earlier
than everyone else — then you will be hailed as a visionary. But if you have
them five years earlier, you will be named a lunatic. ( Barry Jones )

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to