3 arrays into @_

R

Rocky Allen

hi Y'all,

I am writing a backup script for an Oracle Database server on Linux. It
is very long so I only included the code I need help with here. The
entire script is published here. http://bobotheclown.org/scripts/comlang

I am trying to turn this code into a subroutine:

open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
print FH13 @joblog;
print FH13 "\n";
print FH13 "breakpointforrecover\n";
print FH13 "\n";
print FH13 @Intersection;
print FH13 "\n";
print FH13 "\n";
print FH13 @splitnames;
close FH13;

My question is this:

Since I have to pass all of those arrays into the subroutine as @_, how
can I tell when one ends and the next begins?

I would like for it to be a subroutine because the only way I currently
get a logfile i if the backup runs successfully. I want it in any case.


regards,
Rocky Allen
 
P

Paul Lalli

Rocky said:
hi Y'all,

I am writing a backup script for an Oracle Database server on Linux. It
is very long so I only included the code I need help with here. The
entire script is published here. http://bobotheclown.org/scripts/comlang

I am trying to turn this code into a subroutine:

open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
print FH13 @joblog;
print FH13 "\n";
print FH13 "breakpointforrecover\n";
print FH13 "\n";
print FH13 @intersection;
print FH13 "\n";
print FH13 "\n";
print FH13 @splitnames;
close FH13;

My question is this:

Since I have to pass all of those arrays into the subroutine as @_, how
can I tell when one ends and the next begins?

Don't pass the arrays - pass references to them:

sub print_to_file {
my $contentsfile = shift;
my ($joblog, $intersection, $splitnames) = @_;
open my $FH13, '>', $contentsfile or
die "Could not open $contentsfile: $!\n";
my $old_fh = select $FH13;
print @$joblog, "\n";
print "breakpointforrecover\n\n";
print @$intersection, "\n\n";
print @splitnames;
select $old_fh;
}

print_to_file($filename, \@joblog, \@intersection, \@splitnames);

alternatively, use a prototype to auto-create the references for you:
sub print_to_file ($\@\@\@) {
#... all code as before
}
print_to_file($filename, @joblog, @Intersection, @splitnames);

For more information:
perldoc perlsub
perldoc perlreftut
perldoc perlref
perldoc -f select

Paul Lalli
 
R

Rocky Allen

Don't pass the arrays - pass references to them:

sub print_to_file {
my $contentsfile = shift;
my ($joblog, $intersection, $splitnames) = @_;
open my $FH13, '>', $contentsfile or
die "Could not open $contentsfile: $!\n";
my $old_fh = select $FH13;
print @$joblog, "\n";
print "breakpointforrecover\n\n";
print @$intersection, "\n\n";
print @splitnames;
select $old_fh;
}

print_to_file($filename, \@joblog, \@intersection, \@splitnames);

alternatively, use a prototype to auto-create the references for you:
sub print_to_file ($\@\@\@) {
#... all code as before
}
print_to_file($filename, @joblog, @intersection, @splitnames);

For more information:
perldoc perlsub
perldoc perlreftut
perldoc perlref
perldoc -f select

Paul Lalli

Thank you. I have been reading about references lately and couldnt
imagine a situation where I would need one. how funny. Thanks again.

Rocky
 
T

Tad McClellan

Rocky Allen said:
hi Y'all,

I am writing a backup script for an Oracle Database server on Linux. It
is very long so I only included the code I need help with here. The
entire script is published here. http://bobotheclown.org/scripts/comlang

I am trying to turn this code into a subroutine:

open(FH13, ">$contentsfile") or die "Couldnt get FH13:$!";
print FH13 @joblog;
print FH13 "\n";
print FH13 "breakpointforrecover\n";
print FH13 "\n";
print FH13 @intersection;
print FH13 "\n";
print FH13 "\n";
print FH13 @splitnames;
close FH13;

My question is this:

Since I have to pass all of those arrays into the subroutine as @_, how
can I tell when one ends and the next begins?


You can't, so instead pass three *references* to arrays:

myfunc( \@joblog, \@intersection, \@splitnames );

Then deref them in the subroutine body:

my($joblog, $intersection, $splitnames) = @_;
...
print FH13 @$joblog;


See also:

perldoc perlreftut
perldoc perlref
 
P

Paul Lalli

Rocky said:
Thank you. I have been reading about references lately and couldnt
imagine a situation where I would need one. how funny. Thanks again.

.... you've never had a need for using a two-dimensional array or hash?
Or for using a class object?

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top