in reply to I'm trying to consolidate my functions into subroutines

Parameter passing is kind of weird in Perl. When you say
SpecifySeqLengths(my $id, my %seq);
it's actually creating global variables $id and %seq. But your subs don't use those global variables, they create their own variables by saying "my". Those "my" variables have the same name, but they are actually completely separate, so they don't actually share data with each other. Putting your data in global variables is arguably a bad habit, as it tends to inhibit code reuse. The "best practice" is to write something like this:
{ my $seq = HashSequences(); SpecifySeqLengths($seq); } sub HashSequences { my $seq = {}; ... $seq->{$id} .= $1; ... return $seq; } sub SpecifySeqLengths { my ($seq) = @_; ... for my $id (keys %$seq) { ... length($seq->{$id}) ... } ... }

Replies are listed 'Best First'.
Re^2: I'm trying to consolidate my functions into subroutines
by haukex (Archbishop) on May 13, 2017 at 17:38 UTC
    Parameter passing is kind of weird in Perl. When you say SpecifySeqLengths(my $id, my %seq); it's actually creating global variables $id and %seq.

    While I agree with the rest of the post and the code example is good, this part is not quite accurate. Those two variables are still lexically scoped to whatever block they're declared in. If that happens to be the scope of the file, one might tend to call them "global", but typically in Perl that term is used for package variables, which are "global" in the sense that they cross the file boundary. There are actually only a limited number of "truly global" variables (i.e. they cross even package boundaries), such as $_ and other special variables (although strangely, that list only seems to be in the Camel's reference section, not in the Perl docs).

      Good point. What would be a better term for top-level lexical variables?

        File-scope lexicals.


        Give a man a fish:  <%-{-{-{-<

        File scoped (contrary to block scoped)
Re^2: I'm trying to consolidate my functions into subroutines
by AnomalousMonk (Archbishop) on May 13, 2017 at 17:44 UTC

    Further to the AnonyMonk's post: Peter Keystrokes: Note that the invocation of the  SpecifySeqLengths() function in the OPed code before the definition of the subroutine hides problems that exist in that function definition and that warnings and strict (the latter in particular) would like to bring to your attention. Consider the case of invoking the following function before its definition versus after the definition:

    c:\@Work\Perl>perl -wMstrict -le "confuse_the_issue(my $x, my %hash); ;; sub confuse_the_issue { print qq{x: $x}; for $x (keys %hash) { print qq{$x $hash{$x}}; } } " Use of uninitialized value $x in concatenation (.) or string at -e lin +e 1. x: c:\@Work\Perl>perl -wMstrict -le "sub confuse_the_issue { print qq{x: $x}; for $x (keys %hash) { print qq{$x $hash{$x}}; } } ;; confuse_the_issue(my $x, my %hash); " Global symbol "$x" requires explicit package name at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Global symbol "%hash" requires explicit package name at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Global symbol "%hash" requires explicit package name at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.
    In the first case, Perl just gives you a light slap on the wrist, but that doesn't mean you are not already far down the road to Hell.


    Give a man a fish:  <%-{-{-{-<