Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Dear Monks,

I have a good question for you.

Lets say I want to re-program the "exists" function. Of course, that is not my end goal, but I will use this as an example because it will be easier to explain.

Consider this module:

package MyExists; use Exporter qw( import ); @EXPORT = qw(my_exists); sub my_exists { my ($string) = @_; if ( eval("exists($string)") ) { return "Yes!"; } else { return "No!"; } } 1;

Now this script:

use MyExists qw(my_exists); my %hash = ( 'test1' => 'value1', 'test2' => 'value2' ); print "Real exists: "; print exists( $hash{'test1'} ); print "\n"; print "My exists: "; print my_exists( $hash{'test1'} ); print "\n"; print "Real exists: "; print exists( $hash{'test3'} ); print "\n"; print "My exists: "; print my_exists( $hash{'test3'} ); print "\n";

The result is:

Real exists: 1 My exists: No! Real exists: My exists: No!

Now, my real question is: How can you create a function that would act like the "exists" function? In other words, I need to build a function that is able to receive an expression and not a value (like "my_exists" actually does). According to perldoc, "exists" receives and "EXPR". How can I do that myself?

If I pass '$hash{"test1"}' as a string, then I could probably evaluate it right? Wrong! My function doesn't know what $hash is because its not declared in the Module!

I started to look at the prototypes for subroutines and I didn't find a solution. No luck with Google either.

I have a workaround. In short, I pass two arguments, first the HashRef: \%hash, then the string: '$hash{"test1"}'. It works but I would really like to know how to do it the "exists" way .

Thank you!

There are no stupid questions, but there are a lot of inquisitive idiots.

In reply to How can I re-program the exists function? by greengaroo

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the universe expands...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (3)
    As of 2018-02-23 01:29 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (300 votes). Check out past polls.