smturner1 has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I have a situation that I cannot seem to get around. I am using "exists" on a hash and I keep getting the following error:

exists argument is not a HASH or ARRAY element or a subroutine at deploy.pl line 166 (#1) (F) The argument to exists() must be a hash or array element or a subroutine with an ampersand, such as: $foo{$bar} $ref->{"susie"}12 &do_something Uncaught exception from user code: exists argument is not a HASH or ARRAY element or a subroutine at deploy .pl line 166. at deploy.pl line 166

Here is the code:</>

use strict; use warnings; use File::Find; my %old_list=(); my %new_list=(); my $diffs=DirCompare(); print "$_\n" for @$diffs; find ( sub { $old_list { $_ } = 1 }, $ARCHIVE ); find ( sub { $new_list { $_ } = 1 }, $SourceDir ); sub DirCompare{ for my $file ( keys %old_list ) { if ( exists %new_list{$file} ) { next; } else { push @diffs, "Old file not in new: $file"; } } for my $file ( keys %new_list) { if ( exists %old_list{ $file } ) { next; } else { push @diffs, "Old file not in new: $file"; } } return \@diffs; }

Any light that you can shed on this issue would be greatly appreciated.

Replies are listed 'Best First'.
Re: exists EXPR error
by atcroft (Abbot) on Dec 15, 2014 at 03:14 UTC

    Try replacing the line if ( exists %new_list{$file} ) { with if ( exists $new_list{$file} ) {, and the line if ( exists %old_list{$file} ) { with if ( exists $old_list{$file} ) {.

    Hope that helps.

      I am pretty sure that is what I have:

      You suggested, "{ if ( exists $new_list{$file} ) {"

      The code is broken into two lines to improve readability, but all of the code you suggested is present. Unless I am missing something!

      sub DirCompare{ for my $file ( keys %old_list ) { if ( exists %new_list{$file} ) { next; } else { push @diffs, "Old file not in new: $file"; } } for my $file ( keys %new_list) { if ( exists %old_list{ $file } ) { next; } else { push @diffs, "Old file not in new: $file"; } }

        Contrast:if ( exists %new_list{$file} ) {

              with:if ( exists $new_list{$file} ) {

        Can you see.............^.....the difference?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: exists EXPR error
by LanX (Saint) on Dec 15, 2014 at 05:46 UTC
    As a side note %new_list{$file} is a syntax error, but the exists error already makes perl die before this can be reported.

    > perl %h{a}; syntax error at - line 1, near "%h{" Execution of - aborted due to compilation errors.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      I don't have a v5.20 I can test on, but I suppose this is either still an error, or at least a warning with the hash slice syntax (the same way @array[0] will provoke a warning if they are activated, even in list context, while @array[0,] is always valid)

        Hi Eily

        I'm not sure what you mean, this %h{a} is not a hash slice, it has no meaning.

        (slices always start with an @ :)

        > @array[0] will provoke a warning ... even in list context, 

        Hmm not sure if I like that.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)