Help with syntax

Discussion in 'Perl Misc' started by kramer31, Jan 29, 2007.

  1. kramer31

    kramer31 Guest

    Hi. I used perl a few years ago and found it to be wonderfully
    powerful, but I don't have my old code (it's proprietary) and I seem
    to have forgotten some of the subtelties of the language including how
    to use references properly ... I'm using strict here to help catch
    some of these subtle errors at compile time.


    Anyway, can someone look at the code and tell me what the *bleep* I'm
    doing wrong here?

    #!/usr/bin/perl -w
    use strict;

    sub checkDir {
    my $curDir = $_[0];
    my %files = %{$_[1]};
    my $file;

    chdir($curDir);

    my @directories;
    my @listing=`ls`;
    my $fileType;
    my $myDir = `pwd`;

    foreach $file (@listing) {
    print "list: $file";
    $fileType=`file $file`;
    if($fileType =~ /directory/) {
    print "Added to directory\n";
    push @directories, $file;
    }
    else {
    if($fileType !~ /symbolic/) {
    print "regular file\n"
    if(! exists $files{ $file }) { #b
    my @tmpArray;
    $files{ $file } = \@tmpArray;
    }

    push @{ $files{ $file } }, $myDir; #a

    }
    }
    }
    }

    my $curDir = `pwd`;
    my %files = ();

    checkDir($curDir, \%files);

    #end of code

    The error at line labeled # a is:
    "my" variable %files masks earlier declaration in same scope

    The error at line labeled #b is:
    syntax error

    Any help would be much appreciated.
     
    kramer31, Jan 29, 2007
    #1
    1. Advertising

  2. kramer31

    kens Guest

    On Jan 29, 6:35 pm, "kramer31" <> wrote:
    > Hi. I used perl a few years ago and found it to be wonderfully
    > powerful, but I don't have my old code (it's proprietary) and I seem
    > to have forgotten some of the subtelties of the language including how
    > to use references properly ... I'm using strict here to help catch
    > some of these subtle errors at compile time.
    >
    > Anyway, can someone look at the code and tell me what the *bleep* I'm
    > doing wrong here?
    >
    > #!/usr/bin/perl -w
    > use strict;
    >
    > sub checkDir {
    > my $curDir = $_[0];
    > my %files = %{$_[1]};
    > my $file;
    >
    > chdir($curDir);
    >
    > my @directories;
    > my @listing=`ls`;
    > my $fileType;
    > my $myDir = `pwd`;
    >
    > foreach $file (@listing) {
    > print "list: $file";
    > $fileType=`file $file`;
    > if($fileType =~ /directory/) {
    > print "Added to directory\n";
    > push @directories, $file;
    > }
    > else {
    > if($fileType !~ /symbolic/) {
    > print "regular file\n"
    > if(! exists $files{ $file }) { #b
    > my @tmpArray;
    > $files{ $file } = \@tmpArray;
    > }
    >
    > push @{ $files{ $file } }, $myDir; #a
    >
    > }
    > }
    > }
    >
    > }my $curDir = `pwd`;
    > my %files = ();
    >
    > checkDir($curDir, \%files);
    >
    > #end of code
    >
    > The error at line labeled # a is:
    > "my" variable %files masks earlier declaration in same scope
    >
    > The error at line labeled #b is:
    > syntax error
    >
    > Any help would be much appreciated.


    Put a semicolon at the end of this line:
    print "regular file\n"

    HTH, Ken
     
    kens, Jan 30, 2007
    #2
    1. Advertising

  3. kramer31

    kramer31 Guest

    Wow. Thanks a lot. I can't believe I posted a semicolon question to
    the newsgroup. I should remember that line number error don't always
    refer to the exact line where the error is.


    Thanks again.


    On Jan 29, 4:20 pm, "kens" <> wrote:
    > On Jan 29, 6:35 pm, "kramer31" <> wrote:
    >
    >
    >
    > > Hi. I used perl a few years ago and found it to be wonderfully
    > > powerful, but I don't have my old code (it's proprietary) and I seem
    > > to have forgotten some of the subtelties of the language including how
    > > to use references properly ... I'm using strict here to help catch
    > > some of these subtle errors at compile time.

    >
    > > Anyway, can someone look at the code and tell me what the *bleep* I'm
    > > doing wrong here?

    >
    > > #!/usr/bin/perl -w
    > > use strict;

    >
    > > sub checkDir {
    > > my $curDir = $_[0];
    > > my %files = %{$_[1]};
    > > my $file;

    >
    > > chdir($curDir);

    >
    > > my @directories;
    > > my @listing=`ls`;
    > > my $fileType;
    > > my $myDir = `pwd`;

    >
    > > foreach $file (@listing) {
    > > print "list: $file";
    > > $fileType=`file $file`;
    > > if($fileType =~ /directory/) {
    > > print "Added to directory\n";
    > > push @directories, $file;
    > > }
    > > else {
    > > if($fileType !~ /symbolic/) {
    > > print "regular file\n"
    > > if(! exists $files{ $file }) { #b
    > > my @tmpArray;
    > > $files{ $file } = \@tmpArray;
    > > }

    >
    > > push @{ $files{ $file } }, $myDir; #a

    >
    > > }
    > > }
    > > }

    >
    > > }my $curDir = `pwd`;
    > > my %files = ();

    >
    > > checkDir($curDir, \%files);

    >
    > > #end of code

    >
    > > The error at line labeled # a is:
    > > "my" variable %files masks earlier declaration in same scope

    >
    > > The error at line labeled #b is:
    > > syntax error

    >
    > > Any help would be much appreciated.Put a semicolon at the end of this line:

    > print "regular file\n"
    >
    > HTH, Ken
     
    kramer31, Jan 30, 2007
    #3
  4. kramer31

    J. Gleixner Guest

    kramer31 wrote:
    > Wow. Thanks a lot. I can't believe I posted a semicolon question to
    > the newsgroup. I should remember that line number error don't always
    > refer to the exact line where the error is.


    FYI: Take a look at File::Find. You could do the same thing with only
    a few lines of code, using that module.
     
    J. Gleixner, Jan 30, 2007
    #4
  5. kramer31 wrote:
    > Hi. I used perl a few years ago and found it to be wonderfully
    > powerful, but I don't have my old code (it's proprietary) and I seem
    > to have forgotten some of the subtelties of the language including how
    > to use references properly ... I'm using strict here to help catch
    > some of these subtle errors at compile time.
    >
    >
    > Anyway, can someone look at the code and tell me what the *bleep* I'm
    > doing wrong here?
    >
    > #!/usr/bin/perl -w
    > use strict;
    >
    > sub checkDir {
    > my $curDir = $_[0];
    > my %files = %{$_[1]};


    You are copying the entire hash to a hash that is only visible inside this
    subroutine so why not just pass the whole hash in the first place:

    checkDir( $curDir, %files );

    And then copy it to your lexical hash inside the sub:

    sub checkDir {
    my ( $curDir, %files ) = @_;

    If you intended to modify the other hash %files from inside the subroutine
    then you need to use the reference instead of making a copy:

    my %files;
    checkDir( $curDir, \%files );

    sub checkDir {
    my ( $curDir, $files ) = @_;
    # ...
    # modify $files->{ $file }
    }


    > my $file;
    >
    > chdir($curDir);
    >
    > my @directories;
    > my @listing=`ls`;


    You would be better off using opendir/readdir or glob instead of an external
    command. BTW, backquotes return "lines" which end in newlines which have to
    be removed if you want the actual file names.


    > my $fileType;
    > my $myDir = `pwd`;
    >
    > foreach $file (@listing) {
    > print "list: $file";
    > $fileType=`file $file`;


    You would be better off using stat/lstat instead of an external command.

    > if($fileType =~ /directory/) {
    > print "Added to directory\n";
    > push @directories, $file;


    @directories is local in scope to the subroutine and you are not using it
    anywhere else?

    > }
    > else {
    > if($fileType !~ /symbolic/) {
    > print "regular file\n"

    ^
    As was pointed out, the semicolon is missing there.

    > if(! exists $files{ $file }) { #b
    > my @tmpArray;
    > $files{ $file } = \@tmpArray;
    > }


    That test and assignment is superfluous because perl uses autovivification.

    > push @{ $files{ $file } }, $myDir; #a
    >
    > }
    > }
    > }
    > }
    >
    > my $curDir = `pwd`;
    > my %files = ();
    >
    > checkDir($curDir, \%files);
    >
    > #end of code
    >
    > The error at line labeled # a is:
    > "my" variable %files masks earlier declaration in same scope
    >
    > The error at line labeled #b is:
    > syntax error
    >
    > Any help would be much appreciated.


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

    my $curDir = cwd;

    opendir my $dh, $curDir or die "Cannot open '$curDir' $!";

    my %files;
    while ( my $file = readdir $dh ) {
    lstat "$curDir/$file" or die "Cannot stat '$curDir/$file' $!";
    !-d _ && !-l _ && push @{ $files{ $file } }, $curDir;
    }

    __END__




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Jan 30, 2007
    #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. gabriele renzi
    Replies:
    2
    Views:
    242
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    247
  3. Good Night Moon
    Replies:
    9
    Views:
    321
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    352
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    349
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page