Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pushing a variable and hash into a subroutine

Status
Not open for further replies.

FreeBASE

Programmer
May 3, 2002
39
US
Anyone know if this is possible?

The situation::
Basically I am trying to push a variable and a hash into
a subroutine. Look at the code below.

---------------------------------------------------------------------------------------

#!/usr/bin/perl

&whatever("template.tt2", \%tt_hash);

sub whatever {
my ($temp_file, %tt_hash) = @_; #---->> I know this doesn't work

my $template = Template->new();
$template->process($temp_file, \%tt_hash)
|| die "Template process failed: ", $template->error(), "\n";
}
 
Try instead:
Code:
&foo( $var, \%hash );

sub foo {
  my( $var, $hash_ref ) = @_;

  foreach ( sort keys %$hash_ref ) {
    print "$_ => $hash_ref->{$_}\n";
  }
}
Cheers, Neil :)
 
Or if you REALLY want a hash in the subroutine you could do

sub do_hash {

my $temp_file = shift @_;
my %tt_hash = %{ shift @_ };

...


jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top