Search code examples
pathdocument-rootdancer

How to get the public path in Perl Dancer


Probably a silly question, but how do I get the path to the /public folder in Dancer?

I want to store/read csv files under the public folder, but don't know if Dancer offers any convenience methods to get the base path to the public folder.

The error I get when trying to create a file by saying:

open(FILE, ">>", "myapp/public/file.csv") or die "$!";

is:

No such file or directory in /ur/share/perl5/Dancer/Handler.pm l. 98

I'm not sure why it's going to Handler.pm?


Solution

  • My first answer is, don't do it that way...for two reasons:

    1. You're potentially opening up a security issue. What if somehow a hacker figures out a way to write to your environment files, change your passwords, etc.?
    2. You really want your /public files to be static, for version control, portability, etc.

    That being said, if you really still want to do this, the public dir lives in config->{public}:

    print "Public dir:".config->{public}."\n";
    

    Source: http://search.cpan.org/~xsawyerx/Dancer-1.3110/lib/Dancer/Config.pm#public_%28directory%29