John W. Krahn wrote:
mobile.parmeni...@gmail.com wrote:

I'm new to perl. The following code is wrong., and I can not spot
it. Any suggestion?  Thx.

#!/usr/bin/perl -w

#!/usr/bin/perl
use warnings;
use strict;

&init_words ();

init_words();

my %word;

sub init_words{
    while ( $filename=<*.secret> ){

    while ( my $filename = <*.secret> ){

    open( WORDLIST, $filename ) ||
        die "Can not open $filename: $!";
    if ( -M WORDLIST < 7 ){
        while ( $name = <WORDLIST> ){

        while ( my $name = <WORDLIST> ){

        chmop( $name );

        chomp( $name );

        $word = <WORDLIST>;

        my $word = <WORDLIST>;

        chmop( $word );

        chomp( $word );

        $word{$name} = $word;
        }
    }
    else{
        rename( $filename, "$filename.old" ) ||
        die "Can not rename $filename to $filename.old.";
    }
    close( WORDLIST );
    }
}

Your code could also be written as:

#!/usr/bin/perl
use warnings;
use strict;


my %word = init_words();

sub init_words {
    local *ARGV;
    my @old_files;

    push @{ -M < 7 ? \...@argv : \...@old_files }, $_ for <*.secret>;

    if ( @old_files ) {
rename $_, "$_.old" or die "Can not rename $_ to $_.old." for @old_files;
        }

    # return keys and values if there are some
    if ( @ARGV ) {
        return map { chomp; $_ } <>;
        }

    # return nothing if nothing to return
    return;
    }




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to