Store multi-dimensions array for use in latter form?

S

shareparadise

Hi all,
I need a way to store a multi-dimensions array for later use in a
multi-steps form (the array created in the 1st form and need to be used
in 2nd, 3rd,... form).

I dont have permission to install session module on my server, so
sessions is not an option.

I tried to use cookie, but seem like I couldnt retrieve it back, I dont
know if I did it wrong somewhere or just Perl doesnt support that?

Can anyone please show me what I could do in this situation?

Thank you so much!
 
X

xhoster

Hi all,
I need a way to store a multi-dimensions array for later use in a
multi-steps form (the array created in the 1st form and need to be used
in 2nd, 3rd,... form).

Where does the array come from in the first place?
I dont have permission to install session module on my server,

Why not? Because they don't want to bother installing it for you?
(In which case, you could probably figure out how to install it on
your own.) Or because they don't want you to use such a module for
policy reasons? (In which case, you should ask your micro-manager how
to proceed, rather than asking us. Who knows what other assinine policies
our recommendations will unknowningly violate.)
I tried to use cookie, but seem like I couldnt retrieve it back, I dont
know if I did it wrong somewhere or just Perl doesnt support that?

Since you didn't show us what you did, we can't comment on whether you did
it rightly or wrongly. Maybe it is just too big to be stored in a cookie.
Can anyone please show me what I could do in this situation?

I often store it in a hidden field. It has to be serialized, and it has
to be re-validated when you parse it again. Since the AoA usualy was
created by parsing some block of text anyway, I generally use that same
block of text as the serialized form.

Xho
 
S

shareparadise

This project I'm doing is an assignment which will be tested on the
school server, and I know that server doesnt have session module
installed. I dont know if the grader would be kind enough to install it
for me, and I dont want to risk here.


This is how I tried to do it with cookie:

my $query = new CGI;

my $cookie = $query->cookie( -name => 'table',
-value => @table, # tried
$table and \@table as well
-expires => '+1M',
-path => '/'
);
print
$query->header(-cookie=>$cookie),$query->start_html(-title=>"Testing2");
# .....

# Then tried to retrieve it using this:
my @table = $query->cookie(-name=>'table');

Actually I tried with 1 dim array and it worked well, I dont know what
goes wrong here? This is my first hand experience with Perl so Im still
kinda confused.

Thanks.
 
T

Ted Zlatanov

This project I'm doing is an assignment which will be tested on the
school server, and I know that server doesnt have session module
installed. I dont know if the grader would be kind enough to install
it for me, and I dont want to risk here.

You'll find very few people willing to help with homeworks here.

Ted
 
S

shareparadise

This homework is not about passing the array between forms, Ted. It's a
XML processor, and Im not asking anyone to write that for me. Im stuck
at passing the array between form, and I ask for help with that, tell
me what's wrong with it then?
If you dont see if you can help then it's fine, you dont need to add
any uh-helpful comment like that!
 
S

Scott Bryce

This homework is not about passing the array between forms, Ted. It's a
XML processor, and Im not asking anyone to write that for me.

No need. There are several good ones at CPAN.
Im stuck
at passing the array between form, and I ask for help with that, tell
me what's wrong with it then?

You can't pass an AoA between forms. What you need to do is store the
data contained in the AoA on the server, then pass an identifier to the
form that will allow you to find the data later and re-build the AoA.

Take a look at the posting guidelines for this group, posted here twice
a week, then write a short but complete program the attempts to pass an
array of arrays between forms. Someone here will be able to comment on it.
If you dont see if you can help then it's fine, you dont need to add
any uh-helpful comment like that!

With that attitude, you shouldn't expect a lot of help here.
 
X

xhoster

This project I'm doing is an assignment which will be tested on the
school server, and I know that server doesnt have session module
installed. I dont know if the grader would be kind enough to install it
for me, and I dont want to risk here.

This is how I tried to do it with cookie:

my $query = new CGI;

my $cookie = $query->cookie( -name => 'table',
-value => @table,
# tried $table and \@table as well
-expires => '+1M',
-path => '/'
);
print
$query->header(-cookie=>$cookie),$query->start_html(-title=>"Testing2");
# .....

# Then tried to retrieve it using this:
my @table = $query->cookie(-name=>'table');

Actually I tried with 1 dim array and it worked well, I dont know what
goes wrong here?

When you tried to use the multidim table, what actually happened?

Which one worked well with the 1 dim array, the @table, the \@table, or the
$table?
This is my first hand experience with Perl so Im still
kinda confused.

CGI's cookie features doesn't inherently handle multidimensional cookies.
As far as I know, there is no magic incantation (or module) to make it
handle them. You need to write something yourself to join and escape the
data down to either a single dimension or a single string. Generally the
way to do this is obvious based on the structure of your data (i.e. if your
data is numbers with no "weird" characters, just join them on tabs for one
layer and newlines for the other, etc.), or if you got the multidim data
by parsing a string in the first place, store the string itself and
reparse.

If you wanted something more general, you could always dig into the guts of
CGI to use it's internal methods, like CGI::Util::escape() and unescape().


Xho
 
T

Tad McClellan

This homework is not about passing the array between forms, Ted. It's a
XML processor, and Im not asking anyone to write that for me. Im stuck
at passing the array between form, and I ask for help with that, tell
me what's wrong with it then?


Nothing. But we could not tell that from your original post.

It is a fairly common thing hereabouts for folks to ask us to
do their programming for them.

If you dont see if you can help then it's fine, you dont need to add
any uh-helpful comment like that!


There _is_ something wrong with displaying that bad attitude though.

So long!
 
T

Ted Zlatanov

This homework is not about passing the array between forms, Ted. It's a
XML processor, and Im not asking anyone to write that for me. Im stuck
at passing the array between form, and I ask for help with that, tell
me what's wrong with it then?

Note your original text above. The problem you are trying to solve is
a part of the homework. You are supposed to do it on your own, aren't
you? If not, you should specifically say that it's unrelated or that
you are allowed to seek outside help.
If you dont see if you can help then it's fine, you dont need to add
any uh-helpful comment like that!

My response was very helpful, actually. You should take one of two
lessons out of it:

1) don't say it's for a homework problem next time and post under a
different name.

2) do the work on your own, THEN talk to your teacher/TF/course aide
if you can't figure it out, THEN ask on Usenet if they say it's OK
and can't help you further.

I hope you choose the latter. But my goal was just to tell you that
you won't get much help if it's for a homework, not to pass judgment.

As for passing complex data between sessions, store a simple key
(e.g. an integer) in the cookie, and that's your key into a database.
The choice of key and database are up to you, since you didn't tell
us what resources are available on the server.

Generally if the choice is between writing your own code and relying
on a module, try to get the module installed before you write your
own. Trust me, you don't want to reinvent the wheel.

Ted
 
M

Mumia W. (reading news)

This project I'm doing is an assignment which will be tested on the
school server, and I know that server doesnt have session module
installed. I dont know if the grader would be kind enough to install it
for me, and I dont want to risk here.


This is how I tried to do it with cookie:

my $query = new CGI;

my $cookie = $query->cookie( -name => 'table',
-value => @table, # tried
$table and \@table as well

That won't work because you will only get the size of @table into the value.
-expires => '+1M',
-path => '/'
);
print
$query->header(-cookie=>$cookie),$query->start_html(-title=>"Testing2");
# .....

# Then tried to retrieve it using this:
my @table = $query->cookie(-name=>'table');

Actually I tried with 1 dim array and it worked well, I dont know what
goes wrong here? This is my first hand experience with Perl so Im still
kinda confused.

Thanks.

If table is small enough, you can place the entire table into the
cookie; however, cookies only accept scalar values, and tables don't go
into scalars naturally; you need to convert them.

Use Data::Dumper to convert the table onto a scalar string, URL-encode
that string and place it into the cookie.

To extract the table data from the cookie, URL-decode the cookie string
and "eval" the decoded string.

If the data is truly simple, you can write your own subs for converting
the table into a string and back again.
 
X

xhoster

Mumia W. (reading news) said:
To extract the table data from the cookie, URL-decode the cookie string
and "eval" the decoded string.

You shouldn't eval a string that is coming from a cookie. Who knows what
someone stuck in that string when you weren't looking.

Xho
 
M

Mumia W. (reading news)

You shouldn't eval a string that is coming from a cookie. Who knows what
someone stuck in that string when you weren't looking.

Xho

Yow! You're right. And since the OP can't install modules such as
FreezeThaw, a custom encoding function is the only way.
 
S

shareparadise

Unless you can save it into the database or somewhere on the server and
then retrieve it for later use, there is no way to prevent someone to
do something bad to the cookie or even to the hidden field if you are
to use form.
There are many many things to consider if I want to make this project
useful in real world, but that's not the goal of this one. This project
is built based on the assumption that the input file is "well-formed"
and valid, and the users are nice. Anyway, I tried the following code:

use Data::Dumper;
my $table_string = Data::Dumper->Dump(\@table);
# pass it in a hidden field/ or maybe set cookie, it doesnt really
matter tho.
print "<input type='hidden' name = 'table' value = \"$table_string\">";


# retrieve here (of course this is after we submit the form along with
that hidden field already.
my $temp = $query->param("table");
eval $temp;

I was able to retrieve the string back, but couldnt eval it back to the
original table.
 
S

Scott Bryce

use Data::Dumper;
my $table_string = Data::Dumper->Dump(\@table);
# pass it in a hidden field/ or maybe set cookie, it doesnt really
matter tho.
print "<input type='hidden' name = 'table' value = \"$table_string\">";

$table_string probably contains special characters that will need to be
encoded.

http://search.cpan.org/~gaas/HTML-Parser-3.55/lib/HTML/Entities.pm

Also, if I am going to create a string the contains quotes, I would rather

print qq(<input type='hidden' name='table' value="$table_string">);
 
A

anno4000

Mumia W. (reading news) said:
Yow! You're right. And since the OP can't install modules such as
FreezeThaw, a custom encoding function is the only way.

Storable is a core module that does what's needed without eval().

Anno
 
S

shareparadise

@anno4000: I prefer not to store to file, but I'll definetely check it
out right now.
@Scott Bryce: cool, I actually didnt know I could print that way.
Anyway, I tried the encode you suggested + print qq(<input
type='hidden' name='table' value="$table_string">), the result is still
the same tho. BTW, I print out my $table_string here: (It's lots of
lines, Im sorry *_*)

$VAR1 = [ 0, 'element', 'shiporder', undef, undef, undef, undef ];
$VAR2 = [ 1, 'element', 'orderperson', 'string', undef, undef, undef ];
$VAR3 = [ 1, 'element', 'shipto', undef, undef, undef, undef ]; $VAR4 =
[ 2, 'element', 'name', 'string', undef, undef, undef ]; $VAR5 = [ 2,
'element', 'address', 'string', undef, undef, undef ]; $VAR6 = [ 2,
'element', 'city', 'string', undef, undef, undef ]; $VAR7 = [ 2,
'element', 'country', 'string', undef, undef, undef ]; $VAR8 = [ 1,
'element', 'item', undef, undef, 'unbounded', undef ]; $VAR9 = [ 2,
'element', 'title', 'string', undef, undef, undef ]; $VAR10 = [ 2,
'element', 'note', 'string', 0, undef, undef ]; $VAR11 = [ 2,
'element', 'quantity', 'positiveInteger', undef, undef, undef ]; $VAR12
= [ 2, 'element', 'price', 'decimal', undef, undef, undef ]; $VAR13 = [
1, 'attribute', 'orderid', 'string', undef, undef, 'required' ];

And the dump:
# table

* $VAR1 = [
0,
'element',
'shiporder',
undef,
undef,
undef,
undef
];
$VAR2 = [
1,
'element',
'orderperson',
'string',
undef,
undef,
undef
];
$VAR3 = [
1,
'element',
'shipto',
undef,
undef,
undef,
undef
];
$VAR4 = [
2,
'element',
'name',
'string',
undef,
undef,
undef
];
$VAR5 = [
2,
'element',
'address',
'string',
undef,
undef,
undef
];
$VAR6 = [
2,
'element',
'city',
'string',
undef,
undef,
undef
];
$VAR7 = [
2,
'element',
'country',
'string',
undef,
undef,
undef
];
$VAR8 = [
1,
'element',
'item',
undef,
undef,
'unbounded',
undef
];
$VAR9 = [
2,
'element',
'title',
'string',
undef,
undef,
undef
];
$VAR10 = [
2,
'element',
'note',
'string',
0,
undef,
undef
];
$VAR11 = [
2,
'element',
'quantity',
'positiveInteger',
undef,
undef,
undef
];
$VAR12 = [
2,
'element',
'price',
'decimal',
undef,
undef,
undef
];
$VAR13 = [
1,
'attribute',
'orderid',
'string',
undef,
undef,
'required'
];
 
X

xhoster

Storable is a core module that does what's needed without eval().

Storable is indubitably better than Dumper/eval, but how safe is it?
Can corrupt data cause "thaw" to execute arbitrary code, or take huge
amounts of memory/CPU, or segfault? I was surprised that I couldn't
find a discussion of this under either Storable or perlsec.

Xho
 
X

xhoster

Unless you can save it into the database or somewhere on the server and
then retrieve it for later use, there is no way to prevent someone to
do something bad to the cookie or even to the hidden field if you are
to use form.
There are many many things to consider if I want to make this project
useful in real world, but that's not the goal of this one. This project
is built based on the assumption that the input file is "well-formed"
and valid, and the users are nice. Anyway, I tried the following code:

use Data::Dumper;
my $table_string = Data::Dumper->Dump(\@table);
# pass it in a hidden field/ or maybe set cookie, it doesnt really
matter tho.
print "<input type='hidden' name = 'table' value = \"$table_string\">";

print CGI->hidden('table', $table_string);

It will automatically escape what needs escaping.
# retrieve here (of course this is after we submit the form along with
that hidden field already.
my $temp = $query->param("table");
eval $temp;

I was able to retrieve the string back, but couldnt eval it back to the
original table.

Why couldn't you?

Xho
 
S

shareparadise

I wonder if I'm doing something terriblly wrong here, This is part of
the codes:
$temp = $query->param("table");
eval $temp;
print $query->header(),$query->start_html(-title=>"Testing");

# Just try to print out the data
for ($i=0; $i<13; $i++){
for ($j=0; $j<7;$j++){
print ($table[$i][$j]);
print (" ---- ");
}
print ("<br>");
}

The loop couldnt print out a thing tho *_*. I knw I must be doing
something really wrong and stupid here, but cant figure out where.

SP
 
X

xhoster

I wonder if I'm doing something terriblly wrong here, This is part of
the codes:
$temp = $query->param("table");
eval $temp;

If you don't know if you are doing something wrong, you should ask Perl
to tell you when things go wrong.

eval $temp or die "eval of $temp failed with $@";

Also, you should take cgi out of the loop until you can get a normal
store/thaw cycle working as stand alone code.


Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top