We can save the data structure like hashes, arrays, and their combinations to local file and read them back, which can be called as serializing and deserialzing the DS in perl.
Example 1: writing hash to a file and red it back
save_and_read_perl_ds_to_file.pl
#!/usr/bin/perl
use Data::Dumper;
system("clear");
#sample hash
my %h1 = (
k1 => 'v1',
k2 => 'v2',
k3 => 'v3'
);
print "before writing to a file:\n";
print Dumper(\%h1);
#write perl hash to a file using data dumper
open FH, '>', 'ds_file.csv' or die "cant open file";
print FH Data::Dumper->Dump([\%h1]); #NOTE [ %h1 ] unlike print Dumper(\%h1);
close FH;
#read the hash from file
open FH, 'ds_file.csv' or die "$!";
local $/; #slurp mode http://perl-savvy.blogspot.in/2013/01/perl-slurp-mode.html
$hashref = eval <FH>;
close FH;
print "\nafter writing and reading back again :\n ";
print Dumper($hashref);
-----------------------------------------------------------------------
ds_file.csv
$VAR1 = {
'k2' => 'v2',
'k1' => 'v1',
'k3' => 'v3'
};
As you can see the contents of file where in we dumped the hash, in the perl file save_and_read_perl_ds_to_file.pl, we do eval on file_handle of this file, which returns the hash reference.
Friday, January 18, 2013
perl slurp mode
Perl can change the mode of how to read a file.
$/
The input record separator, newline by default. $/ may be set to a value longer than one character in order to match a multi-character delimiter. If $/ is undefined, no record separator is matched, and <FILEHANDLE>will read everything to the end of the current file.
Example:
open FILEHANDLE, 'somefile.txt' or die $!;
local $/;
my $string = <FILEHANDLE>;
Friday, January 11, 2013
AJAX - perl
CALLING SERVER SIDE PERL SCRIPT THROUGH AJAX
Create an XMLHttpRequest Object:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
USING GET METHOD:
xmlhttp.send();
OR
xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true);
xmlhttp.send();
xmlhttp.open("POST","demo_get.pl",true);
xmlhttp.send();
OR
xmlhttp.open("POST","demo_get2.asp?fname=Henry&lname=Ford",true);
xmlhttp.send();
Server Response:
xmlhttp.responseText //get the response data as a string
xmlhttp.responseXML //get the response data as XML data
The onreadystatechange event:
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
here_is_response=xmlhttp.responseText;
}
}
The above function is automatically called when the status of response is changed. Specifically used while AJAX, which is TRUE value in caller.
While getting the response for Synchronous call, which is FALSE value in caller, we use it straight forward:
xmlhttp.open("GET","demo_get.pl?resultFileName="+resultFileName,false);
xmlhttp.send();
reply=xmlhttp.responseText;
here, server side script can by anything: php, perl, asp, jsp, etc
html form onsubmit event with two submit buttons
It is not possible to know which submit button is clicked in onsubmit() of form, when there are two submit buttons on the same form.
e.g.
<html>
<head>
<script>
function verifySubmit()
{
alert("in verifySubmit");
return false;
}
</script>
</head>
<body>
alohaaa
<form name="frm1" onsubmit="return verifySubmit();">
<input type="text" name="fname">
<input type="submit" value="Submit">
<input type="submit" value="Delete">
</form>
</body>
</html>
In above case, there is no way out to perform different operations depending on which submit is clicked; submit or delete.
SOLUTION ::
<script language="javascript" type="text/javascript">
function verifyData(button) {
// validate
var returnVal;
switch (button.value) {
case "submit1":
alert("submit1");
returnVal=true;
break;
case "submit2":
alert("submit2");
returnVal=false; break;
};
return returnVal; }
</script>
<form method="post">
<input type="submit" value="submit1" onclick="return verifyData(this);">
<input type="submit" value="submit2" onclick="return verifyData(this);">
</form>
NOTE:
e.g.
<html>
<head>
<script>
function verifySubmit()
{
alert("in verifySubmit");
return false;
}
</script>
</head>
<body>
alohaaa
<form name="frm1" onsubmit="return verifySubmit();">
<input type="text" name="fname">
<input type="submit" value="Submit">
<input type="submit" value="Delete">
</form>
</body>
</html>
In above case, there is no way out to perform different operations depending on which submit is clicked; submit or delete.
SOLUTION ::
<script language="javascript" type="text/javascript">
function verifyData(button) {
// validate
var returnVal;
switch (button.value) {
case "submit1":
alert("submit1");
returnVal=true;
break;
case "submit2":
alert("submit2");
returnVal=false; break;
};
return returnVal; }
</script>
<form method="post">
<input type="submit" value="submit1" onclick="return verifyData(this);">
<input type="submit" value="submit2" onclick="return verifyData(this);">
</form>
NOTE:
- Action is not present means, submit would reload this page
- If onclick is true, it will submit the request.
- If onlclick is false, it will not submit the request.
Subscribe to:
Posts (Atom)