I have been playing with different styles and data structures, just trying to get a feel for perl better, and what I can do. I have been reading Perl Objects, References and Modules by Randal L. Schwartz. So I have seen what I can do with subroutine references. Now I am a subroutine prototyper by nature. In fact I do two things that seem to frustrate some fellow programers but it seems natural to me. I declare my subroutines before using them and prototype them at the same time. There are many reasons to do this, but a few is to catcht programming errors. The main being:

That said I have had problems with prototypes to anonymous subroutines.

use strict; my $SubRef = sub (\@$){ my $ArrayRef = shift; my $Scalar = shift; foreach my $item (@$ArrayRef){ print "$item"; } print "\n"; print "$Scalar"; print "\n"; }; my @Array = ("This ", "Is ", "A ", "Test ", "."); my $Scalar = "That was a test."; $SubRef->(@Array , $Scalar);

Now this code gives me:

Can't use string ("This ") as an ARRAY ref while "strict refs" in use +at C:\Projects\test.pl line 5.
Now if I change the code like this:
my $SubRef = sub ($$){
and
$SubRef->(\@Array , $Scalar);
And the output is fine. I can even leave it as:
my $SubRef = (\@$){
As long as I change the call to the sub to have the \@ it works. Am I missing something here or doing something silly. I am probably doing something silly but I can't see it. Any pointers or references would be appreciated. Sorry didn't see the pun in that until posted.

Update:
So would it be better to use anonymous arrays and hashes as a general rule of thumb? It seems to me this would minimise confusion in the subs by making the main body of the program ensure that everything already was a ref before you call the sub. But this might just be the mindset for what I am doing. Thank you all for the information.

"No matter where you go, there you are." BB

In reply to Subroutine References by Ninthwave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.