Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > help for function

Reply
Thread Tools

help for function

 
 
NSC
Guest
Posts: n/a
 
      07-02-2004
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.






 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      07-02-2004
"NSC" <(E-Mail Removed)> wrote in message news:<40e5a5b1$(E-Mail Removed)>...

> 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.
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      07-03-2004
NSC wrote:

> <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;
 
Reply With Quote
 
NSC
Guest
Posts: n/a
 
      07-03-2004
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.


"Joe Smith" <(E-Mail Removed)> a écrit dans le message news:
SHCFc.3059$JR4.3003@attbi_s54...
> NSC wrote:
>
> > <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;



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-04-2004
NSC wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments