I want to convert my foreach loop to a sub routine sub mybits. I'm sure I am not calling this properly or for that matter setting it up as a sub.
What I want to do is return a value from the sub routine which is any one of three variables which i tested foreach part and am able to get data.
Got this mesg. I am using strict, warnings: Can't modify non-lvalue subroutine call
How do I call this sub routine to get either of my variables ($dir, $fname, $fsize)?
Code:
my $out;
mybits (my $dir)=$out;
print mybits($dir);
print "This is mybits: $out\n";
sub mybits
{
foreach my $file( @{ $data->{file} } )
{
#my( $dir, $fname );
my( $dir, $fname, $fsize );
if( $file->{path} =~ /^(.*)\/([^\/]+)$/ )
{
$dir = $1;
$fname = $2;
$fsize = $file->{size};
}
else
{
$dir = "";
$fname = $file->{path};
}
#print "This is the DIRECTORY: $dir\n";
#print "This is the FILE: $fname\n";
#print "This is the FILE SIZE: $fsize\n";
}
}
use strictanduse warnings? Further, it doesn't appear as thoughmybitsis returning anything. Also, why would you assign the value ofmybits($dir)to an uninitialized scalar and then expectprint mybits($dir)to produce anything?mybitsdoesn't even take any arguments, since you're not doing anything with@_inside ofmybits.$fileas an element of the array@{$data->{file}}, but then you mention$file->{path}, which would indicate that$fileis a hash reference.... Is$data->{file}an array reference of hash references? What is the structure of$data?