Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do I include a file and execute the code in the file?

( #80940=categorized question: print w/ replies, xml ) Need Help??
Contributed by xorl on May 16, 2001 at 20:12 UTC
Q&A  > files


Description:

This is basically my script ....
if ($var eq "foo") { Include file1.pl and execute code } if ($var eq "bar") { Include file2.pl and execute code } else { Include file1.pl and execute code }
I tried using require but if I need to include the same file twice it tells me the libary is already loaded. Idealy I'd like something like PHP's include command.Any suggestions?
Thanks!
Xorl

Answer: How do I include a file and excude the code in the file?
contributed by Albannach

Have you looked at do?

Answer: How do I include a file and execute the code in the file?
contributed by tachyon

Use this sub:

# Useage example: my $error = run_code('/file1.pl'); print "Run code failed, err msg:\n$error" if $error; sub run_code { my $path = shift; open(CODE, $path) || return "Oops: $path $!\n"; local $/; my $code = <CODE>; eval $code; return $@; }
Needs full path to code to be run.

The local $/ gives a locally undefined value to the input record seperator $/ with the result that we glob the whole file into $code. This saves us from @code = <CODE>; $code= join'',@code;

The sub returns undef if it suceeds or the error message if it fails to run the code.

Hope this helps.

tachyon

Answer: How do I include a file and execute the code in the file?
contributed by chromatic

Write a module that exports functions. Call the functions instead of requireing the file. See perlmod or use Super Search to look for 'writing a module'.

Answer: How do I include a file and execute the code in the file?
contributed by Anonymous Monk

or why not simply use it like this? do 'something.pl'

Answer: How do I include a file and execute the code in the file?
contributed by ton

Umm... why would you need to include the file twice? If you don't like require complaining, you can store the files you've already included in a hash, then include files only if they are not already in the hash...

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (14)
    As of 2014-12-18 15:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (56 votes), past polls