Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: perl eval

by ww (Archbishop)
on Jul 05, 2010 at 14:25 UTC ( #848070=note: print w/replies, xml ) Need Help??


in reply to perl eval

The reason you're not getting any output here is that you never call your sub, "egal ()" in xyz.pl. You could have learned this with judicious insertion of (debugging) print statements (to test the content/existence of the vars in your sub) or running the script in the deugger, >perl -d 848064_xyz.pl 'egal("20090316","7006")'/p>

Further, if you are trying to ensure that egal() gets called, leaving it out of the command line invocation makes no difference.

If you insert a line 26, "egal($param);  # call the sub!", your script will (try to) execute the sub (and as far as printing the plus signs); the balance of your problem (your regex usage) is left as an exercise for the OP.

Stock advice (you were going to get it sooner or later, even though it's not the root of your problem here): It's often a mistake to omit use strict and warnings which would have told you that your barewords, "true" and "false" (lines 17 & 21) might be a problem. But that's a generic problem and bit of advice. Use strict and warnings to help you uncover common problems, albeit, not the real problem here.

Replies are listed 'Best First'.
Re^2: perl eval
by paribasu (Initiate) on Jul 05, 2010 at 17:37 UTC

    $param itself has the calling argument so when i am calling eval with the $param it should get executed... atleast that is what I want..!

      "... atleast that is what I want..!"

      and I want a pony... but I've never found a hayburner in my Christmas stocking.

      Others monks have explained the shortcomings of your eval print $param; but, for emphasis:

      >perl 848064_xyz.pl 'egal("20090316","7006")' 'egal(20090316,7006)' is $param at line 10 'egal(20090316,7006)' Line 11 $param: |'egal(20090316,7006)'| At 13, $test: 'egal(20090316,7006)' Use of uninitialized value in concatenation (.) or string at 848064_xy +z.pl line 22. ++++ >

      And, just BTW, note that your double_quotes -- the "s -- don't pass through the windows shell (my Linux box is down, but IIRC, you'll have a mirror-image issue there).

      Here's your code, slightly modified at 5, 6, 9, 10, 12, 19 and 34 for illustration (thus, "line 26" in my previous reply is now line 38).

      #!/usr/bin/perl use strict; use warnings; # OP says: Now I am calling the script : # 001: perl xyz.pl 'egal("20090316","7006")' my $param = shift; chomp $param; print "\n\n\t $param is \$param at line 10\n\n"; eval print $param; print "\n\tLine 11 \$param: |$param| \n"; sub egal { my $result; my @arg = @_; my $test = $arg[0]; print "\n At 13, \$test: $test\n\n"; my @test = grep (/$test/i, @arg); print "\t $test[0] \n"; print "++++"; if ($#test == $#arg) { $result = 'true'; } else { $result = 'false'; } return $result; print " $result\n"; } egal($param); # call the sub!

        I have updated my problem. Hope it will make u understand the real problem... U run the second piece of code and will find the desired output i am expecting from the first piece... Please understand the problem first before answering.

      ... and 'Doh!' on /me.

      Modify beginning at line 32 as follows:

      } print "Inside the sub at line 34: |$result|\n"; return $result; } my $ result = egal($param); # call the sub! print "At line 39 (after the sub), \$result is: |$result|\n";

      and, lo and behold the output, as above but with a difference after the uninit... :

      ... Use of uninitialized value in concatenation (.) or string at 848064_xy +z.pl line 22. ++++Inside the sub at line 34: |false| At line 39 (after the sub), $result is: |false|
        I dont want to call egal from the script itself. The name of the sub is coming through parameter and I just want to evaluate whatever is there in the parameter.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://848070]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-05-21 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?