Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Perl 'Storable'

  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    Perl 'Storable'

    I have a program that first call a .cgi file, then it calls a html, after that, the html will call another .cgi file. In order to store variables, I am trying to use 'storable'.

    In my first .cgi file, I used this:
    Code:
    use Storable qw(store retrieve freeze thaw dclone);
    %color = ('Blue' => 'aaa', 'Red' => 'bbb', 'Black' => 'ccc', 'White' => 'ddd');
    store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n";
    In my second .cgi file, I used this:
    Code:
    use Storable qw(store retrieve freeze thaw dclone);
    $colref = retrieve('mycolors');
    die "Unable to retrieve from mycolors!\n" unless defined $colref;
    $ext_folder = $colref->{'Blue'};
    But it is not working as expected. What is the problem here?

    Thank you.

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    What are you wanting to do actually? Are you wanting to share this constant data structure between those two cgi files, so you don't have to define it twice?

    Then I would suggest that you create a package that enabled you to import it into each cgi file.

    Code:
    package MyColors;
    # MyColors.pm
    
    use Exporter qw(import);
    our @EXPORT = ();
    our @EXPORT_OK = qw(%color);
    
    use strict;
    
    our %color = (
    	'Blue'  => 'aaa',
    	'Red'   => 'bbb',
    	'Black' => 'ccc',
    	'White' => 'ddd',
    );
    
    1;
    and

    Code:
    # Your .cgi files.
    
    use strict;
    use warnings;
    
    use MyColors qw(%color);
    use Data::Dumper;
    
    print Dumper(\%color);
    However, if you're trying send your second cgi file other information, then I would ask, how are you calling it? Is there a way to send the information directly?

  • Users who have thanked miller for this post:

    camaross (04-09-2011)

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    I have a program that first call a .cgi file, then it calls a html, after that, the html will call another .cgi file. In order to store variables, I am trying to use 'storable'.

    In my first .cgi file, I used this:
    Code:
    use Storable qw(store retrieve freeze thaw dclone);
    %color = ('Blue' => 'aaa', 'Red' => 'bbb', 'Black' => 'ccc', 'White' => 'ddd');
    store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n";
    In my second .cgi file, I used this:
    Code:
    use Storable qw(store retrieve freeze thaw dclone);
    $colref = retrieve('mycolors');
    die "Unable to retrieve from mycolors!\n" unless defined $colref;
    $ext_folder = $colref->{'Blue'};
    But it is not working as expected. What is the problem here?

    Thank you.
    i guess what is wrong, you need to serialize and retrive into/from a file. The example form Storable work because is in the same script.

    you need to explain what do you mean by 'not working as expected', we don't know what you intend to achive and what you get.

    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-09-2011)

  • #4
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Sorry, I should had made it clear in my first post. In the first cgi file, I capture information from user's input out of an html. Then I want to link it to a sophicated program that I purchased to enable users to upload files (the second cgi file).

    Thank you!



    Quote Originally Posted by miller View Post
    What are you wanting to do actually? Are you wanting to share this constant data structure between those two cgi files, so you don't have to define it twice?

    Then I would suggest that you create a package that enabled you to import it into each cgi file.

    Code:
    package MyColors;
    # MyColors.pm
    
    use Exporter qw(import);
    our @EXPORT = ();
    our @EXPORT_OK = qw(%color);
    
    use strict;
    
    our %color = (
    	'Blue'  => 'aaa',
    	'Red'   => 'bbb',
    	'Black' => 'ccc',
    	'White' => 'ddd',
    );
    
    1;
    and

    Code:
    # Your .cgi files.
    
    use strict;
    use warnings;
    
    use MyColors qw(%color);
    use Data::Dumper;
    
    print Dumper(\%color);
    However, if you're trying send your second cgi file other information, then I would ask, how are you calling it? Is there a way to send the information directly?

  • #5
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you for the reply. The problem I had is that the variable is not passed to the next cgi file. Good to know that I need to sterilize it.


    Quote Originally Posted by oesxyl View Post
    i guess what is wrong, you need to serialize and retrive into/from a file. The example form Storable work because is in the same script.

    you need to explain what do you mean by 'not working as expected', we don't know what you intend to achive and what you get.

    best regards

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by camaross View Post
    Thank you for the reply. The problem I had is that the variable is not passed to the next cgi file. Good to know that I need to sterilize it.
    i suppose is a typo but just in case, is not sterilize, serialize that mean send data somewhere in a given format so you can retrive later.
    see freeze and thaw in Storable module.

    best regards

  • Users who have thanked oesxyl for this post:

    camaross (04-09-2011)

  • #7
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thanks!

    Quote Originally Posted by oesxyl View Post
    i suppose is a typo but just in case, is not sterilize, serialize that mean send data somewhere in a given format so you can retrive later.
    see freeze and thaw in Storable module.

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •