Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Code reuse is a wonderful thing. You are getting great advice here. Use modules (see perlmod) and avoid globals, pass data to subs as arguments.

If you must use globals, you can either use package globals or import your variables so that you can see them in your main script. I'll show you how to specify the package of variables and subroutines here. Exporting/importing symbols is a bit more complex, so I'll leave that to the proper documentation.

File Foo.pm:

package Foo; my $hello; sub hello { print "$hello\n"; return; # I like explicit returns. }

File myscript.pl:

use strict; use warnings; use Foo; $Foo::hello = 'hello'; Foo::hello(); #prints "hello\n"

Also, ditch the unneeded & sigil from your subroutine calls. It can have unexpected consequences. Here are the relevent bits from perlsub, with emphasis added:

...If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.
...
Not only does the & form make the argument list optional, it also disables any prototype checking on arguments you do provide. This is partly for historical reasons, and partly for having a convenient way to cheat if you know what you're doing. See Prototypes below.

See perlsub for more details.

I think I need to put together a meditation - Ampersand Considered Harmful...


TGI says moo


In reply to Re: Include subs from different perl file by TGI
in thread Include subs from different perl file by ikkeniet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-26 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found