help for function

N

NSC

Hi all,

I have this :

<SNIP>
foreach $item (@NEW_FILES)


print "\n $item \n ";

($size,$atime,$mtime,$ctime) = (stat($item))[7..10];
$a_time = localtime($atime);
$m_time = localtime($mtime);
$c_time = localtime($ctime);

printf ("\n Size(bytes): %10d \n" ,
$size );
print " Creation Time : $c_time \n" ;
print " Last Modification : $m_time \n" ;
print " Last Access : $a_time \n";
print " Last Access : $a_time \n";
}
<SNIP>

and I like to do this:

<SNIP>
foreach $item (@NEW_FILES)

stats;
}
<SNIP>

so I made:

<SNIP>
sub stats


print "\n $item \n ";
($size,$atime,$mtime,$ctime) = (stat($item))[7..10];
$a_time = localtime($atime);
$m_time = localtime($mtime);
$c_time = localtime($ctime);

print "\n Size: $size \n";
print " Creation Time : $c_time \n" ;
print " Last Modification : $m_time \n" ;
print " Last Access : $a_time \n";
}
<SNIP>

and it didn't work !

I made the same with hash functions and it works.

I declared all variables with 'my' , is that the problem.

thanks all.

Simon S.
 
N

nobull

NSC said:
I have this :

[ snip not real Perl code that doesn't use my() ]
and it didn't work !

Never say "didn't work". Always say what happened and how this
differed from what you wanted.
I declared all variables with 'my' , is that the problem.

Not in the code you showed us you didn't. Anyhow without a clue what
you actually did all I can asy that declaring variables is more often
part of the solution than part of the problem.

Please produce a minimal but complete script that demonstrates what
you are trying to do.

See posting guidelines in comp.lang.perl.misc for this and much other
good advice.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 
J

Joe Smith

NSC said:
<SNIP>
foreach $item (@NEW_FILES)
stats;
}
<SNIP>

There's your problem. You should have used
stats($item);
and changed the first executable line of stats() to be
my $item = shift;
 
N

NSC

Thank you Joe.

It works great, but (IF you have time) I have a subsidiary question :

why does this one work ?
I don't see the difference !

<SNIP>
sub hash {

open(FILE, $item_full_name);
binmode(FILE);
bla, bla
}

bla, bla

foreach (@FILES) {

$item_full_name = $folder . "\\" . $_;
$filecount++;
hash;
}
<SNIP>

Have a nice day.

Simon S.
 
J

Joe Smith

NSC said:
It works great, but (IF you have time) I have a subsidiary question :
why does this one work ?

If you're using variables declared with 'my' (and you should), then
you have to explicitly pass the variable to the subroutine.

In general it is bad to use subroutines that rely on constantly changing
global variables (like your $item_full_name). Pass the changeable bits
as subroutine arguments.
-Joe
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top