require actually requires the file/module, so an error is raised when the file does not exist. You should wrap the require in an eval block (or expression). When using eval it is usually better to have the last statement within the eval always return true, and use that instead of $@ when testing for success. e.g. if (eval "something; 1;") {}

eval and require:

if(eval { require "foo.pm"; 1; } ) { print "loaded module ok\n"; }else{ print "could not load module $@\n"; }
eval and use:
if(eval " use foo; 1" ) { print "loaded module ok\n"; }else{ print "could not load module $@\n"; }
As for this line:
print petrol "hello there !";
The reason why that does not generate an error is that the first argument to print can be a bareword, which is interpreted as a filehandle name. e.g.:
print STDOUT "hello\n";
If you use strict and warnings (which you always should) then you will receive this error:
Unquoted string "petrol" may clash with future reserved word at p line + 3. Name "main::petrol" used only once: possible typo at p line 3. print() on unopened filehandle petrol at p line 3.

In reply to Re: Question about eval by imp
in thread Question about eval by tiny_tim

Title:
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, details, 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, summary, 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.