By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

Help: hash key pass to subroutine as string failed

P: 2
I have a sub expand() to expand the path
Expand|Select|Wrap|Line Numbers
  1. sub expand($%) {
  2.  
  3.     my $self = shift;
  4.     my ($string, %params) = @_;
  5.  
  6.     # Perform parameter checks.
  7.     die("Mandatory parameter 'string' not defined.") unless (defined $string);
  8.     //other handling
  9. }
  10.  
  11. The followng call fails
  12.  
  13. sub foo {
  14.       my $files = {
  15.         'test.in'    => 'test.out'
  16.     };
  17.  
  18.     expand($_) 
  19.         foreach ( keys( %{$files} ) );
  20. }
  21. saying the 'string' not defined.
  22. While the following codes work:
  23. sub foo {
  24.       my $files = {
  25.         'test.in'    => 'test.out'
  26.     };
  27.  
  28.    foreach my $key ( keys %{$files} )  {
  29.             expand (+$key); #expand ($key) also fails
  30.    }
  31. }
Could some one give me a hint why? I am newbie for Perl.
Thanks!
Jan 5 '09 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
As shawn on another forum said, don't use prototypes, instead of :

sub expand($%) {

use a sub:

sub expand {
Jan 5 '09 #2

P: 2
Actually I tried to remove the prototype as suggested, but still failed to pass the correct string value of the key.
the following methods work as well:
method 1:
Expand|Select|Wrap|Line Numbers
  1. while ( my ($key,$value)  = each (%{$files}) ){
  2.       expand($key);
  3.     }
methods 2:
Expand|Select|Wrap|Line Numbers
  1. foreach my $key (keys  %{$files}  ) {
  2.       my $s = sprintf("%s",$key);
  3.       expand($s);
  4.    }
So I suspect foreach implmentation has sth. tricky.
Any suggestion?
Jan 8 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
See other forum
Jan 8 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.