Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

[SOLVED]: Using glob in file test gives "Use of uninitialized value in -e" warning.

by Perl300 (Pilgrim)
on Dec 06, 2017 at 21:19 UTC ( #1205053=perlquestion: print w/replies, xml ) Need Help??
Perl300 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I know this might be something basic that I am unable to see.

On Linux with perl v5.24.0, I want to check if any files exist whose names start with a specific string. So first I tested it on command line and it seemed working fine there:

/perl -le 'print -e glob "/abc/def/ghi/test*"'
It prints 1 if any file exist with name starting with "test" under /abc/def/ghi directory and blank if such file doesn't exist.

But when I use same in program like:

if (-e glob '/abc/def/ghi/test*') # line 4 { `sudo rm /abc/def/ghi/test*`; # Remove files if they already exist }
I get warning (I think it's warning) when I run the program as: Use of uninitialized value in -e at ./script_name.pl line 4.

Which variable is uninitialized here? Is it $_?
I tried replacing single quotes with double quotes, using -f instead of -e but still get same warning.
Am I missing something here? Is it possible to remove the warning by correcting/changing something?

Thank you for your time!

EDIT: Changed subject line to solved!

Replies are listed 'Best First'.
Re: Using glob in file test gives "Use of uninitialized value in -e" warning.
by toolic (Bishop) on Dec 06, 2017 at 21:29 UTC
    When I enable warnings on the command line, I get warnings :)
    perl -wle 'print -e glob "/abc/def/ghi/test*"' Use of uninitialized value in -e at -e line 1. Use of uninitialized value in print at -e line 1.

    Here is one way to do what you want:

    if (glob '/abc/def/ghi/test*') { # sudo }
Re: Using glob in file test gives "Use of uninitialized value in -e" warning.
by NetWallah (Canon) on Dec 06, 2017 at 21:31 UTC
    Congrats on 'use warnings'!

    The error is coming from:

    if (-e glob '/abc/def/ghi/test*') # line 4
    For the case where the 'glob' returns an empty array undef (i.e. test* does not exist).

    You should break that into two tests:

    my @tests; if (@tests = glob '/abc/def/ghi/test*' and -e $tests[0]) {# line 4

                    All power corrupts, but we need electricity.

Re: Using glob in file test gives "Use of uninitialized value in -e" warning.
by afoken (Abbot) on Dec 06, 2017 at 21:47 UTC

    glob returns a list of file names. Combine it with grep:

    /etc>ls pa* passwd passwd- passwd~ /etc>perl -E 'say for grep { -e $_ } glob "pa*"' passwd passwd- passwd~ /etc>

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      What glob returns depends on the context. In scalar context (which -e imposes) it iterates over the matching files, and returns undef when there are no more files. If nothing matches the glob, it returns undef directly.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        What glob returns depends on the context.

        I must admit I never even thought of using glob in a non-list context, so I learned something new today. Great!

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: [SOLVED]: Using glob in file test gives "Use of uninitialized value in -e" warning.
by Laurent_R (Canon) on Dec 07, 2017 at 07:35 UTC
    You've received good answers to your question. I would just add that it is debatable whether the useof the -e operator is useful here, since you're using it on directory entries just returned by glob. Note that since you mention it, using the -f operator would definitely be more useful because if would make it possible to apply your action only to regular files (therefore not trying to delete directories, for example).

    Another comment is that it is usually considered a poor idea to use shell commands from a Perl script when there is an equivalent command in Perl. See unlink for details. Although if you really need to sudo, then it may be the only way.

      Although if you really need to sudo, then it may be the only way.

      Even then, I would probably use sudo to run the perl script and use unlink in the script. Having to work with some random default shell is just a mess, and it can be quite dangerous with elevated privileges.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Yes afoken, you're most probably right.

        It's actually quite strange that I wrote "may be the only way," because I actually thought of sudoing the whole Perl script when I reflected on what I was going to write (and I wasn't quite sure of which of the two solutions would be better), but, for some reason, that thought somehow got completely out of my mind when I actually typed the message.

RE: Using glob in file test gives "Use of uninitialized value in -e" warning.
by Perl300 (Pilgrim) on Dec 06, 2017 at 22:41 UTC
    Thank you afoken, toolic and NetWallah for your inputs.
    As always there is something new to learn in addition to just what one asks. That's one reason I periodically check PM and never found any equivalent site for other technologies/languages.
Re: [SOLVED]: Using glob in file test gives "Use of uninitialized value in -e" warning.
by Perl300 (Pilgrim) on Dec 07, 2017 at 21:30 UTC
    Thank you choroba and Laurent_R. I have made necessary changes to use unlink instead of rm.
    I also got useful idea from information at unlink. Thank you for that suggestion.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1205053]
Approved by toolic
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-12-15 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (443 votes). Check out past polls.

    Notices?