my $scan = 'foo';
# here $scan == 'foo';
if ( $something ) {
my $scan = 'bar';
# here $scan == 'bar';
}
# now the $scan we defined in the if() {} is out of scope,
# gone, defunct, forgotten, inaccessible so $scan == 'foo'
####
print "
=======================================
Here is my really groovy script
I hope you like it
Call it with --help to get the synopsis
=======================================
";
####
print "Oops";
die;
# might as well be
die "Oops";
####
open FILE, $somefile or die "Could not read $somefile, perl says the error is $!\n";
# note if you want to use || rather than or you need to be aware
# that it binds tighter than the , operator so you need parnes thusly
open (FILE, ">$somefile") || die "Could not write $somefile, $!\n";
####
while ( my $line= ) { blah }
####
volume ;my_hacking_script_that_will_now_get_executed.pl
####
system(@stuff)
check_call($custom_message,@stuff);
sub check_call {
my $custom_message = shift;
my @call = @_;
# check for 0 return status
return unless $?;
# bugger, we have an error so tell all
# Work out return code
my $rc = $? >> 8;
die qq!
System call failed
Error code $rc
$custom_message
Actual call was
system( "@call" )!;
}
####
use Carp;
one();
sub one { two() }
sub two { three() }
sub three{ confess "It took a while to get here, but I know just how I did!" }
__DATA__
It took a while to get here, but I know just how I did! at script line 7
main::three() called at script line 6
main::two() called at script line 5
main::one() called at script line 3
####
#!/usr/bin/perl -w
use strict;
use Error::Custom;
synopsis() unless @ARGV;
system(@args);
check_call('custom message',@args); # this lives in Error::Custom
system(@args1);
check_call('more custom messages',@args1);
exit;
sub synopsis {
# note that $0 contains the script name so the name will
# always be the correct one no matter what you call the script
print "
Synopsis $0 SOME_FLAG SOME_ARG
blah
blah\n\n";
exit;
}
####
package Error::Custom;
use strict;
use Exporter;
use Carp;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK );
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = qw(check_call);
@EXPORT_OK = qw(check_call);
sub check_call {
my $custom_message = shift;
my @call = @_;
# check for 0 return status
return unless $?;
# bugger, we have an error so tell all
# Work out return code
my $rc = $? >> 8;
confess qq!
System call failed
Error code $rc
$custom_message
Actual call was
system( "@call" )
!;
}
1;