Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Monks,
Pretending for a moment globals are all fine and dandy, I have an issue. I have some shared routines that a couple of scripts use. They use global variables (one to a WWW::Mechanize object and another which is just holds the location of a directory to dump output in). These will never change throughout the course of the script, so I simply wrote the routines to use whatever variable had been defined when the routine is called.

Contents of my_subs.pl
sub process() { my $url = shift; $mech->get( $url ); my $html = $mech->content(); # Do all kinds of stuff w/ $html, including get() other pages, an +d login() to forms; $html = $mech->content(); return $html }
contents of myscript.pl
require( 'my_subs.pl' ); my $mech = WWW::Mechanize->new(); my $datadir = "/tmp/logs"; foreach ( @url ) { my $returned_html = &process( $_ ); # Bob Loblaw }
When process() is defined in an external file, it thinks $mech and $datadir are undefined. So, it doesn't work. I can't pinpoint exactly when it broke as it runs in cron, and I hadn't checked it in a while (a user pointed it out). It looks like just days ago though, and the last time I updated the packages on my system was a few weeks ago.

If process() is defined within myscript.pl and not an external file, life is good, and it works.

I know I can pass those things (and the few others the routine needs) as parameters, but my initial "parameterized" version of the routine did not work. It's not exactly the most kosher routine in the world. It scrapes a page that doesn't like to be scraped, and on enough failures, the page will do all kinds of nasty things like require visual human confirmation (boo). It's not the easiest to debug, either, as it doesn't tell you what's wrong. So, I only test for 15-20 minute intervals and stop. As such, I'd like to keep things as they are. As before, defining the rather long routine in the script file itself works, so I'm okay for now, but I'd rather have it in one centralized (external) place where it can be shared, and subsequent changes will only happen once.

So, my question now is, what am I missing about require() (and even the package approach) that I didn't know before? I thought declaring something with "my" outside of any scope made it global to everything...including external files.

In reply to require, globals, and some various mayhem by GaijinPunch

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, 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
  • 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.
  • 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 making s'mores by the fire in the courtyard of the Monastery: (11)
    As of 2020-02-27 17:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (121 votes). Check out past polls.

      Notices?