Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How can I re-program the exists function?

by moritz (Cardinal)
on Aug 24, 2012 at 14:24 UTC ( #989549=note: print w/ replies, xml ) Need Help??


in reply to How can I re-program the exists function?

Now, my real question is: How can you create a function that would act like the "exists" function?

I'd be happy to be proven wrong, but I'd say you can't. At least not in pure Perl. Maybe with XS code when you hook into the parser.

In some cases you can tie variables, in which exists will call your EXISTS method. But depending on what you actually want to achieve, that might not be a good solution.

So, in the spirit of XY Question, what is the larger problem your are trying to solve?


Comment on Re: How can I re-program the exists function?
Download Code
Re^2: How can I re-program the exists function?
by greengaroo (Hermit) on Aug 27, 2012 at 15:37 UTC

    Well, as I said it was easier to explain using the exists example, but basically, my goal is to test an entry from a hashref, these entries contains objects and I wanted to create a generic function that receive a hashref path (just like the build-in exists function do) then test if the entry exists (using exists) then do some more tests on the object if it exists. I am able to do it with a workaround, it's just that I wanted to be fancy and be able to send only on parameter to my function, instead of two, like I am doing now.

    I hope this explanation is clear enough. I cannot get into more details and I don't want to start a "why-don't-you-do-this-instead" thread, I just wanted to know if it was possible to emulate the exists function.

    Thanks very much for your reply!

    There are no stupid questions, but there are a lot of inquisitive idiots.
      my goal is to test an entry from a hashref, these entries contains objects

      A reference to an object can't be undef() or even false (unless you do some strange overloading) so what you want is trivial:

      mytest( $hash{object} || '' ); sub mytest { my( $obj ) = @_; return if ! $obj; }

      The "|| ''" is there just to ensure that autovivification doesn't happen. I believe current versions of Perl actually avoid autovivification for this case if you don't assign to $_[0] in the mytest sub, but that's the kind of detail that is subject to change so I tend to "play it safe" on such issues.

      - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (18)
As of 2014-07-31 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls