Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Question about eval

by tiny_tim (Sexton)
on Jan 16, 2007 at 07:39 UTC ( [id://594869]=note: print w/replies, xml ) Need Help??


in reply to Question about eval

Thank you for the replies. It is beginning to make sense, but why does perl allow me to hand a print statement a junk token if I do not use strict ? Also, can I please get a few more uses of eval ? I am still not 100% on eval. Thank you all :)

Replies are listed 'Best First'.
Re^2: Question about eval
by shmem (Chancellor) on Jan 16, 2007 at 08:53 UTC
    Because of TIMTOWDI (there's more than one way to do it). Attempting to print something somewhere is perfectly fine, even on a closed filehandle. But then, you should check the return value of print as it tells you wether the print actually succeeded. If you "use warnings" perl will warn you:
    use warnings; my $junkprint = print JUNK "foo"; my $stdoutprint = print STDOUT "junkprint = '$junkprint'\n"; print "stdoutprint = '$stdoutprint'\n"; __END__ Name "main::JUNK" used only once: possible typo at - line 2. print() on unopened filehandle JUNK at - line 2. Use of uninitialized value in concatenation (.) or string at - line 3. junkprint = '' stdoutprint = '1'

    Read the eval, do, require and use entries in perlfunc, as well as perlfaq8 which contains the difference between do, require and use. Then there is Super Search.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^2: Question about eval
by ikegami (Patriarch) on Jan 16, 2007 at 15:58 UTC

    why does perl allow me to hand a print statement a junk token if I do not use strict ?

    It's a filehandle, not a junk token. There's no way at compile time to know whether the filehandle has been opened or not, so STDOUT, FH, petrol, $fh are all the same and all perfectly valid.

    I am still not 100% on eval.

    require either returns true, or throws an exception. eval catches exceptions. You can check if an exception has been caught or not by checking $@.

    eval returns undef if an exception has been caught, so you can also do something like

    if (eval { require "module/that/does/not/exist"; 1 } ) { print "loaded module ok\n"; } else { print "could not load module: $@\n"; }

    or since require always returns true,

    if (eval { require "module/that/does/not/exist" } ) { print "loaded module ok\n"; } else { print "could not load module: $@\n"; }
Re^2: Question about eval
by ikegami (Patriarch) on Jan 16, 2007 at 15:55 UTC
    It's a filehandle, not a junk token. There's no way at compile time to know whether the filehandle has been opened or not, so STDOUT, FH, petrol, $fh are all the same and all perfectly valid.
Re^2: Question about eval
by lbjay (Novice) on Jan 17, 2007 at 20:59 UTC
    This was hinted at in a couple of the above replies, but combined with die (or croak) perl's eval provides a handy way to implement simple exception handling similar to the try/catch of other languages like Java and JavaScript.
    # some code that might fail eval { open MYFILE, "> some_file.txt" or die $!; ... close MYFILE; }; # catch the "exception" if ($@) { print "Errors during file operation: $@\n"; }
    For more, search for "exception" on CPAN; there are several modules that provide more elaborate exception handling mechanisms.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2025-06-15 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.