help for function

Discussion in 'Perl' started by NSC, Jul 2, 2004.

  1. NSC

    NSC Guest

    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.
    NSC, Jul 2, 2004
    #1
    1. Advertising

  2. NSC

    Guest

    "NSC" <> wrote in message news:<40e5a5b1$>...

    > 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.
    , Jul 2, 2004
    #2
    1. Advertising

  3. NSC

    Joe Smith Guest

    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;
    Joe Smith, Jul 3, 2004
    #3
  4. NSC

    NSC Guest

    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" <> 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;
    NSC, Jul 3, 2004
    #4
  5. NSC

    Joe Smith Guest

    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
    Joe Smith, Jul 4, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,009
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,403
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    891
    Bengt Richter
    Aug 1, 2005
  4. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,216
    Barry Schwarz
    Sep 5, 2005
  5. weafon
    Replies:
    1
    Views:
    285
    Diez B. Roggisch
    Jul 14, 2009
Loading...

Share This Page