Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Is there any way to override "file test operator"?

by Marshall (Canon)
on Aug 09, 2011 at 02:31 UTC ( #919361=note: print w/replies, xml ) Need Help??


in reply to Is there any way to override "file test operator"?

WHAT?

if (-e($file_name)) {};
Trying to override the -e file test is not what I would consider "smart". Even if you could, this is a bad idea.

What other meaning would you have the "-e" do? And why would it make sense in the code?

  • Comment on Re: Is there any way to override "file test operator"?

Replies are listed 'Best First'.
Re^2: Is there any way to override "file test operator"?
by Tux (Canon) on Aug 09, 2011 at 06:46 UTC

    And I - as a simple bystander - would read that as

    if ( - e ($filename)) { ^

    that is "the negative value returned by the function e with argument $filename".


    Enjoy, Have FUN! H.Merijn
      #!usr/bin/perl -w use strict; my $file = 'C:\temp\test20.pl'; if ( -e $file) { print "$file EXISTS!\n"; } print "$0 \n"; if ( - e $file) #this does not work! { print "$file WILD!\n"; } __END__ Can't locate object method "e" via package "C:\temp\test20.pl" (perhaps you forgot to load "C:\temp\test20.pl"?) at C:\TEMP\test20.pl line 13. C:\temp\test20.pl EXISTS! C:\TEMP\test20.pl

        I said that this was how I read that, and parens do matter here:

        $ cat test.pl #!/pro/bin/perl use strict; use warnings; my $file = "/tmp/test20.pl"; -e $file and print "$file EXISTS!\n"; print "$0 \n"; - e $file and print "$file WILD!\n"; $ perl test.pl test.pl Can't call method "e" without a package or object reference at test.pl + line 10. $

        versus

        $ cat test.pl #!/pro/bin/perl use strict; use warnings; my $file = "/tmp/test20.pl"; -e $file and print "$file EXISTS!\n"; print "$0 \n"; - e ($file) and print "$file WILD!\n"; $ perl test.pl test.pl Undefined subroutine &main::e called at test.pl line 10.

        Enjoy, Have FUN! H.Merijn
Re^2: Is there any way to override "file test operator"?
by JavaFan (Canon) on Aug 09, 2011 at 08:19 UTC
    Even if you could, this is a bad idea.
    Such a blanket statement, without knowing the reasons of the OP needs at least some justification to be taken seriously.
Re^2: Is there any way to override "file test operator"?
by QM (Parson) on Mar 20, 2018 at 09:59 UTC
    Why can't the OP decide to make a smarter file test operator? Maybe instrument it, or log something, or count gumdrops? Or maybe he has a new file system idea, where there are virtual files, and they only show up with special juju magic? Or an evil twin has renamed some files with a specific pattern, and the code needs to test for both possibilities?

    Sure, outside of that, it's probably not a good idea.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      I read back through this thread. Now that I see it again, the question appears to be "how can I make new file test operator?".
      I don't know how to do that.

      Overriding an existing file operator (like: -e $filename) is very bad idea. I stick to that opinion. The issue is when I see a line like that in some huge amount of code, I expect -e to do what it normally does without having to read any previous code that redefined that operation. And BTW, I don't know how to redefine -e either.

      In this case, I would make a subroutine that did the desired test and use that. In terms of execution efficiency, I see no difference. In terms of source code, there would be a huge increase in clarity.

        The benefit of overloading it, is that existing code "does the right thing", given whatever the new underlying context is, everyone can use the old idioms, even though they aren't quite the same.

        In fact, a good overload would not lose functionality on normal arguments. For instance, what if someone wanted to instrument logging by absolute filename, to collect statistics? Extending -e to do this would be easier than replacing every file test operator call everywhere, including external packages.

        For these kinds of questions, it's better if we get a real sense of the OP's ultimate goal and purpose, before declaring "that's a bad idea" just because it usually is.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

        -e is broken on windows, it doesn't do unicode

        Win32::Unicode does do unicode , but it can't override -e

        So win32 people can't use -e on win32

        You're still not authority on what should or should not be done

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://919361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2021-04-21 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?