Since we are here to refine our perl skills, I cannot add anything ot the answers already given, but I can ask why you are using positional parameters?

It may be me ( my mind is like a steel trap... rusted open ) but I have an awful time remembering arbitrary order like this. I have found it much easier on myself to use named parameters instead. This also allows a much nicer syntax to give errors or default values.

My take on your code would be

@AllFileDetails[0] = "blah, blah, blah"; @AllFileDetails[1] = "foo, bar, diddledum"; print "Printing array.\n"; print @AllFileDetails; print "\nCalling subroutine now\n"; &WriteToDisk(details => \@AllFileDetails, filename => 'files.txt'); exit; sub WriteToDisk { my %params = @_; my @arraytowrite = @{$params{details}} || (); my $writefile = $params{filename} || ''; my $text = $params{text} || 'no text'; unless ( @arraytowrite && $writefile ) { print "Usage: WriteToDisk( filename => file to use, details => reference to an array holding the detai +ls "; return 0; } print "\nInside the subroutine 'WriteToDisk' now.\n"; print "Printing array.\n"; print @arraytowrite; exit; open HANDLE,$writefile or die "Cannot open open output file $write +file for writing:$!\n"; print HANDLE $ReportGenerated; print HANDLE @arraytowrite; close HANDLE; }
This way, you never need to memorize the call order. Also notice the slight changes I made to the open HANDLE statement. You really want to use parens around both the open and the die arguements combined with ||, or you want to forget all the parens and use 'or'. Don't mix.

Mik Firestone ( perlus bigotus maximus )


In reply to RE: Weird array printing/passing to subroutine problem by mikfire
in thread Weird array printing/passing to subroutine problem by dmtelf

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.