The Perl Basics You Need To Know Page 4

Now I know the module name and it's loaded, so for example if I need to use some variables stored in this module to open a database connection, I will do:

  ("DBI:mysql:$ {.'::DB_NAME'}::$ {.'::SERVER'}",
   $ {.'::USER'},
   $ {.'::USER_PASSWD'},
    PrintError => 1, # warn() on errors
    RaiseError => 0, # don't die on error
    AutoCommit => 1, # commit executes immediately

Where variable like:

  $ {.'::USER'}

In my example are really:


If you want to access these variable from within your code at the run time, instead accessing to the server object , use the request object :

  my  = shift;
  my       = ->dir_config('FooBaseDir')      || '';

  my  = ->dir_config('FooConfigModule') || '';

The Scope of the Special Perl Variables

Now let's talk about Special Perl Variables.

Special Perl variables like $| (buffering), $^T (script's start time), $^W (warnings mode), $/ (input record separator), $\ (output record separator), and many more are all true global variables; they do not belong to any particular package (not even main::) and are universally available. This means that if you change them, you change them anywhere across the entire program; furthermore you cannot scope them with my(). However you can local()ise them which means that any changes you apply will only last until the end of the enclosing scope. In the mod_perl situation where the child server doesn't usually exit, if in one of your scripts you modify a global variable it will be changed for the rest of the process's life and will affect all the scripts executed by the same process. Therefore, localising these variables is highly recommended, I'd say mandatory.

I will demonstrate the case on the input record separator variable. If you undefine this variable, the diamond operator (readline) will suck in the whole file at once if you have enough memory. Remembering this you should never write code like the example below:

  $/ = undef; # BAD!
  open IN, "file" ....
    # slurp it all into a variable
   = <IN>;

The proper way is to have a local() keyword before the special variable is changed, like this:

  local $/ = undef; 
  open IN, "file" ....
    # slurp it all inside a variable
   = <IN>;

But there is a catch. local() will propagate the changed value to the code below it. The modified value will be in effect until the script terminates, unless it is changed again somewhere else in the script.

A cleaner approach is to enclose the whole of the code that is affected by the modified variable in a block, like this:

    local $/ = undef; 
    open IN, "file" ....
      # slurp it all inside a variable
     = <IN>;

That way when Perl leaves the block it restores the original value of the $/ variable, and you don't need to worry elsewhere in your program about its value being changed here.

Note that if you call a subroutine after you've set a global variable but within the enclosing block, the global variable will be visible with its new value inside the subroutine.

Compiled Regular Expressions

This article was originally published on Aug 2, 2000

Thanks for your registration, follow us on our social networks to keep up-to-date