Perl default defined variables – store and restore

This is a little Script to store all default defined variables from Perl(Linux) into an Scalar ant then to restore everything like it was before the store function.

{
my @_STORE_Course = ();         #scalar for Regex and program course
my @_STORE_Environment = ();    #scalar for environment
	#store sub
	sub StoreDefVars(){
		@_STORE_Course = ($.,$/,$\,$,,$",$|,$%,$=,$-,$~,$^,$:,$^L,$^A);
		@_STORE_Environment = ($^W,$^X);
	}
	#load sub
	sub LoadDefVars(){
		($.,$/,$\,$,,$",$|,$%,$=,$-,$~,$^,$:,$^L,$^A) = @_STORE_Course;
		($^W,$^X) = @_STORE_Environment;
	}
	sub ChangedDefVars(){
	    my @_CACHE = ($.,$/,$\,$,,$",$|,$%,$=,$-,$~,$^,$:,$^L,$^A);
	    for(0..$#_STORE_Course){
	        return(1) if($_STORE_Course[$_] ne $_CACHE[$_]);
	    }
	    
	    @_CACHE = ($^W,$^X);
	    for(0..$#_STORE_Environment){
	        return(1) if($_STORE_Environment[$_] ne $_CACHE[$_]);
	    }
	    return(0);
	}
	sub PrintDevVars(){
	    print dumper(\@_STORE_Course),"\n";
	    print dumper(\@_STORE_Environment),"\n";
	}
}

Important to include my Data-Dumper !

If you want to store your data in the intern arrays, run this:

StoreDefVars();
  • scalar for Regex and program course are stored in “@_Course”
  • scalar for environment are stored in “@_Environment”
  • hashes and arrays are not stored because they are global or lower scope

only variables that writeable are getting stored.

Then restore your saved variables:

LoadDefVars();

you could see if you changed any default variable with(1=changed):

ChangedDefVars()

Or you could print all Stored Variables:

PrintDevVars

(Important to include my dumper.)

 


scalar for Regex and program course:

$_ – contains current value of a loop

for("one","two","three"){
    print $_." ";
}
one two three

$& – contains match of the current Regex
$` – contains the left side of the current Regex
$’– contains the right side of the current Regex

my $var = "one two three";
$var =~ /two/;
print $`,"-",$&,"-",$',"\n";
one -two- three

$1,$2,.. – contains matching groups of Regex
$+ – contains the last group match of an Regex

my $var = "1 2 3 4 5 6 7 8 9";
$var =~ /(\d) (\d) (\d) (\d) (\d) (\d) (\d) (\d) (\d)/;
print $1,"-",$2,"-",$3,"-",$+,"\n";
1-2-3-9

$. – contains the current line number of the input file

open(DATEI, "<test.txt");
while(<DATEI>)
 { print $.," -> ",$_; }
close(DATEI);
1 -> first line
2 -> second line
3 -> last line

$/ – sets the input record separator

print "test".$/;
$/ = ";\n";
print "test".$/;
test
test;

$\ – sets the output record separator, added to every print.

print "test\n";
$\ = ";\n";
print "test";
test  
test;

$, – sets the output field separator, added to ever coma separated element

print 1,2,3,4,"\n";
$, = "-";
print 1,2,3,4,"\n";
1234      
1-2-3-4-

$” – sets the output list separator, added to ever coma separated list

my @arr = (1,2,3,4);
print "@arr\n";
$" = "-";
print "@arr\n";
1 2 3 4   
1-2-3-4

$| – output auto flush, disables the output buffer. 0=off, 1=on

 


Format:

$% – current page number
$= – number of lines per page
$- – number of lines left on the page
$~ – name of the current format
$^ – name of the current head format
$: – delimiter of multiple lines
$^L – delimiter of multiple pages
$^A – accumulator

 


scalar for environment are stored:
$? – contains the last status of a pipe or child process

system($test) == 0
  or die "on system: $?"
on system: 65280

$! – contains the error status of a pipe or child process.

open(DATEI, "<file.txt")
  or die "error: $!";
error: No such file or directory at main.pl line 3.

$@ – contains an empty string if eval works, and if not it contains the error

eval { test("dir"); };
print "eval error:$@\n" if($@);
eval&nbsp;error:Undefined&nbsp;subroutine&nbsp;&main::test&nbsp;called&nbsp;at&nbsp;main.pl&nbsp;line&nbsp;3.

$$ – process id
$< – real user id
$> – effective user id
$( – real group id
$) – effective group id

print "Process ID:$$ \n" ;
print "from | real | effective \n" ;
print "user | $<   | $> \n" ;
print "group| $(   | $) \n" ;
Process ID:23024 
from | real | effective 
user | 20104   | 20104 
group| 20104 20104   | 20104 20104

$0 – current program name

print "I am :$0\n" ;
I am :main.pl

$] – current Perl version(is deprecated use $^V instead)

print "Perl Version $] \n" ;
Perl Version 5.018004

$^D – current debugging flag, run your program wit the -D parameter
$^F – highest file descriptor
$^H – contains the number of syntax checks from use strict and other checks
$^I – is defined or undefined if the Inplace flag is set
$^M – could save data if no more ram is available
$^O -contains the current operating system

print "OS: $^O \n" ;
OS: linux

$^P – contains the current debugging status
$^S – the status of the per interpreter, if eval is running its undefined
$^T – contains the start time of the script

sleep(4);
print "Time:".(time()-$^T)."s".$/;
Time:4s

$^V -current Perl interpreter version

print "Perl Version $^V \n" ;
Perl Version v5.18.4

$^W – contains warning from the -w parameter, 1 if set, else 0
$^X – contains the current Perl interpreter path

print "Perl interpreter: $^X \n" ;
Perl interpreter: /usr/bin/perl

 


hashes and arrays:
@_ – contains the parameter from calling a function

test(1,2,3,4,5);
sub test{
    for(@_){ print $_.", ";}
}
1, 2, 3, 4, 5,

@ARGV – contains the parameter from calling your Perl script

for(@ARGV){ print $_.", ";}
$ perl main.pl one twoe three
one, twoe, three,

@INC -paths where Perl looks for Modules

for(@INC){ print $_.", ";}
/usr/local/lib64/perl5, /usr/local/share/perl5, /usr/lib64/perl5/vendor_perl, /usr/share/perl5/vendor_perl, /usr/lib64/perl5, /usr/share/perl5, .,

@ISA – contains a list of basic classes for object oriented
@EXPORT – Used for Modules to export
@EXPORT_OK – Used for Modules to export, but optional
%ENV – contains environment variables from your system

for(keys %ENV){ print $_.", ";}
LD_LIBRARY_PATH, SMLROOT, GNUSTEP_USER_ROOT, GUILE_LOAD_PATH, GNUSTEP_HOST_CPU, SCRIBACONF, TERM, _, INFOPATH, GNUSTEP_SYSTEM_ROOT, GNUSTEP_HOST, GNUSTEP_FLATTENED, GNUSTEP_MAKEFILES, GNUSTEP_HOST_VENDOR, HOSTNAME, GNUSTEP_HOST_OS, HAXE_STD_PATH, SHLVL, LIBRARY_COMBO, DART_SDK, GNUSTEP_PATHLIST, GNUSTEP_LOCAL_ROOT, PWD, GNUSTEP_NETWORK_ROOT, CLASSPATH, HOME, PATH, GNUSTEP_IS_FLATTENED,

%SIG – contains Signal Handler to communicate to other processes

for(keys %SIG){ print $_.", ";}
PIPE, RTMIN, PWR, NUM32, IOT, NUM57, NUM45, STKFLT, NUM53, NUM62, CHLD, NUM36, KILL, NUM37, RTMAX, STOP, NUM39, NUM61, POLL, NUM38, NUM41, NUM47, XFSZ, URG, NUM50, NUM33, TSTP, TTOU, NUM52, PROF, INT, CONT, UNUSED, NUM56, XCPU, NUM55, CLD, FPE, NUM63, TERM, NUM48, NUM42, ABRT, USR1, TTIN, NUM35, ILL, NUM49, NUM43, NUM58, USR2, TRAP, NUM60, NUM51, BUS, SYS, NUM40, HUP, WINCH, IO, ALRM, NUM59, NUM44, NUM46, SEGV, QUIT, VTALRM, NUM54,

 

If you want a list as comment block use this:

=INFO
## programm course
$_ – contains current value of a loop
$& – contains match of the current Regex
$` – contains the left side of the current Regex
$' – contains the right side of the current Regex
$+ – contains the last group match of an Regex
$. – contains the current line number of the input file
$/ – sets the input record separator
$\ – sets the output record separator, added to every print.
$, – sets the output field separator, added to ever coma separated element
$” – sets the output list separator, added to ever coma separated list
$| – output auto flush, disables the output buffer. 0=off, 1=on
## Format
$% – current page number
$= – number of lines per page
$- – number of lines left on the page
$~ – name of the current format
$^ – name of the current head format
$: – delimiter of multiple lines
$^L – delimiter of multiple pages
$^A – accumulator
## Enviroment
$? – contains the last status of a pipe or child process
$! – contains the error status of a pipe or child process.
$@ – contains an empty string if eval works, and if not it contains the error
$$ – process id
$< – real user id
$> – effective user id
$( – real group id
$) – effective group id
$0 – current program name
$^V – current Perl version
$^D – current debugging flag, run your program wit the -D parameter
$^F – highest file descriptor
$^H – contains the number of syntax checks from use strict and other checks
$^I – is defined or undefined if the Inplace flag is set
$^M – could save data if no more ram is available
$^O -contains the current operating system
$^P – contains the current debugging status
$^S – the status of the per interpreter, if eval is running its undefined
$^T – contains the start time of the script
$^W – contains warning from the -w parameter, 1 if set, else 0
$^X – contains the current Perl interpreter path
=cut

15 thoughts on “Perl default defined variables – store and restore”

  1. Aw, this was an exceptionally nice post. Spending some time
    and actual effort to create a superb article… but what can I
    say… I hesitate a lot and never manage to get anything done.

  2. I have been exploring for a little for any high quality articles or blog posts in this sort of area .
    Exploring in Yahoo I finally stumbled upon this web
    site. Reading this info So i am happy to convey that I’ve an incredibly good uncanny feeling I
    found out just what I needed. I so much undoubtedly will make
    certain to do not put out of your mind this web site and give it a glance on a relentless basis.

  3. I’m amazed, I have to admit. Rarely do I encounter a blog that’s equally
    educative and amusing, and let me tell you, you have hit the nail on the head.
    The problem is an issue that not enough people are
    speaking intelligently about. Now i’m very happy that I stumbled
    across this in my search for something relating to this.

  4. Thank you for the auspicious writeup. It in fact was once a leisure account it.
    Look complicated to far brought agreeable from you! However,
    how can we be in contact?

  5. I’m not that much of a internet reader to be honest but your sites really nice,
    keep it up! I’ll go ahead and bookmark your site to come back later.

    Many thanks

  6. Good post! We will be linking to this particularly great post on our site.

    Keep up the great writing.

  7. I was wondering if you ever considered changing the structure of your site?
    Its very well written; I love what you have got to say.

  8. Very nice post. I just stumbled upon your weblog and wanted to say
    that I have really enjoyed browsing your blog posts.
    After all I will be subscribing to your rss feed and I hope you write again soon!

  9. Hey There. I found your blog using msn. This is an extremely well written article.
    I’ll make sure to bookmark it and return to
    read more of your useful information. Thanks for the post. I’ll definitely return.

  10. Why people still use to read news papers when in this technological
    world all is existing on web?

  11. Excellent items from you, man. I have take note your stuff previous to and you’re simply extremely excellent.
    I actually like what you have received right here, certainly like what you are stating and the way in which you say it.
    You’re making it entertaining and you continue to take care of to stay it sensible.
    I can’t wait to read much more from you. This is really a great website.

  12. Excellent post. I was checking continuously this weblog and I am
    inspired! Extremely useful information specifically the final phase 🙂 I deal with such information much.
    I used to be seeking this particular information for a very lengthy time.
    Thank you and best of luck.

Leave a Reply

Your email address will not be published.

four × 3 =