Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: perl eval

by paribasu (Initiate)
on Jul 05, 2010 at 17:37 UTC ( #848090=note: print w/replies, xml ) Need Help??


in reply to Re: perl eval
in thread perl eval

$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..!

Replies are listed 'Best First'.
Re^3: perl eval
by ww (Archbishop) on Jul 05, 2010 at 18:29 UTC
    "... 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.

        Your eval fails and you never check for it. But you were told that already. So please read the answers first before asking again.

Re^3: perl eval
by ww (Archbishop) on Jul 05, 2010 at 18:55 UTC

    ... 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.
        and I just want to evaluate whatever is there in the parameter

        That might end in tears!

        use strict; use warnings; my $param = shift; eval $param; die $@ if $@;

        Let's see some files.

        $ perl spw848064 'system ls' reallyImportant.data spw848064 $

        You can see what's coming :-)

        $ perl spw848064 'system qw{rm -f reallyImportant.data}' $

        Oops!

        <!--$ perl spw848064 'system ls' spw848064 $

        It might be worth doing a little sanity checking before evaling willy-nilly.

        Cheers,

        JohnGG

Log In?
Username:
Password:

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

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