in reply to Weird array printing/passing to subroutine problem
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
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.@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; }
Mik Firestone ( perlus bigotus maximus )
|
|---|