HOW-TO of the Month Club (end Of MARCH Edition)

(Even though many of you *flamed* me - _I'mmmm baaaack_ :)

This edition I will show you how to declare variables
WITHOUT using *my* $var everywhere...

#! perl -Tw
use strict;
use warnings;

# get execution name
$::prog = $0;
# clean off directory portion
$::prog =~ s|.*/||;

print "You ran $::prog! Good job!\n";

$:: syntax brings these variabkes into the current
program - which is always $main::

$::prog is a shorter way of saying $main::prog

No need to say
my $main::prog; # Or
my $prog;

as this is the same:

$::prog;

Two draw-backs - One is that you are actually typing more;
but I feel the code is clearer. The second draw-back is
that you must use $::varname everywhere now...

Also, all of these $:: are brought into GLOBAL
usage (which some programmers frown upon much
like GOTO syntax is frowned upon.)

You must use the *local* declarator to fix this
inside subroutines:

$::outside = 5;

sub change_5 () {

  local $::outside = 7;
  print "Inside sub: $::outside\n\n";
}

&change_5();
print "Out of sub: $::outside\n\n";


===== End of concept One... ===== Start concept Two -

Should I -
    use warnings, $^W, or -w
which to use ?

They are the same :)

#! /usr/bin/perl -w
$^W = 1; # same as -w above
use warnings; # same as -w above

Or, on the command-line:
        perl -w prog.perl


With $^W you can temporarily turn off warnings for a local test:

$::usage = "usage: $::prog [-abc] [ file ... ] (or -h for help)";
if (@ARGV && $ARGV[0] =~ "^-.+" ) {
  local($^W) = 0;  # suppress annoying undef warnings
  require "getopts.pl";
  &Getopts("abcXYZh");
}

Depending upon the number of flames I may post
something REALLY basic next time %)

--
_Sx_ http://youve-reached-the.endoftheinternet.org/ _____
perldoc -qa.a | perl -lpe '($_)=m("(.*)")' | grep Martian

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to