get session info without route
Hello I use Dancer 1.3118 with YAML session in config. session: 'YAML' in config.yml in my file.pm I try to use my $PATHXLS = config->{'PATHXLS'}. session 'numclient' .'/'; if this ligne is in any route (get , post, any .. ) it's working for this route but if this line is at beginning of file, outside of route , and I want to use $PATHXLS, info of session 'numclient' is empty I tried to use hook 'before' my $PATHXLS; hook 'before' => sub { $PATHXLS = config->{'PATHXLS'}. session 'numclient' .'/'; }; debug $PATHXLS; but this is the same problem , $PATHXLS is defined but without session info do you have a idea to get a variable for all file with session info ? bye Hugues.
On 09/10/2013 11:03 AM, Hugues Max wrote:
Hello I use Dancer 1.3118 with YAML session in config.
session: 'YAML' in config.yml
in my file.pm I try to use
my $PATHXLS = config->{'PATHXLS'}. session 'numclient' .'/';
if this ligne is in any route (get , post, any .. ) it's working for this route
but if this line is at beginning of file, outside of route , and I want to use $PATHXLS, info of session 'numclient' is empty
I tried to use hook 'before'
my $PATHXLS; hook 'before' => sub { $PATHXLS = config->{'PATHXLS'}. session 'numclient' .'/'; };
debug $PATHXLS;
but this is the same problem , $PATHXLS is defined but without session info
do you have a idea to get a variable for all file with session info ? bye Hugues.
Session should work within "before" hook. It is better to use vars (valid through the current request) to store this type of information. Regards Racke -- Perl and Dancer Development Visit our Open Source conference on E-commerce: http://www.ecommerce-innovation.com/
Please see my hook ( here numclient = 366 ) hook 'before' => sub { var numclient => session 'numclient'; debug "session numclient ". session 'numclient';; debug "vars in hook ".vars->{'numclient'}; }; debug "vars out hook" . vars->{'numclient'}; and log [8425] debug @0.004796> [hit #1]session numclient 366 in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 25 [8425] debug @0.004964> [hit #1]vars in hook 366 in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 26 [8425] core @0.005126> [hit #1]entering before hook in /usr/local/share/perl5/Dancer/Hook.pm l. 58 [8425] core @0.090481> [hit #1]response: 200 in /usr/local/share/perl5/Dancer/Handler.pm l. 179 [8426] debug @0.000009> vars out hook in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 29 vars out hook is empty... Le 10/09/2013 12:47, Stefan Hornburg (Racke) a écrit :
On 09/10/2013 11:03 AM, Hugues Max wrote:
Hello I use Dancer 1.3118 with YAML session in config.
session: 'YAML' in config.yml
in my file.pm I try to use
my $PATHXLS = config->{'PATHXLS'}. session 'numclient' .'/';
if this ligne is in any route (get , post, any .. ) it's working for this route
but if this line is at beginning of file, outside of route , and I want to use $PATHXLS, info of session 'numclient' is empty
I tried to use hook 'before'
my $PATHXLS; hook 'before' => sub { $PATHXLS = config->{'PATHXLS'}. session 'numclient' .'/'; };
debug $PATHXLS;
but this is the same problem , $PATHXLS is defined but without session info
do you have a idea to get a variable for all file with session info ? bye Hugues.
Session should work within "before" hook. It is better to use vars (valid through the current request) to store this type of information.
Regards Racke
On 09/10/2013 04:05 PM, Hugues Max wrote:
Please see my hook ( here numclient = 366 )
hook 'before' => sub { var numclient => session 'numclient'; debug "session numclient ". session 'numclient';; debug "vars in hook ".vars->{'numclient'}; };
debug "vars out hook" . vars->{'numclient'};
and log
[8425] debug @0.004796> [hit #1]session numclient 366 in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 25 [8425] debug @0.004964> [hit #1]vars in hook 366 in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 26 [8425] core @0.005126> [hit #1]entering before hook in /usr/local/share/perl5/Dancer/Hook.pm l. 58 [8425] core @0.090481> [hit #1]response: 200 in /usr/local/share/perl5/Dancer/Handler.pm l. 179 [8426] debug @0.000009> vars out hook in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 29
vars out hook is empty...
Do you really use debug "vars out hook" . vars->{'numclient'}; outside a route? In this case, vars isn't populated as you don't have a current request. Regards Racke -- Perl and Dancer Development Visit our Open Source conference on E-commerce: http://www.ecommerce-innovation.com/
In fact a search a solution to DO NOT repeat in my code something like my $PATHXLS = config->{'PATHXLS'}.session 'numclient' for each route for the moment in: get '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient' post '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient' if I have 10 sessions setting, I need to add 10 lines per route etc... what is the solution ? Le 10/09/2013 16:08, Stefan Hornburg (Racke) a écrit :
On 09/10/2013 04:05 PM, Hugues Max wrote:
Please see my hook ( here numclient = 366 )
hook 'before' => sub { var numclient => session 'numclient'; debug "session numclient ". session 'numclient';; debug "vars in hook ".vars->{'numclient'}; };
debug "vars out hook" . vars->{'numclient'};
and log
[8425] debug @0.004796> [hit #1]session numclient 366 in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 25 [8425] debug @0.004964> [hit #1]vars in hook 366 in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 26 [8425] core @0.005126> [hit #1]entering before hook in /usr/local/share/perl5/Dancer/Hook.pm l. 58 [8425] core @0.090481> [hit #1]response: 200 in /usr/local/share/perl5/Dancer/Handler.pm l. 179 [8426] debug @0.000009> vars out hook in /home/faxexpert/DancerFx/lib/fx/outils.pm l. 29
vars out hook is empty...
Do you really use
debug "vars out hook" . vars->{'numclient'};
outside a route?
In this case, vars isn't populated as you don't have a current request.
Regards Racke
Is it possible to pass the 'session' hash to a subroutine and set it in there? This is something I did in Dancer2 for setting some sessions.. On Tue, Sep 10, 2013 at 4:16 PM, Hugues Max <huguesmax@gmail.com> wrote:
In fact a search a solution to DO NOT repeat in my code something like
my $PATHXLS = config->{'PATHXLS'}.session 'numclient' for each route
for the moment in: get '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient'
post '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient'
if I have 10 sessions setting, I need to add 10 lines per route etc... what is the solution ?
Le 10/09/2013 16:08, Stefan Hornburg (Racke) a écrit :
On 09/10/2013 04:05 PM, Hugues Max wrote:
Please see my hook ( here numclient = 366 )
hook 'before' => sub { var numclient => session 'numclient'; debug "session numclient ". session 'numclient';; debug "vars in hook ".vars->{'numclient'}; };
debug "vars out hook" . vars->{'numclient'};
and log
[8425] debug @0.004796> [hit #1]session numclient 366 in /home/faxexpert/DancerFx/lib/**fx/outils.pm l. 25 [8425] debug @0.004964> [hit #1]vars in hook 366 in /home/faxexpert/DancerFx/lib/**fx/outils.pm l. 26 [8425] core @0.005126> [hit #1]entering before hook in /usr/local/share/perl5/Dancer/**Hook.pm l. 58 [8425] core @0.090481> [hit #1]response: 200 in /usr/local/share/perl5/Dancer/**Handler.pm l. 179 [8426] debug @0.000009> vars out hook in /home/faxexpert/DancerFx/lib/** fx/outils.pm l. 29
vars out hook is empty...
Do you really use
debug "vars out hook" . vars->{'numclient'};
outside a route?
In this case, vars isn't populated as you don't have a current request.
Regards Racke
______________________________**_________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/**mailman/listinfo/dancer-users<http://lists.preshweb.co.uk/mailman/listinfo/dancer-users>
On 09/10/2013 04:16 PM, Hugues Max wrote:
In fact a search a solution to DO NOT repeat in my code something like
my $PATHXLS = config->{'PATHXLS'}.session 'numclient' for each route
for the moment in: get '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient'
post '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient'
if I have 10 sessions setting, I need to add 10 lines per route etc... what is the solution ?
Adding your session settings to vars in "before" hook and using these vars in routes should work. Regards Racke -- Perl and Dancer Development Visit our Open Source conference on E-commerce: http://www.ecommerce-innovation.com/
Yes it's works :-) thanks. hook 'before' => sub { var PATHXLS => config->{'PATHXLS'}. session 'numclient'; }; get '/outils' => sub { debug "vars in get /outils " . vars->{PATHXLS}; if I understand well, vars and session can't be use outside of route,OK but all rest of code can works correctly ? example: my $fichierXls my @fichier = glob($PATHXLS. '*'); # path will be not good foreach my $fichier(@fichier) { if ($fichier=~/\.xls$/i || $fichier=~/\.xlsx/i){ my ($filename, $directory ) = fileparse($fichier); $fichierXls=$filename; } } my $fichierXls is correctly defined and I can use everywhere in and out of route Le 10/09/2013 16:25, Stefan Hornburg (Racke) a écrit :
On 09/10/2013 04:16 PM, Hugues Max wrote:
In fact a search a solution to DO NOT repeat in my code something like
my $PATHXLS = config->{'PATHXLS'}.session 'numclient' for each route
for the moment in: get '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient'
post '/outils' => sub { I add: my $PATHXLS = config->{'PATHXLS'}.session 'numclient'
if I have 10 sessions setting, I need to add 10 lines per route etc... what is the solution ?
Adding your session settings to vars in "before" hook and using these vars in routes should work.
Regards Racke
On Tue, 10 Sep 2013 16:42:37 +0200 Hugues Max <huguesmax@gmail.com> wrote:
if I understand well, vars and session can't be use outside of route,OK but all rest of code can works correctly ?
Yeah - but code outside of route handlers / hooks will run at initial startup. That can be useful though, to set stuff up. In the example you gave, the global var you set can then be used within routes etc. A few comments on your code:
foreach my $fichier(@fichier) { if ($fichier=~/\.xls$/i || $fichier=~/\.xlsx/i){ my ($filename, $directory ) = fileparse($fichier); $fichierXls=$filename; } }
You can match .xls/.xlsx in one regex with e.g.: if ($fichier =~ /\.xlsx?$/i) { ... } You also missed anchoring to end of string in the second regex, so a filename like fooxlsxfoo.jpg would match your check. Also, you're going to look at every file you found and set $fichierXls each time you find a spreadsheet file, so you'll end up with just the filename of the last one found in $fichierXls. It's not clear if that's the desired behaviour or not. Cheers Dave P -- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
Thanks david if ($fichier =~ /\.xlsx?$/i) is better yes this is ambiguous code , I search all xls(x) files in directory , but I will take only one bye Hugues. Le 10/09/2013 17:04, David Precious a écrit :
On Tue, 10 Sep 2013 16:42:37 +0200 Hugues Max <huguesmax@gmail.com> wrote:
if I understand well, vars and session can't be use outside of route,OK but all rest of code can works correctly ? Yeah - but code outside of route handlers / hooks will run at initial startup. That can be useful though, to set stuff up. In the example you gave, the global var you set can then be used within routes etc.
A few comments on your code:
foreach my $fichier(@fichier) { if ($fichier=~/\.xls$/i || $fichier=~/\.xlsx/i){ my ($filename, $directory ) = fileparse($fichier); $fichierXls=$filename; } } You can match .xls/.xlsx in one regex with e.g.:
if ($fichier =~ /\.xlsx?$/i) { ... }
You also missed anchoring to end of string in the second regex, so a filename like fooxlsxfoo.jpg would match your check.
Also, you're going to look at every file you found and set $fichierXls each time you find a spreadsheet file, so you'll end up with just the filename of the last one found in $fichierXls. It's not clear if that's the desired behaviour or not.
Cheers
Dave P
On Tue, 10 Sep 2013 16:05:47 +0200 Hugues Max <huguesmax@gmail.com> wrote:
Please see my hook ( here numclient = 366 )
hook 'before' => sub { var numclient => session 'numclient'; debug "session numclient ". session 'numclient';; debug "vars in hook ".vars->{'numclient'}; };
debug "vars out hook" . vars->{'numclient'};
vars only exists within a request. Using it outside of a request (i.e. not in a route handler or suitable hook) is meaningless. Try e.g. hook before => sub { var pathxls => config->{'PATHXLS'} . session 'numclient'; }; get '/outils' => sub { # you can use vars->{pathxls} in your route handler, e.g. my $pathxls = vars->{pathxls}; }; -- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
participants (4)
-
David Precious -
Hugues Max -
Kovács Dávid [ Davs ] -
Stefan Hornburg (Racke)